gcc: Linking C library in a C++ app results in "multiple definition of" errors
I have a working C library which I want to link to a C++ application using gcc but the linker (g++) is giving me the "multiple definition" error. With a C application and gcc it works. The headers defining the interface all contain the:
#ifdef __cplusplus
extern "C" {
#endif
I checked the library using the "nm" command and it does have multiple definitions of the me开发者_开发知识库thod (the method in question is not from the public interface).
My questions are:
Why does my library have multiple definitions (some have the T while others have U)?
Why it works if the application including the file is a C application (I'm using -Wall to build)?
Do I need any special attribute or use a specific file extension to make it work or is the case that I need to go back to programming school :) ?
Paying more attention to the lib.a file I can see that one of the objects is included twice. For example, I have two sections for the same object:
obj1.o
00000000 T Method
obj2.o
00000000 T Hello
obj1.o
00000000 T Method
I guess this is the problem?
Any help is really appreciated.
My wild guess is that the "#define BLAHBLAH_H" and "#ifndef BLAHBLAH_H / #endif" set outside the 'extern "C"{}' thing.
after playing around I found that actually the whole command line (it's kind of a complex application with an automated compilation and linkage) contained the --whole-archive parameter before the inclusion of the C library. Moving the library after the --no-whole-archive fixed the problem.
Original command
gcc -Wl,**--whole-archive** -l:otherlibs *-Llibpath -l:libname* Wl,**--no-whole-archive** -o myApp hello.c
Fixed command
gcc -Wl,**--whole-archive** -l:otherlibs Wl,**--no-whole-archive** *-Llibpath -l:libname* -o myApp hello.c
Thank you for everyone's help guys and sorry if I didn't provide enough/accurate information.
Best Regards
精彩评论