Tuesday, June 12, 2007

C++ note: mix with C code

The easiest way to make use of C modules is including the header files with

extern "C"
{
#include <a_c_module.h>
}

Then the C++ compiler knows that the header defines the structures in the c way, and will compile references in the C++ module in a compatible way.

For the standard libraries name "cblabla", extern "C" is already included in the header files. This is the reason why modules etc can be included directly.

For the linker, different vendors might have different standards to mangle the refernece names. Therefore, it needs to be confirmed that the C modules are compiled in a compatible way as the C++ module.

Making use of C++ modules from C modules is harder than the other way around. For the C++ programmers, it might be possible to turn some necessary C files into the C++ format and then compile the whole project as C++. Many files can be kept as the C modules and linked explicitly.

No comments: