问题
I am trying to integrate some external code into my application. My code was pure C, but the new code is C++, so I simply renamed my C files to .cc and compiled the whole thing with g++.
It compiles fine, but I get a crapton 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'm using Cmake to build the thing, but nothing really complicated. I don't know why I get all these errors, as my code is just a bunch of methods (I don't use anything from the std package) and the code I try to integrate is not much more complicated.
Note that the warning comes from linking my own code, and not (yet) from the new C++ code.
Anybody ?
EDIT: after digging in the external code I try to integrate, I found some includes :
#include <iostream>
#include <cmath>
#include <fstream>
#include <cfloat>
#include <stdlib.h>
#include <string.h>
Also, iostream is included in other headers, too, and all of them have include guards.
UPDATE: I managed to clean a bit the external code and remove the 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 refers to abs and other functions using a default namespace. May this be the problem ?
回答1:
You seem to 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 most informative:
svrtH_generator.cc:(.text+0x1060): multiple definition of `std::fixed(std::ios_base&)'
You must somehow include two versions of iostream headers...
回答2:
This looks kind of like a situation where you did not do include guards. But all those functions look suspiciously like C++ native functions. Are you sure you are not including some C++ headers?
回答3:
Finally, I was able to make things compile. I found some hints saying that I should get rid of C-style includes (#include <stdlib.h>
) and replace them with C++
style includes (#include <cstdlib>
). This made things worse !
Putting the includes back to .h
style (and correcting the inconsistencies about that in the exernal code) made the linker happy.
That, and removing all unused code and includes from the xternal code, solved my problem. Thanks for your help guys !
来源:https://stackoverflow.com/questions/2208834/multiple-definition-of-lots-of-std-functions-when-linking