須要使用cmake的find_package將boost庫添加到項目中,經過cmake --help-module FindBoost 能夠查看cmake引入Boost的幫助信息:git
能夠看到,Boot_LIBRARIES確實是boost相關的庫,剛開始編寫的CMakeLists.txt文件以下:github
cmake_minimum_required(VERSION 2.8.4) project(boostCmake) set(CMAKE_CXX_STANDARD 11) set(Boost_DEBUG 1) find_package(Boost) if(Boost_FOUND) MESSAGE("Boost_FOUND") include_directories(${Boost_INCLUDE_DIRS}) link_directories(${Boost_LIBRARIES}) MESSAGE(WARNING "Boost_INCLUDE_DIRS is ${Boost_INCLUDE_DIRS}") add_executable(boostCmake main.cpp) MESSAGE(WARNING "Boost_LIBRARIES is ${Boost_LIBRARY_DIRS}") MESSAGE(WARNING "Boost_LIBRARIES is ${Boost_LIBRARIES}") target_link_libraries(boostCmake ${Boost_LIBRARIES} ) endif()
但是連接一直不成功,MESSAGE(WARNING "Boost_LIBRARIES is ${Boost_LIBRARIES}") 打印的值始終爲空。ui
這是由於cmake的find_package須要制定具體的library,${Boost_LIBRARIES}纔會有值,要否則就會是空,find_package的正確寫法是:spa
find_package(Boost 1.54 REQUIRED COMPONENTS system thread)
這樣能夠看到打印的結果,所須要的庫都找到了,這樣target_link_libraries(boostCmake ${Boost_LIBRARIES}纔會真正的連接到具體的so上,否則就會一直報連接錯誤。3d
具體工程能夠參考:https://github.com/aktiger/boostCmake.gitcode