0

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.

Uroboros
  • 189
  • 2
  • 12
  • First you need to set `set(Boost_USE_STATIC_LIBS OFF) set(Boost_USE_MULTITHREADED ON)` before your find_package call. And second you should switch the order of the libraries in your target_link_libraries call: Phython libraries go first, then Boost libraries. – vre Sep 15 '17 at 13:57
  • @vre done, but didn't help :( – Uroboros Sep 15 '17 at 14:04
  • Did you deleted the `CMakeCache.txt` after applying the changes? Sometimes it is necessary, otherwise your changes won't apply. – vre Sep 15 '17 at 14:08
  • @vre yep, nothing changed – Uroboros Sep 15 '17 at 14:20
  • And you might need to specify `set(Boost_ADDITIONAL_VERSIONS "1.65" "1.65.0")` before calling find_package. – vre Sep 15 '17 at 14:21
  • @vre unfortunately, didn't help – Uroboros Sep 15 '17 at 14:31
  • `FIND_PACKAGE (Threads REQUIRED)` is probably needed. Also I don't think `if (Boost_FOUND)` and `if (PYTHONLIBS_FOUND)` is needed as there isn't any alternative provided. I'd do some troubleshooting, particularly checking what's being linked: `MESSAGE(STATUS "${Boost_LIBRARIES} ${PYTHON_LIBRARIES}")`. – doqtor Sep 15 '17 at 19:04
  • @doqtor MESSAGE output: `/usr/local/lib/libboost_python.so;/usr/local/lib/libboost_system.so;/usr/local/lib/libboost_thread.so;/usr/local/lib/libboost_regex.so;/usr/local/lib/libboost_chrono.so;/usr/local/lib/libboost_date_time.so;/usr/local/lib/libboost_atomic.so /usr/lib/x86_64-linux-gnu/libpython3.5m.so` – Uroboros Sep 15 '17 at 19:26
  • @doqtor Added `FIND_PACKAGE (Threads REQUIRED)`; linker error persists. Should I add something like `target_link_library(..., ${Threads_LIBRARIES})`? Seems there are no such thing – Uroboros Sep 15 '17 at 19:31
  • @doqtor After adding `target_link_libraries(my_lib ${CMAKE_THREAD_LIBS_INIT})` (it's -lpthread flag), there are only one linker error: first PyInit undefined reference mentioned in question. Well, need to link something python related, but what? – Uroboros Sep 15 '17 at 19:58
  • Is your boost python built with python3.5? See [this](https://stackoverflow.com/a/45767023/4899330) – doqtor Sep 15 '17 at 20:03
  • @doqtor I rebuilt boost::python with python3.5 according to the link you've provided. So now I'm searching libs with `find_package(Boost REQUIRED COMPONENTS python3)` as `find_package(Boost REQUIRED COMPONENTS python)` couldn't find `libboost_python.so`, there is `libboost_python3.so` now. But the problem still persists... – Uroboros Sep 15 '17 at 21:10

1 Answers1

1

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.

Uroboros
  • 189
  • 2
  • 12