我如何使用CMake的选择性针对静态或动态的Boost库链接? [英] How can I optionally link against static or dynamic boost library using CMake?

查看:2809
本文介绍了我如何使用CMake的选择性针对静态或动态的Boost库链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个CMake的项目,我有时想编译反对静态Boost库,但我想也可以很容易地只使用动态库从cmake的GU​​I。在我的顶层的CMakeLists.txt我有这样的:

I have a CMake project that I sometimes want to compile against the static boost libraries, but I want to also make it easy to just use the dynamic libraries from the cmake GUI. In my top level CMakeLists.txt I have this:

option(USE_STATIC_BOOST "Build with static BOOST libraries instead of dynamic" NO)

然后在不同的文件中,我有以下的逻辑来设置:

Then in a different file, I have the following logic set up:

if(USE_STATIC_BOOST)
   unset(Boost_LIBRARIES)
   message(WARNING "Linking against boost static libraries")
   set(Boost_USE_STATIC_LIBS ON)
   set(Boost_USE_MULTITHREADED ON)
   find_package(Boost REQUIRED COMPONENTS thread program_options system)
else(USE_STATIC_BOOST)
   unset(Boost_LIBRARIES)
   message(WARNING "Linking against boost dynamic libraries")
   set(Boost_USE_STATIC_LIBS OFF)
   set(Boost_USE_MULTITHREADED ON)
   find_package(Boost REQUIRED COMPONENTS thread program_options system)
endif(USE_STATIC_BOOST)

这似乎很好地工作,如果我从头开始,并使用:

This seems to work fine if I start from scratch and use:

cmake ../.. -DUSE_STATIC_BOOST=YES

然而,当我使用

ccmake ../..

我不能让它使用静态库,无论我做什么。 CMake的出现在动态选项加载到高速缓存的启动和不断变化的USE_STATIC_BOOST不开机。我甚至想取消设置(Boost_LIBRARIES)明确清晰的出来。有没有办法做我想要做什么?

I cannot get it to use the static libraries no matter what I do. CMake appears to load the dynamic option into cache on startup and changing USE_STATIC_BOOST doesn't switch it. I even tried to unset(Boost_LIBRARIES) to explicitly clear it out. Is there a way to do what I'm trying to do?

使用x86_64的Linux和G ++编译。在此先感谢!

Using x86_64 Linux and g++ to compile. Thanks in advance!

推荐答案

要强制的 FindBoost CMake的模块来再次搜索所需的库中,你必须清除缓存变量 Boost_INCLUDE_DIR Boost_LIBRARY_DIRS ,即:

To force the FindBoost CMake module to search for the desired libraries again, you have to clear the cache variables Boost_INCLUDE_DIR and Boost_LIBRARY_DIRS, i.e.:

set(Boost_USE_STATIC_LIBS ${USE_STATIC_BOOST})
set(Boost_USE_MULTITHREADED ON)
unset(Boost_INCLUDE_DIR CACHE)
unset(Boost_LIBRARY_DIRS CACHE)
find_package(Boost REQUIRED COMPONENTS thread program_options system)
if(USE_STATIC_BOOST)
   message(STATUS "Linking against boost static libraries")
else(USE_STATIC_BOOST)
   message(STATUS "Linking against boost dynamic libraries")
endif(USE_STATIC_BOOST)

注意参数缓存是必要的,使的未设置命令清除高速缓存中的变量。

Note that the argument CACHE is necessary to make the unset command clear the variables in the cache.

另外请注意,一旦选择变量 USE_STATIC_BOOST 已经被缓存,你需要明确设置在命令行中的变量或编辑缓存文件,使C进行价值一个变化:

Also note that once the option variable USE_STATIC_BOOST has been cached, you need to explicitly set the variable from the command line or edit the value in the cache file to make CMake notice the change:

cmake ../.. -DUSE_STATIC_BOOST=NO 

这篇关于我如何使用CMake的选择性针对静态或动态的Boost库链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆