Calling static C ++ member functions from C code

I have a bunch of C code. I do not intend to convert them to C ++ code.

Now I would like to name some C ++ code (I do not mind changing C ++ code so that they are called by C code).

class Utils { public: static void fun(); } class Utils2 { public: static std::wstring fun(); } 

If I tend to call them the following syntax, they won’t compile (I use VC ++ 2008, with C code files with the extension .c)

 Utils::fun(); // Opps. How I can access std::wstring in C? Utils2::fun(); 

Any suggestion?

+6
c ++ c
source share
11 answers
 // c_header.h #if defined(__cplusplus) extern "C" { #endif void Utils_func(); size_t Utils2_func(wchar_t* data, size_t size); #if defined(__cplusplus) } #endif //eof // c_impl.cpp // Beware, brain-compiled code ahead! void Utils_func() { Utils::func(); } size_t Utils2_func(wchar_t* data, size_t size) { std::wstring wstr = Utsls2::func(); if( wstr.size() >= size ) return wstr.size(); std::copy( wstr.begin(), wstr.end(), data ); data[wstr.size()] = 0; return str.size(); } //eof 
+7
source share

How about a wrapper

 extern "C" void Utilsfun(int i){Utils::fun(i);} 

Update:

Here's how you can call C ++ functions from C, but accessing std :: wstring from C is another matter.

If you really wanted to manipulate C ++ classes from C code, then you could create an API in which classes will work with C ++ functions, and are passed back to C using void pointers. I saw it, but it's not perfect

 extern "C" { void * ObjectCreate(){return (void *) new Object();} void ObjectOperate(void *object, char *parameter){((Object*)object)->Operate(parameter);} void ObjectDelete(void *object){delete ((Object*)object);} } 

You need to be very careful in managing the creation and deletion.

+5
source share

I think the only solution is to wrap them in global C-style functions in C ++ code, for example:

 extern "C" int Util2_Fun() { return Util2::Fun(); } 

I suppose you could also declare global function pointers as externs using some nasty variations:

 extern int (*Utils2_Fun)()=(int *())(Util2::Fun); 

And then call the function pointer directly from the C package using this pointer, but there is little to recommend this approach.

+1
source share

Create a wrapper function in C ++ code:

 extern "C" void Wrapper() { Utils2::fun(); } 

and then in your C code:

 extern void Wrapper(); int main() { Wrapper(); return 0; } 
+1
source share

You can make C ++ callable from C using the extern "C" construct.

0
source share

The most common solution is to write the C interface to your C ++ functions. This is C ++ code that is declared using extern "C" { ... } . These wrapper functions can call any C ++ code that they like, but since they are declared extern "C" , they will not be manipulated with names (you cannot create namespaces or overload here).

This should be related to your C file, and you're good to go.

That is, the header file contains

 #ifdef __cplusplus extern "C" { #endif void wrapper1(void); int wrapper2(int x); char* wrapper3(int y); #ifdef __cplusplus } #endif 

Ifdefs are needed to protect the C compiler from extern "C" . And you implement them in your C ++ source

 void wrapper1(void) { Util::funcOne(); } int wrapper2(int x) { return Util::funcTwo(x); } char* wrapper3(int y) { return Util::funcThree(y); } 
0
source share

If you do as ppl says (using extern "C"), beware that you only pass objects to the C function, which will compile in C.

0
source share

You will not have practical use for C ++ objects in your C code, so you probably want to create some kind of "C-binding" for your C ++ code, which consists of a number of regular functions called from C and return only regular C. data types. Then your wrapper functions can call all kinds of classes and objects, etc. But they provide a simpler C-Style interface for objects that you can use from C to bridge the gap. You can also use function pointers in some cases to give C access to static methods, but it's usually easiest to create a wrapper, IMHO.

0
source share

You can either write global external functions "C" or use function pointers to additionally create static class functions known to C. C ++ code can put these pointers into a global structure or pass them to C, calling the C function as a parameter. Alternatively, you can create a registry where C code can request function pointers from C ++ by specifying a string identifier. I use all of these varieties.

0
source share

If you have control over the entire source, I would not try to save part of it as C. It should be compiled as C ++ (or easily modified to do this). This does not mean that you need to rewrite it as C ++, just compile it as such. This way you can use any parts of C ++. Over time, C code will turn into more C ++, but this will happen slowly as needed.

Of course, if you want it to remain compiled in C for other reasons, this does not apply.

0
source share

C is a subset of C ++ .. Therefore, you cannot call members of a C ++ class and namespace in C.

-3
source share

All Articles