<分区>
ld
行为怪异,我想了解发生了什么。在 mycode.cpp
,我有以下内容:
#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++)