0
votes

I am trying to use cmake to build a simple parser project. I used boost::program_options in my code, but it seems cmake does not look up the boost lib directory. Get confused and frustrated..

My CMakeLists.txt is

# basic info
CMAKE_MINIMUM_REQUIRED(VERSION 3.1.0)
PROJECT(parser CXX)
SET(CMAKE_CXX_STANDARD 14)

# Boost
FIND_PACKAGE(Boost 1.61.0 REQUIRED PATHS /path/to/boost NO_DEFAULT_PATH)
MESSAGE(STATUS "Boost version: ${Boost_VERSION}" )
MESSAGE(STATUS "Boost include dirs: ${Boost_INCLUDE_DIRS}" )
MESSAGE(STATUS "Boost library dirs: ${Boost_LIBRARY_DIRS}" )
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS})
LINK_DIRECTORIES(${Boost_LIBRARY_DIRS})

# main
FILE(GLOB main_SRC *.cpp)
ADD_EXECUTABLE(main ${main_SRC})
TARGET_LINK_LIBRARIES(main boost_program_options)

I use a modified BoostConfig.cmake (which points to my own Boost library)

The result for running cd build; cmake .. is

-- Boost version: 1.61.0
-- Boost include dirs: /path/to/boost/include
-- Boost library dirs: /path/to/boost/lib
-- Configuring done
-- Generating done
-- Build files have been written to: /path/to/build

Thus I believe cmake has found the my Boost library. But then if I run make I will end up with a bunch of errors like

undefined reference to `boost::program_options ... `

If I run make VERBOSE=1 I will see

/path/to/g++ -rdynamic CMakeFiles/main.dir/main.cpp.o -o main -lboost_program_options

the command does not have -L or -Wl,rpath for ${Boost_LIBRARY_DIRS}. If I add the flag manually then I can compile the project successfully.

I also tried linking to static lib by TARGET_LINK_LIBRARIES(main ${Boost_LIBRARY_DIR}/libboost_program_options.a) instead of LINK_DIRECTORIES(), but the same error was thrown.

Not sure what makes things wrong.. Thanks in advance

1
FYI I use cmake 3.8.0 and g++ 7.1.0 ..Gaohan Miao
With CMake you should not link libraries explicitly, but use module-provided targets instead. See also: CMake-FindBoostIvan Aksamentov - Drop

1 Answers

0
votes

What happens with the standard method using the imported targets? That is,

# Use and set variable/-Dflag/environment for custom Boost location
set(BOOST_ROOT /path/to/boost)
find_package(Boost 1.61.0 REQUIRED
  COMPONENTS program_options)
add_executable(main ...)
target_link_libraries(main Boost::program_options)