2016-12-30 109 views
0

我想在我的C++項目中包含Boost的線程庫。我的CMake的文件是像這樣:鏈接Boost線程庫

cmake_minimum_required(VERSION 3.6) 
project(LearningC) 

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

set(CMAKE_CXX_STANDARD 11) 

set(SOURCE_FILES main.cpp Student.cpp Student.h) 

add_executable(LearningC ${SOURCE_FILES}) 

target_link_libraries(LearningC ${Boost_LIBRARIES}) 

我得到一個錯誤:

Undefined symbols for architecture x86_64: 
    "boost::this_thread::interruption_point()", referenced from: 
     boost::condition_variable::wait(boost::unique_lock<boost::mutex>&) in main.cpp.o 
    [More stack traces...] 

我在做什麼錯?

+1

所以你需要在'find_package(Boost)'調用中列出'線程'庫:'find_package(Boost COMPONENTS thread REQUIRED)'。像[there](http://stackoverflow.com/a/3917033/3440745)。 – Tsyvarev

+0

在我的情況下,我必須添加'find_package(Boost COMPONENTS thread system REQUIRED)'和'target_link_libraries( $ {Boost_LIBRARIES})''當然! – Tanasis

回答

0

我找到了解決方案。基本上,Boost的大部分代碼都在C++頭文件(.hpp)中。然而,一些庫需要編譯和鏈接...下面的代碼工作!

cmake_minimum_required(VERSION 3.6) 
project(LearningC) 

set(CMAKE_CXX_STANDARD 11) 

set(SOURCE_FILES main.cpp Student.cpp Student.h) 

add_executable(LearningC ${SOURCE_FILES}) 

find_package(Boost COMPONENTS thread system REQUIRED) 

include_directories(${Boost_INCLUDE_DIR}) 
target_link_libraries(LearningC ${Boost_LIBRARIES}) 
1

好吧,我看你已經找到了解決辦法,但也有一些改進,我想提出(只要您需要的CMake 3.6):

  • 使用進口的目標管理每個目標的編譯器/連接器選項,而不是「全局」變量和函數(如include_directories(),...)
  • 使用project()完整簽名定義一堆PROJECT_xxx變量,然後用它們
  • 提供LA的顯式列表, nguages是否用於避免默認和可能的冗餘檢查
  • 在Boost的情況下,您不需要查找並鏈接隱式依賴關係 - FindBoost.cmake會爲您做到這一點。只需指定組件只是你真正需要和使用的。

下面是一個修改CMakeLists.txt

cmake_minimum_required(VERSION 3.6) 
project(LearningCxx VERSION 1.0.0 LANGUAGES CXX) 

set(CMAKE_CXX_STANDARD 11) 
set(CMAKE_CXX_STANDARD_REQUIRED ON) 

find_package(Boost COMPONENTS thread REQUIRED) 

add_executable(
    ${PROJECT_NAME} 
    main.cpp 
    Student.cpp 
) 

target_link_libraries(
    ${PROJECT_NAME} 
    Boost::thread 
) 

更新:我做了一個例子爲這個項目,但刪除Students.cpp,並與下面的代碼替換main.cpp

#include <boost/thread.hpp> 
#include <iostream> 

int main() 
{ 
    std::cout << boost::thread::hardware_concurrency() << std::endl; 
} 

這是我的測試運行:

[email protected]〉…/tests/boost-thread-cmake/build〉empty dir〉cmake .. 
-- The CXX compiler identification is GNU 5.4.0 
-- Check for working CXX compiler: /usr/lib/outproc/bin/c++ -- works 
-- Detecting CXX compiler ABI info - done 
-- Detecting CXX compile features - done 
-- Looking for C++ include pthread.h - found 
-- Looking for pthread_create - not found 
-- Looking for pthread_create in pthreads - not found 
-- Looking for pthread_create in pthread - found 
-- Found Threads: TRUE 
-- Boost version: 1.62.0 
-- Found the following Boost libraries: 
-- thread 
-- chrono 
-- system 
-- date_time 
-- atomic 
-- Configuring done 
-- Generating done 
-- Build files have been written to: /work/tests/boost-thread-cmake/build 
[email protected]〉…/tests/boost-thread-cmake/build〉default〉pfx: /usr/local〉make 
Scanning dependencies of target LearningCxx 
[ 50%] Building CXX object CMakeFiles/LearningCxx.dir/main.cpp.o 
[100%] Linking CXX executable LearningCxx 
[100%] Built target LearningCxx 
[email protected]〉…/tests/boost-thread-cmake/build〉default〉pfx: /usr/local〉ldd ./LearningCxx 
     linux-vdso.so.1 (0x00007fff73b75000) 
     libboost_thread.so.1.62.0 => /usr/lib64/libboost_thread.so.1.62.0 (0x00007fd1adafd000) 
     libboost_chrono.so.1.62.0 => /usr/lib64/libboost_chrono.so.1.62.0 (0x00007fd1ad8f6000) 
     libboost_system.so.1.62.0 => /usr/lib64/libboost_system.so.1.62.0 (0x00007fd1ad6f2000) 
     libboost_date_time.so.1.62.0 => /usr/lib64/libboost_date_time.so.1.62.0 (0x00007fd1ad4e1000) 
     libboost_atomic.so.1.62.0 => /usr/lib64/libboost_atomic.so.1.62.0 (0x00007fd1ad2df000) 
     libpthread.so.0 => /lib64/libpthread.so.0 (0x00007fd1ad0c3000) 
     libstdc++.so.6 => /usr/lib/gcc/x86_64-pc-linux-gnu/6.3.0/libstdc++.so.6 (0x00007fd1accc8000) 
     libm.so.6 => /lib64/libm.so.6 (0x00007fd1ac9cd000) 
     libgcc_s.so.1 => /usr/lib/gcc/x86_64-pc-linux-gnu/6.3.0/libgcc_s.so.1 (0x00007fd1ac7b6000) 
     libc.so.6 => /lib64/libc.so.6 (0x00007fd1ac41d000) 
     librt.so.1 => /lib64/librt.so.1 (0x00007fd1ac215000) 
     /lib64/ld-linux-x86-64.so.2 (0x00007fd1add25000) 
[email protected]〉…/tests/boost-thread-cmake/build〉default〉pfx: /usr/local〉cmake --version 
cmake version 3.7.1 
+0

通過「隱式依賴」...你是指系統庫嗎?如果是這樣,我已將它刪除,編譯器抱怨:架構x86_64的未定義符號:「boost :: system :: system_category()」 –

+0

@MikeM,據我所知,FindBoost.cmake能夠找到並在CMake 3.6中鏈接w/implicit deps以及...在這個模塊中檢查'_Boost_MISSING_DEPENDENCIES'函數... – zaufi