2016-09-15 124 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