Cmake find_package 需要指定具体的so

需要使用cmake的find_package将boost库添加到项目中,通过cmake --help-module FindBoost 可以查看cmake引入Boost的帮助信息:

image

可以看到,Boot_LIBRARIES确实是boost相关的库,刚开始编写的CMakeLists.txt文件如下:

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}") 打印的值始终为空。

这是因为cmake的find_package需要制定具体的library,${Boost_LIBRARIES}才会有值,要不然就会是空,find_package的正确写法是:

find_package(Boost 1.54 REQUIRED COMPONENTS system  thread)

image

这样可以看到打印的结果,所需要的库都找到了,这样target_link_libraries(boostCmake ${Boost_LIBRARIES}才会真正的链接到具体的so上,不然就会一直报链接错误。

具体工程可以参考:https://github.com/aktiger/boostCmake.git

posted @ 2018-05-09 14:47  justinzhang  阅读(1215)  评论(0编辑  收藏  举报