Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
General
GeneralPortable MacsHardwareNetworking
Applications
Mac ApplicationsEudoraFirefox / MozillaInternet ExplorerOutlook ExpressMS OfficeEntourageExcelPowerPointWordVirtual PCMedia PlayerOther MS Products
Programming
Mac ProgrammingCodeWarriorPerl
Country Specific
Australian Mac GroupUK Mac Group

Mac Forum / Programming / Mac Programming / May 2006



Tip: Looking for answers? Try searching our database.

undefined symbols in xcode

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
johnd - 30 May 2006 01:50 GMT
I'm getting an 'Undefined symbols' link error in xcode and i'm trying
to figure out why this is the case for this particular project.  The
specific functions listed as undefined in the Xcode Build Results
window are included in the project in an included c file.  They are
called in 2 different code files in the project, a .c and a .cpp file.
If the project just includes the .c file it links.  When the .cpp file
is included i get the undefined symbols error.  Does anyone have any
ideas why this would be the case?  Obviously the functions are there
since the project links when just the .c file that references the
functions is included in the project.
johnd - 30 May 2006 03:03 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
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
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.