Multiple definition of a set of std :: functions when binding

I am trying to integrate some external code into my application. My code was pure C, but the new code was C ++, so I just renamed my C files to .cc and compiled it all with g ++.

It compiles fine, but I get the krypton of link errors:

CMakeFiles/svrt.dir/svrtH_generator.cc.o: In function `operator new(unsigned long, void*)': svrtH_generator.cc:(.text+0x0): multiple definition of `operator new(unsigned long, void*)' CMakeFiles/svrt.dir/svrt_generator.cc.o:svrt_generator.cc:(.text+0x0): first defined here CMakeFiles/svrt.dir/svrtH_generator.cc.o: In function `operator new[](unsigned long, void*)': svrtH_generator.cc:(.text+0x10): multiple definition of `operator new[](unsigned long, void*)' CMakeFiles/svrt.dir/svrt_generator.cc.o:svrt_generator.cc:(.text+0x10): first defined here CMakeFiles/svrt.dir/svrtH_generator.cc.o: In function `operator delete(void*, void*)': svrtH_generator.cc:(.text+0x20): multiple definition of `operator delete(void*, void*)' CMakeFiles/svrt.dir/svrt_generator.cc.o:svrt_generator.cc:(.text+0x20): first defined here CMakeFiles/svrt.dir/svrtH_generator.cc.o: In function `operator delete[](void*, void*)': svrtH_generator.cc:(.text+0x30): multiple definition of `operator delete[](void*, void*)' CMakeFiles/svrt.dir/svrt_generator.cc.o:svrt_generator.cc:(.text+0x30): first defined here [you got the idea...] svrtH_generator.cc:(.text+0x1060): multiple definition of `std::fixed(std::ios_base&)' CMakeFiles/svrt.dir/svrt_generator.cc.o:svrt_generator.cc:(.text+0xe80): first defined here collect2: ld returned 1 exit status make[3]: *** [dev/svrt/libsvrt.so] Error 1 make[2]: *** [dev/svrt/CMakeFiles/svrt.dir/all] Error 2 make[1]: *** [dev/svrt/CMakeFiles/svrt.dir/rule] Error 2 make: *** [svrt] Error 2 

I use Cmake to create things, but nothing complicated. I don’t know why I am getting all these errors, since my code is just a bunch of methods (I don’t use anything from the std package), and the code I'm trying to integrate is not much more complicated.

Please note that the warning is due to a link to my own code and not (yet) from the new C ++ code.

Is anyone

EDIT: after digging into the external code, I try to integrate, I found that some of them include:

 #include <iostream> #include <cmath> #include <fstream> #include <cfloat> #include <stdlib.h> #include <string.h> 

In addition, iostream is also included in other headers, and all of them include protective devices.

UPDATE: I managed to clear out the external code a bit and remove unnecessary dependencies. I still have some linker errors, but much less:

 CMakeFiles/svrt.dir/svrtH_generator.cc.o: In function `std::abs(long)': svrtH_generator.cc:(.text+0x0): multiple definition of `std::abs(long)' CMakeFiles/svrt.dir/svrt_generator.cc.o:svrt_generator.cc:(.text+0x0): first defined here CMakeFiles/svrt.dir/svrtH_generator.cc.o: In function `__gnu_cxx::abs(long long)': svrtH_generator.cc:(.text+0x20): multiple definition of `__gnu_cxx::abs(long long)' CMakeFiles/svrt.dir/svrt_generator.cc.o:svrt_generator.cc:(.text+0x20): first defined here CMakeFiles/svrt.dir/svrtH_generator.cc.o: In function `__gnu_cxx::div(long long, long long)': svrtH_generator.cc:(.text+0x40): multiple definition of `__gnu_cxx::div(long long, long long)' CMakeFiles/svrt.dir/svrt_generator.cc.o:svrt_generator.cc:(.text+0x40): first defined here CMakeFiles/svrt.dir/svrtH_generator.cc.o: In function `std::div(long, long)': svrtH_generator.cc:(.text+0x350): multiple definition of `std::div(long, long)' CMakeFiles/svrt.dir/svrt_generator.cc.o:svrt_generator.cc:(.text+0x150): first defined here 

The code includes both cmath and cstdlib , and references abs and other functions using the default namespace. Maybe this is a problem?

0
c ++ c linker linker-errors multiple-definition-error
Feb 05 '10 at 16:57
source share
3 answers

Finally, I was able to compile things. I found some hints that I need to get rid of the C-style ( #include <stdlib.h> ) and replace them with the C++ style includes ( #include <cstdlib> ). This exacerbated the situation!

Including the revert back to the .h style (and fixing inconsistencies with this in the external code) made the linker happy.

That, and removing all the unused code and including it from the internal code, I solved my problem. Thanks for the help guys!

0
Feb 08 '10 at 21:32
source share

It seems you have some functions named new and delete , and these are reserved keywords in C ++ for memory allocation. Try renaming them to something else ( svrt_new , svrt_delete for example).

Also this line is actually the most informative:

 svrtH_generator.cc:(.text+0x1060): multiple definition of `std::fixed(std::ios_base&)' 

You must somehow include two versions of the iostream headers ...

+3
Feb 05 '10 at 16:59
source share

It looks like a situation where you did not turn on the guards . But all these functions look suspiciously like C ++ native functions. Are you sure you are not including some C ++ headers?

0
Feb 05 '10 at 17:28
source share



All Articles