- CMake Cookbook
- Radovan Bast Roberto Di Remigio
- 189字
- 2025-04-04 16:17:18
How to do it
Boost consists of many different libraries and these can be used almost independently from each other. Internally, CMake represents this library collection as a collection of components. The FindBoost.cmake module can search not only for the full installation of the library collection but also for particular components and their dependencies within the collection, if any. We will build up the corresponding CMakeLists.txt step by step:
- We first declare the minimum CMake version, project name, language, and enforce the use of the C++11 standard:
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
project(recipe-08 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
- Then, we use find_package to search for Boost. The dependency on Boost is mandatory, hence the REQUIRED argument. Since we only need the filesystem component in this example, we pass that as an argument after the COMPONENTS keyword to find_package:
find_package(Boost 1.54 REQUIRED COMPONENTS filesystem)
- We add an executable target, to compile the example source file:
add_executable(path-info path-info.cpp)
- Finally, we link the target to the Boost library component. Since the dependency is declared PUBLIC, targets depending on our target will pick up the dependency automatically:
target_link_libraries(path-info
PUBLIC
Boost::filesystem
)