Albert
Albert

Reputation: 68260

How to link Python C module with `ld`. undefined reference to `__dso_handle'

My current command:

c++ -fPIC -c algo_cython.cpp
ld -shared algo_cython.o -L/usr/lib/gcc/x86_64-linux-gnu/4.7 -lc -lstdc++   -o algo_cython.so

And the error:

algo_cython.o: In function `__static_initialization_and_destruction_0(int, int)':
algo_cython.cpp:(.text+0x83e4): undefined reference to `__dso_handle'
ld: algo_cython.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

Upvotes: 3

Views: 2530

Answers (1)

Anya Shenanigans
Anya Shenanigans

Reputation: 94829

compile algo_cython.cpp using the option -fPIC - you can't compile a shared object in 64bit on intel without this flag, so the line for compiling should read:

c++ -fPIC -c algo_cython.cpp

Additionally, I'd actually use the compiler-driver to produce the shared object, rather than directly calling ld i.e. you can use:

c++ -shared algo_cython.o -L/usr/lib/gcc/x86_64-linux-gnu/4.7 -lc -lstdc++   -o algo_cython.so

Unless you really want to do something that can't be driven from the compiler driver, directly invoking ld is not what you want to do.

Upvotes: 2

Related Questions