I am using embedded Python (3.9) in ubuntu 20.04 and trying to import ctypes which produces the error _ctypes.cpython-39-x86_64-linux-gnu.so: undefined symbol: PyFloat_Type
.
I am compiling a shared object, which is loaded dynamically using dlopen()
.
CMake
is used to build the shared object. I am stating Python3 dependency like so:
find_package(Python3 REQUIRED COMPONENTS Development Development.Embed)
and link using target_link_libraries(${target_name} Boost::filesystem Python3::Python)
If I understand correctly, this tells CMake to link directly with libpython3.9.so
(I also tried to explicitly state linking to libpython3.9.so
, but that did not solve the issue).
I do see that libpython3.9.so
exports PyFloat_Type
and that _ctypes.cpython-39-x86_64-linux-gnu.so
does not.
The import is simply done by the PyRun_SimpleString()
function: PyRun_SimpleString("import ctypes")
.
I should state that I have seen on the web some solutions, but none of them worked (like exporting LD_FLAGS="-rdynamic"
, but does also did not help).
I should also point out that importing using the interpreter (python3.9) works well.
Here is the build command generated by 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
Thanks for any help in advance!