什么是正确的方法链接Boost与CMake和Visual Studio在Windows中? [英] Whats the proper way to link Boost with CMake and Visual Studio in Windows?
问题描述
我想为Visual Studio 2012生成一些我需要的Boost 1.58库(chrono,regex和thread),并将库链接到CMake。我有真正的问题为CMake和Visual Studio找到或链接的libs,这取决于我设置的配置。
我最后使用以下配置:
bjam.exe --link = static --threading multi --variant = debug阶段
生成静态库。
我应该如何生成库并用CMake搜索它们,以便Visual Studio正确链接它们?
我已经提出了一个解决方案, > Visual Studio搜索动态库,因此我们需要将Boost库编译为共享,多线程,调试和发布,以及运行时链接共享。在windows中使用bjam.exe所有命令都有前缀 - 除了链接,所以正确的方法来构建库是:
bjam.exe link = shared --thread = multi --variant = debug --variant = release --with-chrono --with-regex --with-thread阶段
这将在文件夹Boost / stage / lib中生成libs和DLL,因此我们需要设置一个环境变量Boost_LIBRARY_DIR C:/ Boost / stage /
$ b有更多的选择可能来自:
<$> c $ c> runtime-link = shared / static
toolet = 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)//如果没有VS会给出重定义的链接错误
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 })
I am trying to generate some Boost 1.58 libraries that I need (chrono, regex and thread) for Visual Studio 2012 and link the libraries with CMake. I have real problems for CMake and Visual Studio to find or link the libs, depending on the configuration I set.
I am finally using the following configuration:
bjam.exe --link=static --threading multi --variant=debug stage
But this doesn't seem to generate static libs.
How should I generate the libs and search them with CMake in order for Visual Studio to link them properly?
解决方案I finally came up with the solution and I think it is detailed enough to become a generic answer.
Visual Studio searches for dynamic libraries so we need to compile Boost libraries as shared, multithreaded, debug and release, and runtime-link shared. In windows using bjam.exe all commands have the prefix "--" except link, so the right way to build the libraries is:
bjam.exe link=shared --threading=multi --variant=debug --variant=release --with-chrono --with-regex --with-thread stage
This will generate the libs and DLLs in the folder Boost/stage/lib, so we need to set an environment variable Boost_LIBRARY_DIR C:/Boost/stage/lib, for example.
There are more options that may come in hand:
runtime-link = shared/static toolset= msvc-11.0
The libraries will have a name like this for release:
boost_chrono-vc110-mt-1_58.lib boost_chrono-vc110-mt-1_58.dll
And for debug:
boost_chrono-vc110-mt-gd-1_58.lib boost_chrono-vc110-mt-gd-1_58.dll
In order for CMake to link them properly we need to write the following in the 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与CMake和Visual Studio在Windows中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!