gcc/ g++ Linking Issues

lrtaylor@micron.com lrtaylor@micron.com
Tue Jan 6 20:54:00 GMT 2004


Johnsy,

Do you have your include file in an 'extern "C"' block?  For example, in hello.cpp, you could have

extern "C" {
#include "helloworld.h"
}

Or in helloworld.h, you could check to see if the file is being included using a C++ compiler and adding the 'extern "C"' block in the header file something like this:

#ifdef __cplusplus
extern "C" {
#endif

<--helloworld.h internals-->

#ifdef __cplusplus
}
#endif

If you don't have something like this, your problem is probably that C++ "mangles" symbol names in order to be able to include the function signature in the name.  It's this feature that allows you to do function overloading - it generates a unique symbol name for each overloaded function, even though the functions have the same name.  The problem in this case, is that C doesn't do that, so when you compile your C++ program with the template to your C function, if you don't tell the compiler that the function should use the C calling convention (no mangled names) via the 'extern "C"' block, the compiler will mangle the name, and will not be able to find the symbol in the C library.

Does that make sense?

Cheers,
Lyle

-----Original Message-----
From: gcc-help-owner@gcc.gnu.org [mailto:gcc-help-owner@gcc.gnu.org]On
Behalf Of Johnsy George
Sent: Tuesday, January 06, 2004 12:32 PM
To: gcc-help@gcc.gnu.org
Subject: gcc/ g++ Linking Issues


Hi,
I'm having a problem while linking a dynamic library which was created using
gcc
 with a .cpp file using the g++ compiler.Each time I link I get an error of
undefined symbols.
Here is the command :
For creating dynamic library I use:
gcc helloworld.c -dynamic helloworld
#this creates a helloworld dynamic library.

Here's the command to link:

g++ hello.cpp  -lhelloworld
error : undefined symbol printhello
Here helloworld is a library in which a function called printhello() is
implemented which is
used by hello.cpp.
Please let me know if you have any suggesstions.

johnsy



More information about the Gcc-help mailing list