链接时大量 std:: 函数的多重定义
我正在尝试将一些外部代码集成到我的应用程序中。我的代码是纯 C 代码,但新代码是 C++,因此我只需将 C 文件重命名为 .cc 并使用 g++ 编译整个文件。
它编译得很好,但我得到了一堆链接错误:
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 和其他函数。这可能是问题所在吗?
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您似乎有一些名为
new
和delete
的函数,这些函数是 C++ 中用于内存分配的保留关键字。尝试将它们重命名为其他名称(例如,svrt_new
、svrt_delete
)。此外,这一行实际上信息最丰富:
您必须以某种方式包含两个版本的 iostream 标头...
You seem to have some functions named
new
anddelete
, 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:
You must somehow include two versions of iostream headers...
这看起来有点像您没有包含防护的情况。但所有这些函数看起来都像 C++ 本机函数。您确定不包含一些 C++ 头文件吗?
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?
最后,我能够编译。我发现一些提示说我应该摆脱 C 风格的包含 (
#include
) 并将其替换为C++
风格的包含 (#include
)。这让事情变得更糟!将包含内容放回
.h
样式(并纠正外部代码中的不一致之处)使链接器感到满意。那,并删除所有未使用的代码并从外部代码中包含,解决了我的问题。谢谢你们的帮助!
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 withC++
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 !