短版
您只需要BOOST_ROOT
,但如果您有多个安装或针对 iOS 或 Android 的交叉编译,您将希望禁用在系统中搜索本地 Boost。在这种情况下,addBoost_NO_SYSTEM_PATHS
设置为 false。
set( BOOST_ROOT "" CACHE PATH "Boost library path" )
set( Boost_NO_SYSTEM_PATHS on CACHE BOOL "Do not search system for Boost" )
通常,这是使用语法在 CMake 命令行上传递的-D<VAR>=value
。
更长的版本
正式来说FindBoost页面声明这些变量应该用于“提示”Boost 的位置。
该模块从变量中读取有关搜索位置的提示:
BOOST_ROOT - Preferred installation prefix
(or BOOSTROOT)
BOOST_INCLUDEDIR - Preferred include directory e.g. <prefix>/include
BOOST_LIBRARYDIR - Preferred library directory e.g. <prefix>/lib
Boost_NO_SYSTEM_PATHS - Set to ON to disable searching in locations not
specified by these hint variables. Default is OFF.
Boost_ADDITIONAL_VERSIONS
- List of Boost versions not known to this module
(Boost install locations may contain the version)
这使得理论上正确的咒语:
cmake -DBoost_NO_SYSTEM_PATHS=TRUE \
-DBOOST_ROOT=/path/to/boost-dir
从源代码编译时
include( ExternalProject )
set( boost_URL "http://sourceforge.net/projects/boost/files/boost/1.63.0/boost_1_63_0.tar.bz2" )
set( boost_SHA1 "9f1dd4fa364a3e3156a77dc17aa562ef06404ff6" )
set( boost_INSTALL ${CMAKE_CURRENT_BINARY_DIR}/third_party/boost )
set( boost_INCLUDE_DIR ${boost_INSTALL}/include )
set( boost_LIB_DIR ${boost_INSTALL}/lib )
ExternalProject_Add( boost
PREFIX boost
URL ${boost_URL}
URL_HASH SHA1=${boost_SHA1}
BUILD_IN_SOURCE 1
CONFIGURE_COMMAND
./bootstrap.sh
--with-libraries=filesystem
--with-libraries=system
--with-libraries=date_time
--prefix=<INSTALL_DIR>
BUILD_COMMAND
./b2 install link=static variant=release threading=multi runtime-link=static
INSTALL_COMMAND ""
INSTALL_DIR ${boost_INSTALL} )
set( Boost_LIBRARIES
${boost_LIB_DIR}/libboost_filesystem.a
${boost_LIB_DIR}/libboost_system.a
${boost_LIB_DIR}/libboost_date_time.a )
message( STATUS "Boost static libs: " ${Boost_LIBRARIES} )
然后,当您调用此脚本时,您需要包含 boost.cmake 脚本(我的位于 a 子目录中)、包含标题、指示依赖项并链接库。
include( boost )
include_directories( ${boost_INCLUDE_DIR} )
add_dependencies( MyProject boost )
target_link_libraries( MyProject
${Boost_LIBRARIES} )