C++ 使用 CMake 链接 Boost 库的静态版本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3176035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 12:14:50  来源:igfitidea点击:

Link the static versions of the Boost libraries using CMake

c++boostcmake

提问by domachine

I've got both the static and the dynamic versions of the boost libraries in /usr/lib. Now I'd like CMake to prefer the static versions during the linkage of my executable. What can I do?

我在/usr/lib 中有boost 库的静态和动态版本。现在我希望 CMake 在我的可执行文件链接期间更喜欢静态版本。我能做什么?

回答by greyfade

In your CMakeLists.txtfile:

在您的CMakeLists.txt文件中:

set(Boost_USE_STATIC_LIBS   ON)
find_package(Boost REQUIRED ...)

Where I have ..., you optionally put the names of the libraries you want to use, and then target_link_libraries(targetname ${Boost_LIBRARIES})later below. If you have a fairly recent distribution of CMake, it should work exactly as advertised. I do it exactly this way in my own projects.

在我有的地方...,您可以选择将要使用的库的名称放在target_link_libraries(targetname ${Boost_LIBRARIES})下面,然后放在下面。如果你有一个相当新的 CMake 发行版,它应该像宣传的那样工作。我在自己的项目中就是这样做的。

回答by bowman han

Here is a full example of CMAKEFILE

这是 CMAKEFILE 的完整示例

cmake_minimum_required(VERSION 3.15)
project(your_project)
set(Boost_USE_STATIC_LIBS   ON)
find_package(Boost 1.70 COMPONENTS program_options REQUIRED)
set(CMAKE_CXX_STANDARD 14)   
add_executable(your_project main.cpp)
target_link_libraries(rconpp Boost::program_options)

references:

参考:

cmake documents about BOOST

关于 BOOST 的 cmake 文档