在Windows上将boost库与Boost_USE_STATIC_LIB关闭 [英] Linking boost library with Boost_USE_STATIC_LIB OFF on Windows
问题描述
我的CMakeFiles.txt如下所示:
cmake_minimum_required(VERSION 2.6)
#Set警告和启用调试
SET(CMAKE_C_FLAGS-Wall -q)
include(FindBoost)
set(Boost_USE_STATIC_LIBS ON)
set Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
find_package(Boost 1.57.0 COMPONENTS系统文件系统要求)
if(Boost_FOUND)
消息boost found!)
include_directories($ {Boost_INCLUDE_DIRS})
add_executable(foo main.cpp)
需要asio
if(WIN32)
target_link_libraries(foo wsock32 ws2_32)
endif()
target_link_libraries(foo $ {Boost_LIBRARIES})
endif()
我为Visual Studio 2013 64位渲染项目:
cmake -GVisual Studio 12 Win64-DBOOST_LIBRARYDIR = D:\Development\Tools\boost_1_57_0\stage\x64\lib ..\KServer
输出为:
C编译器标识是MSVC 18.0.31101.0
- CXX编译器标识是MSVC 18.0.31101.0
- 检查工作C编译器使用:Visual Studio 12 2013 Win64
- 检查工作C编译器使用:Visual Studio 12 2013 Win64 - works
- 检测C编译器ABI信息
- 检测C编译器ABI信息 - 完成
- 检查工作CXX编译器使用:Visual Studio 12 2013 Win64
- 检查工作CXX编译器使用:Visual Studio 12 2013 Win64 - 工作
- 检测CXX编译器ABI信息
- 检测CXX编译器ABI信息 - 完成
- Boost版本:1.57.0
- Boost版本:1.57.0
- 找到以下Boost库:
- system
- filesystem
- 找到Boost!
- 配置完成
- 生成完成
- 构建文件已写入:D:/ Development / Private / C ++ / KServerProject
$ b
当我改变我的cmake文件使用:
(Boost_USE_STATIC_LIBS OFF)
然后在Visual Studio中创建时出现以下错误:
错误LNK1104:无法打开文件'libboost_filesystem-vc120-mt-gd-1_57.lib'D:\Development\Private\C ++ \\ \\ KServerProject\src\LINK foo
检查
属性页
在工作室库添加为依赖:
手动添加文件夹
D:\Development\Tools\boost_1_57_0\ stage \x64 \lib
到其他库目录
它构建良好。
如何使用动态库创建项目?
解决方案我相信你需要添加
add_definitions(-DBOOST_ALL_NO_LIB)
请参阅 http://www.boost.org /doc/libs/1_57_0/libs/config/doc/html/index.html 。我有它设置在我的CMakeLists.txt和它的工作原理我的视觉工作室建立与增强。作为一个测试,我删除它,并得到了你做的相同的错误。
这是值得的,这里是如何使用boost与cmake。
#boost
set(Boost_NO_SYSTEM_PATHS true)
set(Boost_USE_STATIC_LIBS OFF CACHE BOOL使用Boost的静态库)
set(Boost_USE_MULTITHREADED ON)
find_package(Boost REQUIRED
COMPONENTS
system program_options线程文件系统
date_time计时器正则表达式序列化
)
include_directories($ { Boost_INCLUDE_DIRS})
link_libraries($ {Boost_LIBRARIES})
if(WIN32)
#禁用boost中的自动链接
add_definitions(-DBOOST_ALL_NO_LIB)
#强制所有boost库动态链接(我们已经禁用
#autolinking,所以我不知道为什么我们需要这个,但我们做!)
add_definitions(-DBOOST_ALL_DYN_LINK)
endif()
My CMakeFiles.txt looks like this:
cmake_minimum_required ( VERSION 2.6 ) # Set warnings on and enable debugging SET( CMAKE_C_FLAGS "-Wall -q" ) include(FindBoost) set(Boost_USE_STATIC_LIBS ON) set(Boost_USE_MULTITHREADED ON) set(Boost_USE_STATIC_RUNTIME OFF) find_package( Boost 1.57.0 COMPONENTS system filesystem REQUIRED ) if( Boost_FOUND ) message( STATUS "Boost found!" ) include_directories(${Boost_INCLUDE_DIRS}) add_executable(foo main.cpp) # Needed for asio if(WIN32) target_link_libraries(foo wsock32 ws2_32) endif() target_link_libraries(foo ${Boost_LIBRARIES}) endif()
I render the project for Visual Studio 2013 64-bit:
cmake -G "Visual Studio 12 Win64" -DBOOST_LIBRARYDIR=D:\Development\Tools\boost_1_57_0\stage\x64\lib ..\KServer
The output is:
-- The C compiler identification is MSVC 18.0.31101.0 -- The CXX compiler identification is MSVC 18.0.31101.0 -- Check for working C compiler using: Visual Studio 12 2013 Win64 -- Check for working C compiler using: Visual Studio 12 2013 Win64 -- works -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working CXX compiler using: Visual Studio 12 2013 Win64 -- Check for working CXX compiler using: Visual Studio 12 2013 Win64 -- works -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Boost version: 1.57.0 -- Boost version: 1.57.0 -- Found the following Boost libraries: -- system -- filesystem -- Boost found! -- Configuring done -- Generating done -- Build files have been written to: D:/Development/Private/C++/KServerProject
This is all good and well.
Problem starts here:
When I change my cmake file to use:
set(Boost_USE_STATIC_LIBS OFF)
I then get the following error in Visual Studio when building:
error LNK1104: cannot open file 'libboost_filesystem-vc120-mt-gd-1_57.lib' D:\Development\Private\C++\KServerProject\src\LINK foo
Checking the
Property Pages
in the studio the library is added as a dependency:When manually adding the folder
D:\Development\Tools\boost_1_57_0\stage\x64\lib
toAdditional Library Directories
it builds fine.How can I get it to create project using dynamic libs?
解决方案I believe you need to add
add_definitions( -DBOOST_ALL_NO_LIB )
See http://www.boost.org/doc/libs/1_57_0/libs/config/doc/html/index.html. I have it set in my CMakeLists.txt and it works for my visual studio builds with boost. As a test, I removed it and got the same error you did.
For what it is worth, here is how I use boost with cmake.
# boost set(Boost_NO_SYSTEM_PATHS true) set (Boost_USE_STATIC_LIBS OFF CACHE BOOL "use static libraries from Boost") set (Boost_USE_MULTITHREADED ON) find_package(Boost REQUIRED COMPONENTS system program_options thread filesystem date_time chrono timer regex serialization ) include_directories(${Boost_INCLUDE_DIRS}) link_libraries(${Boost_LIBRARIES}) if (WIN32) # disable autolinking in boost add_definitions( -DBOOST_ALL_NO_LIB ) # force all boost libraries to dynamic link (we already disabled # autolinking, so I don't know why we need this, but we do!) add_definitions( -DBOOST_ALL_DYN_LINK ) endif()
这篇关于在Windows上将boost库与Boost_USE_STATIC_LIB关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!