【问题标题】:Whats the proper way to link Boost with CMake and Visual Studio in Windows?在 Windows 中将 Boost 与 CMake 和 Visual Studio 链接的正确方法是什么?
【发布时间】:2015-09-10 11:59:24
【问题描述】:

我正在尝试为 Visual Studio 2012 生成一些我需要的 Boost 1.58 库(chrono、regex 和 thread),并将这些库与 CMake 链接。根据我设置的配置,CMake 和 Visual Studio 在查找或链接库时遇到了实际问题。

我终于用下面的配置了:

bjam.exe --link=static --threading multi --variant=debug 阶段

但这似乎不会生成静态库。

我应该如何生成库并使用 CMake 搜索它们以便 Visual Studio 正确链接它们?

【问题讨论】:

    标签: visual-studio-2012 boost cmake


    【解决方案1】:

    我终于想出了解决方案,我认为它足够详细,可以成为一个通用的答案。

    Visual Studio 搜索动态库,因此我们需要将 Boost 库编译为共享、多线程、调试和发布以及运行时链接共享。在使用 bjam.exe 的 windows 中,除了链接之外,所有命令都有前缀“--”,因此构建库的正确方法是:

    bjam.exe link=shared --threading=multi --variant=debug --variant=release --with-chrono --with-regex --with-thread stage
    

    这将在文件夹 Boost/stage/lib 中生成库和 DLL,因此我们需要设置一个环境变量 Boost_LIBRARY_DIR C:/Boost/stage/lib,例如。

    可能会有更多选择:

    runtime-link = shared/static
    toolset= msvc-11.0
    

    这些库的发布名称如下:

    boost_chrono-vc110-mt-1_58.lib
    boost_chrono-vc110-mt-1_58.dll
    

    对于调试:

    boost_chrono-vc110-mt-gd-1_58.lib
    boost_chrono-vc110-mt-gd-1_58.dll
    

    为了让 CMake 正确链接它们,我们需要在 CMakeLists.txt 中编写以下内容:

        add_definitions( -DBOOST_ALL_DYN_LINK )  //If not VS will give linking errors of redefinitions
        set(Boost_USE_STATIC_LIBS OFF )
        set(Boost_USE_MULTITHREADED ON)
        set(Boost_USE_STATIC_RUNTIME OFF)
        find_package(Boost COMPONENTS thread chrono regex REQUIRED )
    
        INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS})
    
        TARGET_LINK_LIBRARIES( ${PROJ_NAME} ${Boost_LIBRARIES} )  
    

    【讨论】:

    • 请注意,绝对可以使用静态 boost 进行构建,但您可能需要在找到 Boost 之前指定 Boost_USE_STATIS_LIBS=On,另请参阅 stackoverflow.com/a/6646746/417197
    【解决方案2】:

    bjam.exe --link=static --threading multi --variant=debug stage

    但这似乎不会生成静态库。

    构建特殊阶段目标将 Boost 库二进制文件放在 Boost 树的 stage\lib\ 子目录中。 更多关于在 Windows 上构建 Boost here

    CMake:

    SET (CMAKE_BUILD_TYPE Debug) # in order to link with boost debug libs you may need to set that to build your program in debug mode (or do that from command line)
    
    FIND_PACKAGE (Boost 1.58 COMPONENTS "chrono" "regex" "thread" REQUIRED)
    
    #ADD_DEFINITIONS(-DBOOST_ALL_DYN_LINK) # make sure you don't have this as it will try to link with boost .dll's
    
    INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS})
    LINK_DIRECTORIES(${Boost_LIBRARY_DIRS})
    
    TARGET_LINK_LIBRARIES(${EXE_OR_LIB_NAME} ${Boost_LIBRARIES})
    

    【讨论】:

    • 然后检查在Boost_LIBRARIES 下实际找到的内容,并将其与您的 boost 构建的内容进行比较。没有发布更多细节,我们只能猜测......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-18
    • 1970-01-01
    • 2019-01-31
    • 2018-12-01
    • 2011-12-16
    • 1970-01-01
    相关资源
    最近更新 更多