CMake:对boost库的未定义引用

时间:2016-09-15 14:09:04

标签: c++ boost cmake

我通过这个添加了提升:

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})

当我编译源代码时,我得到了:

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

我检查了拼写(Boost_LIBRARIES vs BOOST_LIBRARIES),但没关系。

我使用包boost-devel在Fedora中安装了boost。

1 个答案:

答案 0 :(得分:5)

查看source code,根据传递给Boost_LIBRARIES的组件列表填充find_package。尝试:

find_package(Boost REQUIRED COMPONENTS system)

您还应该使用导入的目标:

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)