9

我有一个 CMake 项目,有时我想针对静态 boost 库进行编译,但我也想让使用 cmake GUI 中的动态库变得容易。在我的顶级 CMakeLists.txt 我有这个:

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

然后在另一个文件中,我设置了以下逻辑:

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)

如果我从头开始并使用,这似乎工作正常:

cmake ../.. -DUSE_STATIC_BOOST=YES

但是,当我使用

ccmake ../..

无论我做什么,我都无法让它使用静态库。CMake 似乎在启动时将动态选项加载到缓存中,并且更改 USE_STATIC_BOOST 不会切换它。我什至试图 unset(Boost_LIBRARIES) 明确清除它。有没有办法做我想做的事?

使用 x86_64 Linux 和 g++ 编译。提前致谢!

4

1 回答 1

12

要强制FindBoost CMake 模块再次搜索所需的库,您必须清除缓存变量Boost_INCLUDE_DIRBoost_LIBRARY_DIRS,即:

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()
   message(STATUS "Linking against boost dynamic libraries")
endif()

请注意,该参数CACHE是使unset命令清除缓存中的变量所必需的。

另请注意,一旦选项变量USE_STATIC_BOOST被缓存,您需要从命令行显式设置变量或编辑缓存文件中的值以使 CMake 注意到更改:

cmake ../.. -DUSE_STATIC_BOOST=NO 
于 2012-07-27T12:51:51.050 回答