2016-09-15 69 views
1

我通过这个添加上去:CMake的:未定义参考Boost库

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),但它的确定。

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

+3

是什么'$ {Boost_LIBRARIES}'的内容? – Hayt

+0

它应该是静态提升lib的路径(https://cmake.org/cmake/help/v3.0/module/FindBoost.html) – Seraph

+2

没有“一个”静态提升lib。你能打印它,而不是它“应该”? – Hayt

回答

4

查看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) 
+0

cmake无法找到boost_system ...我已经安装了boost-system,但是boost-system-dev不存在 – Seraph

+3

仅仅针对其他人:链接语法'Boost :: '会自动将include目录添加到您的目标。这就是为什么'include_directories'在这里是“无用的” – Hayt

+0

@Seraph在构建 – Hayt