Make sure that all C++ functions that are called by C functions are declared as extern "C". For example:
extern "C" void my_cpp_func();
This makes sure that your functions are exported by the C++ in an old-school fashion. This can be very handy for example when you make a DLL that interfaces with other (not only C) languages and you don't want your DLL to export nasty mangled C++ symbol names.
In the header file of the C++ .cpp that defines the called function you have to do the following trick to
make the header valid when included from both C++ and C:
#ifdef __cplusplus<br />
extern "C" {<br />
#endif<br />
<br />
<br />
void my_cpp_func();<br />
<br />
<br />
#ifdef __cplusplus<br />
}<br />
#endif<br />
Note that you should not use C++ elements in the declaration of these functions like C++ classes and bool data type. If you want to pass complex data than pass around plain C struct pointers.
More detailed answer:
- Create a console C++ project in visual studio.
- Add x.cpp and implement there your function.
- Add x.h and declare there your C++ function as I described above. Don't forget to include this to your x.cpp because the compiler has to know that in the object file of the .cpp file it has to generate C like names (extern "C")!
- Add y.c, and include the x.h header. Inside y.c you can call the C++ function from your C functions.