Look for Boost library when compiling with CMake under Linux
find_package
find_package
You can find the header file and the required library file or a CMake packaging configuration file by calling ,
find_package(Boost
[version] [EXACT] # 可选项,最小版本或者确切所需版本
[REQUIRED] # 可选项,如果找不到所需库,报错
[COMPONENTS <libs>...] # 所需的库名称,比如说. "date_time" 代表 "libboost_date_time"
)
After running, you can get a lot of variables, some of the main ones are listed below
Boost_FOUND - 如果找到了所需的库就设为true
Boost_INCLUDE_DIRS - Boost头文件搜索路径
Boost_LIBRARY_DIRS - Boost库的链接路径
Boost_LIBRARIES - Boost库名,用于链接到目标程序
Boost_VERSION - 从boost/version.hpp文件获取的版本号
Boost_LIB_VERSION - 某个库的版本
You can help find the boost library by setting some variables before searching for the package
BOOST_ROOT - 首选的Boost安装路径
BOOST_INCLUDEDIR - 首选的头文件搜索路径 e.g. <prefix>/include
BOOST_LIBRARYDIR - 首选的库文件搜索路径 e.g. <prefix>/lib
Boost_NO_SYSTEM_PATHS - 默认是OFF. 如果开启了,则不会搜索用户指定路径之外的路径
Sample program
If the target program foo needs to link the Boost library regex and system, write the following CMakeLists file,
# CMakeLists.txt
project(tutorial-0)
cmake_minimum_required(VERSION 3.5)
set(CMAKE_CXX_STANDARD 14)
set(BOOST_ROOT /usr/local/install/boost_1_61_0)
find_package(Boost COMPONENTS regex system REQUIRED)
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
MESSAGE( STATUS "Boost_INCLUDE_DIRS = ${Boost_INCLUDE_DIRS}.")
MESSAGE( STATUS "Boost_LIBRARIES = ${Boost_LIBRARIES}.")
MESSAGE( STATUS "Boost_LIB_VERSION = ${Boost_LIB_VERSION}.")
add_executable(foo foo.cpp)
target_link_libraries (foo ${Boost_LIBRARIES})
endif()
- Set the preferred search path by setting BOOST_ROOT
- Print out the search results through the MESSAGE function
-- Boost_INCLUDE_DIRS = /usr/local/install/boost_1_61_0/include.
-- Boost_LIBRARIES = /usr/local/install/boost_1_61_0/lib/libboost_regex.so;/usr/local/install/boost_1_61_0/lib/libboost_system.so.
-- Boost_LIB_VERSION = 1_61.
- Compilation process need to use the search path is stored in the variable Boost_INCLUDE_DIRS library file path, the required link is stored in the variable Boost_LIBRARIES in
Reprinted: https://www.jianshu.com/p/1827cd86d576