I added the line extern "C" void perlinTest(void); to a C++ header along with the include of the c header file hoping that was all I needed but the compiler complains:
Undefined symbols for architecture i386:
"perlinTest()", referenced from:
CreateRenderer3(IResourceManager*) in Renderer.o
Your C++ code needs to be aware that the function is a C function. To do so, you need to declare it this way:
A realistic example for your situation would be:
The reason for this is that C++ function names are mangled to something that tells about the types of the parameters. At the lowest level, this is what allows overloading: it never really is legal to have two visible symbols that share the same name, so C++ allows them by embedding markers that indicate the types of the parameters in the function names. For instance,
void perlinTest()gets mangled as_Z10perlinTestvon my Lion box withg++(and probablyclang++), though this is ABI-specific and will not necessarily be the same on other platforms.However, C doesn’t support overloading, and functions aren’t subject to name mangling, so when your C++ code tries to call one, it needs to know that it must not use a mangled name. This is what
extern "C"tells the compiler.If your header files need to be readable from both C and C++, the common practice is to wrap them in an
extern "C"block (extern "C" { /* declarations */ }) itself wrapped in an#ifdef __cpluspluspreprocessor directive (so the C code doesn’t see theextern "C"code).