Reputation: 1085
I am trying to link my code to boost, python and armadillo. Here is the CMakeLists.txt I use
cmake_minimum_required(VERSION 3.3)
set(EXEC_NAME phases)
set(LIBS_NAME Hamilton)
project(${EXEC_NAME})
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Release")
endif()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(LIB_HEADER point.h
hamilton.h)
set(LIB_SOURCE point.cpp
hamilton.cpp)
add_library(${LIBS_NAME} SHARED ${LIB_SOURCE} ${LIB_HEADER})
add_library("${LIBS_NAME}_mod" MODULE ${LIB_SOURCE} ${LIB_HEADER})
set(HEADER_FILES point.h hamilton.h)
set(SOURCE_FILES main.cpp)
add_executable(${EXEC_NAME} ${SOURCE_FILES} ${HEADER_FILES})
target_link_libraries(${EXEC_NAME} ${LIBS_NAME})
find_package(Boost COMPONENTS python REQUIRED)
include_directories(${Boost_INCLUDE_DIRS})
target_link_libraries(${EXEC_NAME} ${Boost_LIBRARIES})
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -lboost_python")
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -lboost_python")
find_package(Armadillo REQUIRED)
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -larmadillo")
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -larmadillo")
include_directories(${ARMADILLO_INCLUDE_DIRS})
target_link_libraries(${EXEC_NAME} ${ARMADILLO_LIBRARIES})
find_package(PythonLibs REQUIRED)
include_directories(${PYTHON_INCLUDE_DIRS})
target_link_libraries(${EXEC_NAME} ${PYTHON_LIBRARIES})
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -lpython")
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -lpython")
message(STATUS "Building in ${CMAKE_BUILD_TYPE} ...")
As you can see I am adding the flag -l...
after each library. If I remove them then I get a linker error.
Is there any nice way to avoid this?
I use intel compiler icc but also tries clang and gcc
clang doesn't need the extra -l...
flags.
NOTE
Only using target_link_libraries
without the CMAKE_xxx_flags
gives me a linker error. That is why I am asking this question.
I have cmake 3.3, boost 1.59 with boost-python and armadillo installed using homebrew
Update
The error I get is
Reaping winning child 0x7fa188492f80 PID 47863
Live child 0x7fa188492f80 (libHamilton.dylib) PID 47864
Undefined symbols for architecture x86_64:
"__ZN5boost6python15instance_holder10deallocateEP7_objectPv", referenced from:
__ZN5boost6python7objects11make_holderILi3EE5applyINS1_12value_holderI8Hami ltonEENS_3mpl7vector3IKdKmSB_EEE7executeEP7_objectdmm in hamilton.cpp.o
which is from linking to boost libraries.
Outputting the content of Boost_LIBRARIES
gives me
/usr/local/lib/libboost_python-mt.dylib
on the other hand Boost_python_LIBRARY
returns empty string
Upvotes: 0
Views: 357
Reputation: 44258
As stated in FindBoost doc
target_link_libraries(${EXEC_NAME} ${Boost_LIBRARIES})
need to be used, remove
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -lboost_python")
etc
Check content of Boost_LIBRARIES
variable:
message( "Boost_LIBRARIES: ${Boost_LIBRARIES}" )
You may try to use Boost_python_LIBRARY
variable instead, also Boost_python_LIBRARY_DEBUG
or Boost_python_LIBRARY_RELEASE
may work, in this case most probably your boost installation is somehow broken or at least not the way as expected by cmake module.
Update: your error shows that you have issue linking with multythread or shared version of boost_python. You can fix it by specifying:
set( Boost_USE_MULTITHREADED OFF )
or
set( Boost_USE_STATIC_LIBS ON )
before find_package
or find the cause of that linking error.
Upvotes: 2