DA--
DA--

Reputation: 765

CMake linking Boost: cannot find -lboost_program_options

I want to use Boosts support for command line flags in C++ on Linux. I use CMake to build the application, but I get a error "cannot find -lboost_program_options". The library boost_program_options was build independently using bjam, and the libraries are in the stage/lib subdirectory of the boost directory.

What does work: A solution is to add the stage/lib library using link_directories, but the CMake manual states:

Note that this command is rarely necessary. Library locations returned by find_package() and find_library() are absolute paths.

So that should not be nescessary.

What I want to have working:

Using find_package should be enough, but that doesn't work, here's the CMakeLists:

cmake_minimum_required(VERSION 3.6)
project(inp_manipulation)
set(CMAKE_CXX_STANDARD 11)
include_directories(includes lib/boost_1_62_0 lib/)
SET(CMAKE_INCLUDE_PATH ${CMAKE_INCLUDE_PATH} "lib/boost_1_62_0")
SET(CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH} "lib/boost_1_62_0/stage/lib")

find_package(Boost 1.62.0)
include_directories(${Boost_INCLUDE_DIR})

file(GLOB SOURCES *.cpp)
set(MAIN_FILE main.cpp)
set(SOURCE_FILES ${SOURCES})

add_executable(inp_manipulation ${MAIN_FILE} ${SOURCE_FILES} )
target_link_libraries(inp_manipulation -static-libgcc -static-libstdc++ boost_program_options)

Question

Where is the mistake in the CMakeLists?

Thanks in advance!

Upvotes: 4

Views: 2374

Answers (1)

Richard Hodges
Richard Hodges

Reputation: 69882

First, you must tell cmake that you require the specific component library from boost:

find_package(Boost 1.62.0 COMPONENTS program_options)

Second, always use the output variables from BoostFind.cmake

target_link_libraries(inp_manipulation -static-libgcc -static-libstdc++ ${Boost_LIBRARIES})

Documentation here: https://cmake.org/cmake/help/v3.0/module/FindBoost.html

Output variables are:

Boost_FOUND - True if headers and requested libraries were found

Boost_INCLUDE_DIRS - Boost include directories

Boost_LIBRARY_DIRS - Link directories for Boost libraries

Boost_LIBRARIES - Boost component libraries to be linked

etc

Upvotes: 3

Related Questions