Search code examples
c++boostcmakeboost-asioboost-python

CMake Boost linking problems


I'm using Boost::Python and Boost::Asio writing my sources and next write CMakeLists.txt to create my own shared library from sources like that (a part of file):

`find_package(Boost REQUIRED COMPONENTS python system thread regex)
if (Boost_FOUND)
    set(Boost_USE_STATIC_LIBS     OFF)
    set(Boost_USE_MULTITHREADED    ON)
    include_directories(${Boost_INCLUDE_DIRS})
    link_directories(${Boost_LIBRARY_DIRS})
endif()
find_package(PythonLibs 3 REQUIRED)
find_package(PythonInterp 3 REQUIRED)
if (PYTHONLIBS_FOUND)
    include_directories(${PYTHON_INCLUDE_DIRS})
    link_directories(${PYTHON_LIBRARIES})
endif()
add_library(my_lib SHARED ${MY_SOURCES})
set_target_properties(my_lib PROPERTIES PREFIX "" SUFFIX ".pyd")
target_link_libraries(my_lib ${Boost_LIBRARIES} ${PYTHON_LIBRARIES})`

(As Boost::Asio is header-only library I added just system thread regex since I've found out it depends on it). So CMake results look correct:

-- Boost version: 1.65.0 -- Found the following Boost libraries: -- python -- system -- thread -- regex -- chrono -- date_time -- atomic -- Configuring done -- Generating done

(But why is it searching for chrono etc.? Additional dependencies?) Well, when I'm running make my_lib, there are some linker errors like: In function PyInit_my_lib: undefined reference to boost::python::detail::init_module(PyModuleDef&, void (*)()) and In function boost::asio::detail::posix_thread::~posix_thread(): /usr/local/include/boost/asio/detail/impl/posix_thread.ipp:35: undefined reference to pthread_detach , so Boost wasn't linked properly. I've read a lot of docs and similar questions, but couldn't understand what am I doing wrong.

P.S. When I disabled -Wl,--no-undefined linker option, linking was successful, but undefined references are still there and I can't import module using python.


Solution

  • Finally, the solution was found by myself. The problem was indeed Boost::Python wasn't built properly. I don't know completely was it a bug or my own fault, but in my case just editing Boost Build's user-config.jam for using python3.5 wasn't enough: running build script resulted to libboost_python3.so, but internally python2.7 interpreter was used by it for reasons unknown to me.

    So, what I did is launched Boost initial bootstrapping as ./bootstrap.sh --with-python=/usr/bin/python3.5m, i.e. pointed the absolute path to required interpreter. After rebuilding Boost::Python, all symbols was resolved successfully.