我正在嘗試將一些外部代碼集成到我的應用程序中。我的代碼是純C,但新代碼是C++,因此我只是將我的C文件重命名爲.cc,並用g ++編譯了整個代碼。鏈接時出現很多std ::函數的多重定義
它編譯罰款,但我得到鏈接錯誤的crapton:
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
我使用cmake構建的東西,但沒有什麼複雜的。我不知道爲什麼我會得到所有這些錯誤,因爲我的代碼只是一堆方法(我沒有使用std包中的任何東西),我試圖集成的代碼並不複雜。
請注意,警告來自鏈接我自己的代碼,而不是(從)新的C++代碼。
有人嗎?
編輯:在外部代碼中,我試圖整合後挖,我發現了一些包括:
#include <iostream>
#include <cmath>
#include <fstream>
#include <cfloat>
#include <stdlib.h>
#include <string.h>
另外,iostream都是包含在其它頭,也和所有的人都包括警衛。
更新:我設法清理了一下外部代碼並刪除了不必要的依賴關係。我還有一些鏈接錯誤,但要少得多:
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
代碼包括cmath
和cstdlib
,並指ABS和使用默認名稱空間等功能。這可能是問題嗎?
你的代碼是什麼樣的?如何包含任何C++頭文件? – jalf 2010-02-05 17:07:00