Python import got SegFault with shared object built from Boost Python

Viewed 171

I am trying to wrap a C++ library into a Python module with Boost Python.
Everything is OK when I used Python 2.7, but I got segmentation fault when upgraded to 3.6.

In my CMakeLists.txt, I wrote

find_package(Pythonlibs 3.6 REQUIRED)
find_package(Boost COMPONENTS python REQUIRED)
message(STATUS ${Boost_INCLUDE_DIR})
message(STATUS ${Boost_LIBRARIES})
message(STATUS ${PYTHON_INCLUDE_DIR})
message(STATUS ${PYTHON_LIBRARIES})

and cmake returned

-- Boost version: 1.65.1
-- Found the following Boost libraries:
--   python
-- /usr/include
-- /usr/lib/x86_64-linux-gnu/libboost_python.so
-- /usr/include/python3.6m
-- /usr/lib/x86_64-linux-gnu/libpython3.6m.so

and make output the shared object which is I want to use in Python, say xx.lib.
I opened a Python shell and typed

>>> import xx

it just printed Segmentation fault (core dumped) and existed.
After I added a trace function in Python, I found that the segfault was caused by an undefined symbol

ImportError: ./xx.so: undefined symbol: _ZN5boost6python6detail11init_moduleER11PyModuleDefPFvvE

Then I used idd to lookup the dependencies of the shared object, it showed (partially)

linux-vdso.so.1 (0x00007ffe636c5000)
libboost_python-py27.so.1.65.1 => /usr/lib/x86_64-linux-gnu/libboost_python-py27.so.1.65.1 (0x00007f801af9b000)
libpython3.6m.so.1.0 => /usr/lib/x86_64-linux-gnu/libpython3.6m.so.1.0 (0x00007f801a8f0000)

As second lines showed, it linked to Boost Python 2.7 libraries, which I thought it was the reason I got segfault, but libboost_python-py36.so is truly existed

$ ls /usr/lib/x86_64-linux-gnu/ | grep libboost_python-py
libboost_python-py27.a
libboost_python-py27.so
libboost_python-py27.so.1.65.1
libboost_python-py36.a
libboost_python-py36.so

Why it linked to a wrong library? Thanks a lot!

[Update]
OS: Ubuntu 18.04
Boost was installed when installing ROS melodic

1 Answers

The problem is solved after adding Python version in find_package

find_package(Boost COMPONENTS python3 REQUIRED)
Related