【问题标题】:How to link Python C module with `ld`. undefined reference to `__dso_handle'如何将 Python C 模块与 `ld` 链接。对“__dso_handle”的未定义引用
【发布时间】:2013-04-22 09:53:28
【问题描述】:

我当前的命令:

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

还有错误:

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

【问题讨论】:

  • 你能显示用于生成 algo_cython.o 的编译命令吗?

标签: python linux ld


【解决方案1】:

使用选项 -fPIC 编译 algo_cython.cpp - 如果没有此标志,您将无法在 intel 上编译 64 位共享对象,因此编译行应为:

c++ -fPIC -c algo_cython.cpp

此外,我实际上会使用编译器驱动程序来生成共享对象,而不是直接调用ld,即您可以使用:

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

除非你真的想做一些编译器驱动无法驱动的事情,否则直接调用ld不是你想做的。

【讨论】:

  • 对不起,我错过了,我已经有-fPIC。为什么我不能直接拨打ld?直接调用它会是什么样子?
  • 另外,它仍然不起作用。它不会在链接时引发错误,但是当我尝试导入该 Python 模块时,我得到undefined symbol: init_csage_module
  • 好的,最后一个错误是 Sage 特定的。 (您必须链接到 libcsage.so。)
  • 重新评论第一条评论:因为它通常会遗漏一些您需要链接才能使其工作的东西(例如核心库)。关于第二条评论:你的模块依赖于 sagelib?
  • 肯定漏掉了什么。这就是为什么我在这里询问究竟缺少什么。不完全取决于,但我使用它的标题,因为它在 Sage 中使用。
猜你喜欢
  • 1970-01-01
  • 2011-10-03
  • 2012-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-18
相关资源
最近更新 更多