Cmake find_package 须要指定具体的so

须要使用cmake的find_package将boost库添加到项目中,经过cmake --help-module FindBoost 能够查看cmake引入Boost的帮助信息:git

image

能够看到,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)

image

这样能够看到打印的结果,所须要的库都找到了,这样target_link_libraries(boostCmake ${Boost_LIBRARIES}才会真正的连接到具体的so上,否则就会一直报连接错误。3d

具体工程能够参考:https://github.com/aktiger/boostCmake.gitcode

相关文章
相关标签/搜索