我在 ubuntu 20.04 中使用嵌入式 Python (3.9) 并尝试导入产生错误 _ctypes.cpython-39-x86_64-linux-gnu.so: undefined symbol: PyFloat_Type
的 ctypes。
我正在编译一个共享对象,它是使用 dlopen()
动态加载的。
CMake
用于构建共享对象。我这样声明 Python3 依赖项: find_package(Python3 REQUIRED COMPONENTS Development Development.Embed)
并使用 target_link_libraries(${target_name} Boost::filesystem Python3::Python)
链接
如果我理解正确,这会告诉 CMake 直接与 libpython3.9.so
链接(我也尝试明确声明链接到 libpython3.9.so
,但这并没有解决问题)。我确实看到 libpython3.9.so
导出 PyFloat_Type
而 _ctypes.cpython-39-x86_64-linux-gnu.so
没有。
导入由 PyRun_SimpleString()
函数简单地完成: PyRun_SimpleString("import ctypes")
。
我应该说我在网上看到了一些解决方案,但没有一个有效(比如导出 LD_FLAGS="-rdynamic"
,但也没有帮助)。
我还应该指出,使用解释器(python3.9)导入效果很好。
这是 CMake 生成的构建命令: /usr/bin/c++ -fPIC -g -Xlinker -export-dynamic -shared -Wl,-soname,mytest.python3.so -o mytest.python3.so CMakeFiles/mytest.python3.dir/[mydir]/[myobjects].o /usr/lib/x86_64-linux-gnu/libboost_filesystem.so.1.71.0 /usr/lib/x86_64-linux-gnu/libpython3.9.so /usr/lib/x86_64-linux-gnu/libpython3.9.so
提前感谢您的帮助!