0
ld
行为异常,我想了解发生了什么。在mycode.cpp
,我有以下几点:无法链接包含iostream的动态库
#include <algorithm>
#include "mycode.hpp"
// awesome stuff here
我g++ -fPIC -c mycode.cpp
编译它,并用ld -Bshareable -o libmylib.so mycode.o
联系起来。奇迹般有效。
然后我想打电话给cout
mycode.cpp
。其实,即使添加此cout
,如果我只是在上面的代码中添加#include <iostream>
,而连接之前,我得到的错误
mycode.o: In function `__static_initialization_and_destruction_0(int, int)':
mycode.cpp:(.text+0x50): undefined reference to `__dso_handle'
ld: mycode.o: relocation R_X86_64_PC32 against undefined hidden symbol `__dso_handle' can not be used when making a shared object
ld: final link failed: Bad value
如果我g++ -shared
链接它,它的工作原理,但是这不是问题的关键。我不明白这里有什么问题,我正在寻找见解。
编辑:我知道一个人必须直接调用g++
而不是ld
。我的问题是,我想了解引擎盖下是什么:为什么包括iostream的破东西,同时算法已经在这里(所以ld
知道STDC++)
相当肯定你的答案是正确的:[只使用g ++工程,但不是“g ++ -c”和ld](http://stackoverflow.com/questions/6704780/using-only-g-works-but- not-gc-and-ld) –
请参阅http://www.linuxquestions.org/questions/programming-9/fyi-shared-libs-and-iostream-c-331113/page2.html#post2936978,但要考虑到它是一个非常旧的版本gcc – Leon
不要直接调用ld。与GCC链接。 – rubenvb