OK, i found the answer to this on another forum, which is that under
xcode any c functions have to have the following ifdef's in their .h
file as specified in the example below if you want them to link in c++
code.
So why is this different in xcode vs Metrowerks for c++ projects? Is
this just something different in the way the gcc compiler works vs the
Metrowerks compiler? Are there any other things like this i would need
to be aware of when moving Metrowerks c++ projects to xcode? Excuse my
xcode ignorance.
How to fix the problem:
if you have the function defined in texture.c then texture.h should
have:
#ifdef __cplusplus
extern "C" {
#endif
GLuint load_PNG_texture(char const *textureFile);
#ifdef __cplusplus
}
#endif
Dave Seaman - 30 May 2006 03:41 GMT
> OK, i found the answer to this on another forum, which is that under
> xcode any c functions have to have the following ifdef's in their .h
> file as specified in the example below if you want them to link in c++
> code.
> So why is this different in xcode vs Metrowerks for c++ projects? Is
> this just something different in the way the gcc compiler works vs the
> Metrowerks compiler? Are there any other things like this i would need
> to be aware of when moving Metrowerks c++ projects to xcode? Excuse my
> xcode ignorance.
> How to fix the problem:
> if you have the function defined in texture.c then texture.h should
> have:
> #ifdef __cplusplus
> extern "C" {
> #endif
> GLuint load_PNG_texture(char const *textureFile);
> #ifdef __cplusplus
> }
> #endif
I don't know about Metrowerks, but the the test for __cplusplus and the
extern "C" declaration are perfectly standard stuff and they apply to
every C/C++ compilation environment that I have used (mostly in
Unix/Linux environments using make).

Signature
Dave Seaman
U.S. Court of Appeals to review three issues
concerning case of Mumia Abu-Jamal.
<http://www.mumia2000.org/>
Michael - 31 May 2006 22:03 GMT
Just to make sure you understand what is going on, the gcc C++ compiler
mangles the names of functions to encode the parameter types. This is
done in part to support function overloading. Adding the parameter
types to the name allows the compiler to generate unique names for the
overloaded functions (eg, void foo(int x); -> foo_int and void
foo(float x); -> foo_float). When building, Xcode will by default use
the C compiler for files ending with .c, which doesn't mangle the
names, and the C++ compiler for your C++ files, which will mangle the
function names. This leads to the problem you experienced, the C
compiler generated an object file with "normal" function names, while
the C++ compiler generated an object file that expected the mangled
function name, which will fail to link since the names don't match. The
"extern "C"" stuff that you added just tells the C++ complier not to
mangle the names of those functions.
Metrowerks is probably compiling everything with its C++ complier, in
which case there are no issues with the name mangling, since everything
would be mangled, including your C functions. If you want to simulate
the behavior of Metrowerks, you can change Xcode's file mappings and
send all of your .c files through the C++ compiler instead of the C
compiler.
Michael
> OK, i found the answer to this on another forum, which is that under
> xcode any c functions have to have the following ifdef's in their .h
[quoted text clipped - 20 lines]
> }
> #endif