Seraph Seraph - 2 months ago 25
C++ Question

CMake: undefined reference to boost library

I added boost via this:

set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
find_package(Boost REQUIRED)
include_directories(${Boost_INCLUDE_DIR})

project(APP C CXX)
add_executable(APP src.cpp)
target_link_libraries(APP ${Boost_LIBRARIES})


And when I compiled source, I got:

demo.cpp:(.text+0x3d3): undefined reference to `boost::system::generic_category()'


I checked spelling (Boost_LIBRARIES vs BOOST_LIBRARIES) but it's ok.

I installed boost in Fedora with the package boost-devel.

Answer

Looking in the source code, Boost_LIBRARIES is filled according to the component list passed to find_package. Try:

find_package(Boost REQUIRED COMPONENTS system)

You should also use imported targets:

set(Boost_USE_STATIC_LIBS        ON)
set(Boost_USE_MULTITHREADED      ON)
set(Boost_USE_STATIC_RUNTIME    OFF)
find_package(Boost REQUIRED COMPONENTS system)

# the call to include_directories is now useless:
# the Boost::system imported target used below
# embeds the include directories

project(APP C CXX)
add_executable(APP src.cpp)
target_link_libraries(APP Boost::system)