CMake与Boost Libraries的链接

时间:2016-05-01 21:33:25

标签: boost cmake

我正在尝试为我的Windows CMake项目链接boost_system库,但不断收到以下错误。我已经针对类似的问题尝试了几种提议的解决方案,但似乎没有任何效果。

Error:Unable to find the requested Boost libraries.
Boost version: 1.60.0
Boost include path: C:/Program Files/boost_1_60_0
Could not find the following Boost libraries:
    boost_system
No Boost libraries were found.  You may need to set BOOST_LIBRARYDIR to the 
directory containing Boost libraries or BOOST_ROOT to the location of Boost.

这是我的cmake文件的当前状态(与提升有关)

set(Boost_USE_STATIC_LIBS OFF)  
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)

SET(BOOST_INCLUDEDIRS "C:/Program Files/boost_1_60_0")
SET(BOOST_LIBRARYDIR "C:/Program Files/boost_1_60_0/lib")

find_package(Boost 1.60.0 COMPONENTS system REQUIRED)

if(Boost_FOUND)
    include_directories(${Boost_INCLUDE_DIRS})
LINK_DIRECTORIES(${Boost_LIBRARY_DIRS})
endif()

2 个答案:

答案 0 :(得分:2)

以下是我如何移植它,但您需要指定所需的组件:

首先,您需要构建库。在Windows中打开命令提示符并输入:(您需要指定MinGW安装文件夹的/ bin /文件夹,因此请相应地修改第一行)

PATH=C:\MinGW\bin;%PATH%
cd C:/Program Files/boost_1_60_0/
bootstrap.bat 
b2 --toolset=gcc link=static --build-type=complete

编译需要几分钟。 然后在你的CMakeLists.txt中添加:

if (WIN32)
    set(BOOST_ROOT C:/Program Files/boost_1_60_0/)
    set(Boost_USE_STATIC_LIBS OFF)  
    set(Boost_USE_MULTITHREADED ON)
    set(Boost_USE_STATIC_RUNTIME OFF)

    include_directories(${BOOST_ROOT})
    link_directories(${BOOST_ROOT}/stage/lib) # add this before add_executable()
endif()

# Here add your add_executable function
add_executable(your_exec ${SOURCE_FILES} ${INCLUDE_FILES})

if(NOT MSVC)
    find_package(Boost REQUIRED COMPONENTS date_time filesystem wserialization system serialization thread regex)

    if (Boost_FOUND)
        include_directories(${Boost_INCLUDE_DIRS})
        target_link_libraries(your_exec ${Boost_LIBRARIES})
    endif()
endif()

答案 1 :(得分:0)

您使用的是错误的变量名称。它应该是

SET(BOOST_INCLUDEDIR "C:/Program Files/boost_1_60_0")

记住失踪的复数。 另请参阅https://cmake.org/cmake/help/v3.5/module/FindBoost.html

顺便说一句,您不应该这样设置,而是将C:/Program Files/boost_1_60_0添加到CMAKE_PREFIX_PATH并将其传递给您的cmake电话。