static vs extern “C”/”C++”

Yes, you are just lucky 🙂 The extern “C” is one language linkage for the C language that every C++ compiler has to support, beside extern “C++” which is the default. Compilers may supports other language linkages. GCC for example supports extern “Java” which allows interfacing with java code (though that’s quite cumbersome). extern “C” … Read more

Call a C function from C++ code

Compile the C code like this: gcc -c -o somecode.o somecode.c Then the C++ code like this: g++ -c -o othercode.o othercode.cpp Then link them together, with the C++ linker: g++ -o yourprogram somecode.o othercode.o You also have to tell the C++ compiler a C header is coming when you include the declaration for the … Read more

How to call C++ function from C?

You need to create a C API for exposing the functionality of your C++ code. Basically, you will need to write C++ code that is declared extern “C” and that has a pure C API (not using classes, for example) that wraps the C++ library. Then you use the pure C wrapper library that you’ve … Read more