且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我如何使用CMake的选择性针对静态或动态的Boost库链接?

更新时间:2023-10-22 22:34:10

要强制的 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