我有一个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不会切换它。我甚至试图取消设置(Boost_LIBRARIES)以明确清除它。有办法做我想做的事吗?
使用x86_64 Linux和g ++进行编译。提前谢谢!
答案 0 :(得分:12)
要强制FindBoost CMake模块再次搜索所需的库,您必须清除缓存变量Boost_INCLUDE_DIR
和Boost_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