ModernCMake-Chinese
  • Introduction
  • Modern CMake 简介
    • 安装 CMake
    • 运行 CMake
    • Do's and Don'ts
    • CMake 中的新变化
  • 基础知识简介
    • 变量和 Cache
    • 在CMake 中编程
    • 与你的代码交互
    • 如何结构化你的工程
    • 运行其他程序
  • Adding Features
    • C++11 and Beyond
    • Small but common needs
    • Utilities
    • Useful modules
    • IDEs
    • Debugging
  • Including Projects
    • Submodule
    • DownloadProject
    • Fetch (CMake 3.11)
  • Testing
    • GoogleTest
    • Catch
  • Exporting and Installing
    • Installing
    • Exporting
    • Packaging
  • Looking for libraries
    • CUDA
    • OpenMP
    • Boost
    • MPI
    • ROOT
      • UseFile Example
      • Simple Example
      • Simple Example CMake 3.11+
      • Dictionary Example
    • Minuit2
Powered by GitBook
On this page

Was this helpful?

  1. Looking for libraries

Boost

PreviousOpenMPNextMPI

Last updated 6 years ago

Was this helpful?

The Boost library is included in the find packages that CMake provides, but it has a couple of oddities in how it works. See for a full description; this will just give a quick overview and provide a recipe. Be sure to check the page for the minimum required version of CMake you are using and see what options you have.

First, you can customize the behavior of the Boost libraries selected using a set of variables that you set before searching for Boost. There are a growing number of settings, but here are the three most common ones:

set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)

In CMake 3.5, imported targets were added. These targets handle dependencies for you as well, so they are a very nice way to add Boost libraries. However, CMake has the dependency information baked into it for all known versions of Boost, so CMake must be newer than Boost for these to work. In a recent , CMake started assuming that the dependencies hold from the last version it knows about, and will use that (along with giving a warning). This functionality was backported into CMake 3.9.

The import targets are in the Boost:: namespace. Boost::boost is the header only part. The other compiled libraries are available, and include dependencies as needed.

Here is an example for using the Boost::filesystem library:

set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
find_package(Boost 1.50 REQUIRED COMPONENTS filesystem)
message(STATUS "Boost version: ${Boost_VERSION}")

# This is needed if your Boost version is newer than your CMake version
# or if you have an old version of CMake (<3.5)
if(NOT TARGET Boost::filesystem)
    add_library(Boost::filesystem IMPORTED INTERFACE)
    set_property(TARGET Boost::filesystem PROPERTY
        INTERFACE_INCLUDE_DIRECTORIES ${Boost_INCLUDE_DIR})
    set_property(TARGET Boost::filesystem PROPERTY
        INTERFACE_LINK_LIBRARIES ${Boost_LIBRARIES})
endif()

target_link_libraries(MyExeOrLibrary PUBLIC Boost::filesystem)
FindBoost
merge request