Gcc: Linking the C library in a C ++ application leads to a "multiple definition" of errors

I have a working C library that I want to associate with a C ++ application using gcc, but the linker (g ++) gives me a "multiple definition" error. Using the C application and gcc it works. The headers defining the interface contain:

#ifdef __cplusplus 
extern "C" {
#endif

I checked the library with the "nm" command, and it has several method definitions (this method is not from the public interface).

My questions:

  • Why does my library have several definitions (some have T and others have U)?

  • Why does this work if the application, including the file, is a C application (I use -Wall to build)?

  • Do I need any special attribute or use a specific file extension for it to work, or if I need to return to the programming school :)?

Paying more attention to the lib.a file, I 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 think this is a problem?

Any help really appreciated.

+3
source share
2 answers

My wild guess is that "#define BLAHBLAH_H " and "#ifndef BLAHBLAH_H / # endif" are set outside of "extern" C "{} '.

+1
source

, ( ) -whole-archive C. -no-whole-archive .

   gcc -Wl,**--whole-archive** -l:otherlibs *-Llibpath -l:libname* Wl,**--no-whole-archive** -o myApp hello.c

   gcc -Wl,**--whole-archive** -l:otherlibs Wl,**--no-whole-archive** *-Llibpath -l:libname* -o myApp hello.c

, , / .

+1

All Articles