我想在 Python 中使用 C++ 函数。因此,我决定尝试使用 SWIG 来实现此目的。 首先,我运行命令:
swig -python test.i
然后用g++-6.2编译如下:
g++-6.2 -c test.cpp test_wrap.c -fPIC -I /usr/include/python3.6m
到目前为止,一切正常,但在必须创建链接的最后一步时出现问题。我运行命令:
ld -shared test.o test_wrap.o -o _test.so
按照 Swig tutorial 中的建议,但我收到以下错误:
test.o: In function `__static_initialization_and_destruction_0(int, int)':
test.cpp:(.text+0x66): undefined reference to `__dso_handle'
ld: test.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
有人知道如何解决这个问题吗?
最佳答案
您没有与 ld
命令链接。您链接到与编译时相同的 $(CC)
或 $(CXX)
(gcc
或 g++
) ,使用适合链接的选项。它将使用应生成的 ABI、目标格式等的正确命令行选项来驱动后端链接器 ld
。
这样做,它应该可以正常工作:
g++-6.2 -shared test.o test_wrap.o -o _test.so
关于python - 制作共享对象时,无法使用针对未定义隐藏符号 `__dso_handle' 的重定位 R_X86_64_PC32,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57957080/