我正在处理通过 pybind11 从 Ubuntu 20.04 上的 Python (v3.8) 应用程序调用的 C++ 代码。
运行应用程序出现错误
Traceback (most recent call last):
snip...
ImportError: <full-path>/<lib-name-A>.so: undefined symbol: <mangled symbol name>
lib-name-A 输出上的 ldd 给出了定义符号的 lib B 的绝对路径。库 B 上的 nm -s --demangle 显示符号在文本部分中定义。为了确定起见,我比较了损坏的名称,lib B 中的名称与错误消息中给出的名称相同。因此,鉴于库存在于 ldd 显示其预期的位置,并且在其中定义了符号,为什么我会收到错误消息?
最佳答案
nm -s --demangle` on lib B shows the symbol is defined in the text section
在 lib B 中定义的函数是必要的,但还不够。该函数还必须从库 B 中导出。
机会是,它不是。要验证,请运行:
nm -D /path/to/lib-B.so | grep $mangled_symbol_name_here
如果我的猜测是正确的(函数未导出),您将不会从上述命令中获得任何输出。更新:
Checking with
nm -D
, and the symbol is missing. Checking again with-s
, the symbol hast
(lowercase) next to it, rather thanT
好的,所以我的猜测是正确的。
I've added
__attribute__((visibility("default")))
in front of the function declaration,
这才是正确的解决方案。
Question is why I need to declare it explicitly as visible.
很可能是因为您使用
-fvisibility=hidden
构建此代码,其中说“隐藏未明确声明为可见的所有内容”。与
-fvisibility=hidden
建立联系是当前的最佳实践,但您确实需要注意在您正在构建的共享库之外使用了哪些符号。
关于c++ - 未找到来自共享库的符号,但已定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69514978/