ld
is acting weirdly and I want to understand what's going on. In a mycode.cpp
, I have the following:
#include <algorithm>
#include "mycode.hpp"
// awesome stuff here
I compile it with g++ -fPIC -c mycode.cpp
and link it with ld -Bshareable -o libmylib.so mycode.o
. Works like a charm.
Then I want to call cout
in mycode.cpp
. Actually, even before adding this cout
, if I just add #include <iostream>
in the code above, while linking I get the error
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
If I link it with g++ -shared
, it works, but that's not the point. I do not understand what's wrong here, and I am looking for insights.
EDIT: I understand one must call g++
instead of ld
directly. My problem is, I want to understand what's under the hood: why including iostream break things while algorithm is already here (so ld
knows stdc++)