1

I am having trouble linking python 3 in cmake.

The options to link in the shell are $(/usr/bin/python3-config --ldflags) and that works completely fine.

But when I move to cmake, I use:

set(Python_ADDITIONAL_VERSIONS 3.5)
find_package(PythonLibs REQUIRED)
include_directories(${PYTHON_INCLUDE_DIRS})
target_link_libraries(sim ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries(sim ${CMAKE_DL_LIBS})
target_link_libraries(sim ${PYTHON_LIBRARIES})

giving me the error:

/usr/bin/ld: /usr/local/lib/libpython3.5m.a(dynload_shlib.o): undefined reference to symbol 'dlsym@@GLIBC_2.2.5'
/usr/lib/x86_64-linux-gnu/libdl.so: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status

I then tried

target_link_libraries(sim "$(/usr/bin/python3-config --ldflags)")

but this also does not work with error message

c++: error: $(/usr/bin/python3-config: No such file or directory
c++: error: unrecognized command line option ‘--ldflags)’

The only method that works for me is to open the subshell by

echo $(/usr/bin/python3-config --ldflags)

-L/usr/lib/python3.5/config-3.5m-x86_64-linux-gnu -L/usr/lib -lpython3.5m -lpthread -ldl -lutil -lm -Xlinker -export-dynamic -Wl,-O1 -Wl,-Bsymbolic-functions

and write the content to cmake as

target_link_libraries(sim "-L/usr/lib/python3.5/config-3.5m-x86_64-linux-gnu -L/usr/lib -lpython3.5m -ldl -lutil -lm -Xlinker -export-dynamic -Wl,-O1 -Wl,-Bsymbolic-functions")

This works but the method is nasty. Is there any clean way to perform such linking?

Troubadour
  • 13,334
  • 2
  • 38
  • 57
ar2015
  • 5,558
  • 8
  • 53
  • 110

0 Answers0