views:

386

answers:

3

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 ?

+3  A: 

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...

Kornel Kisielewicz
Actually I don't... all my functions are prefixed similarly to what you propose.
Wookai
A: 

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?

xzqx
I did the include guards. And the only headers I include are mine or from the new code. I'll look deeper in the new code to see if they inclde a C++ header.
Wookai
A: 

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 !

Wookai
Well, you should not use the .h versions. They are not standard; the standard method of including C headers is `<cheader_name>`. So *use* `<cstdlib>` and instead of saying "it made it worse", find out way to make your overall situation *better*.
GMan
I know ! I don't know why the author of the external code did not use the standard headers, but I spent already too much time cleaning/fixing his code. Now that it works, I have better things to do...
Wookai