Skip to content

Instantly share code, notes, and snippets.

@FlorianWolters
Last active April 3, 2024 22:46
Show Gist options
  • Star 24 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save FlorianWolters/11225791 to your computer and use it in GitHub Desktop.
Save FlorianWolters/11225791 to your computer and use it in GitHub Desktop.
Add Boost C++ Libraries as a dependency with plain CMake
# Copyright (c) 2014-2023 Florian Wolters
# MIT License
cmake_minimum_required(VERSION 3.26.3)
project(
"hello_boost_with_cmake"
VERSION 2.0.0
LANGUAGES CXX)
find_package(
Boost 1.82 REQUIRED
COMPONENTS atomic
bzip2
chrono
container
context
coroutine
contract
date_time
fiber
filesystem
graph
iostreams
json
locale
log_setup
log
math_c99f
math_c99l
math_c99
math_tr1f
math_tr1l
math_tr1
nowide
prg_exec_monitor
program_options
python310
random
regex
serialization
stacktrace_noop
stacktrace_windbg_cached
stacktrace_windbg
system
thread
timer
type_erasure
unit_test_framework
url
wave
wserialization
zlib)
add_executable(main)
target_link_libraries(
main
PRIVATE Boost::headers
Boost::atomic
Boost::bzip2
Boost::chrono
Boost::container
Boost::context
Boost::coroutine
Boost::contract
Boost::date_time
Boost::fiber
Boost::filesystem
Boost::graph
Boost::iostreams
Boost::json
Boost::locale
Boost::log_setup
Boost::log
Boost::math_c99f
Boost::math_c99l
Boost::math_c99
Boost::math_tr1f
Boost::math_tr1l
Boost::math_tr1
Boost::nowide
Boost::prg_exec_monitor
Boost::program_options
Boost::python310
Boost::random
Boost::regex
Boost::serialization
Boost::stacktrace_noop
Boost::stacktrace_windbg_cached
Boost::stacktrace_windbg
Boost::system
Boost::thread
Boost::timer
Boost::type_erasure
Boost::unit_test_framework
Boost::url
Boost::wave
Boost::wserialization
Boost::zlib)
target_sources(main PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp)
{
"version": 6,
"cmakeMinimumRequired": {
"major": 3,
"minor": 26,
"patch": 3
},
"configurePresets": [
{
"name": "default",
"displayName": "Default",
"description": "Default build using Ninja Multi-Config generator",
"generator": "Ninja Multi-Config",
"binaryDir": "${sourceDir}/_b",
"cacheVariables": {
"Boost_INCLUDE_DIR": {
"type": "PATH",
"value": "${sourceDir}/external/boost"
},
"Boost_LIBRARY_DIR_RELEASE": {
"type": "PATH",
"value": "${sourceDir}/external/boost/lib64-msvc-14.3"
},
"Boost_LIBRARY_DIR_DEBUG": {
"type": "PATH",
"value": "${sourceDir}/external/boost/lib64-msvc-14.3"
},
"Boost_NO_SYSTEM_PATHS": {
"type": "BOOL",
"value": "OFF"
},
"Boost_NO_WARN_NEW_VERSIONS": {
"type": "BOOL",
"value": "ON"
},
"Boost_USE_MULTITHREADED": {
"type": "BOOL",
"value": "ON"
},
"Boost_USE_STATIC_LIBS": {
"type": "BOOL",
"value": "ON"
},
"Boost_USE_STATIC_RUNTIME": {
"type": "BOOL",
"value": "OFF"
}
}
}
],
"buildPresets": [
{
"name": "default",
"configurePreset": "default"
}
]
}
/// @file
/// Declares the `main` entry-point free function.
///
/// @author Florian Wolters
/// @date 2014-2023
/// @copyright MIT License
/// Runs the application.
///
/// @return always `0`
auto main() -> int {
return 0;
}
@alexreinking
Copy link

This made sense in 2014, but since it still comes up highly in the Google search results for "boost cmake", I thought I'd add the "modern CMake" way of doing this:

cmake_minimum_required(VERSION 3.25)
project(boost-example)

find_package(Boost 1.70.0 REQUIRED system filesystem)

add_executable(myapp main.cpp)
target_link_libraries(myapp PRIVATE Boost::headers Boost::system Boost::filesystem)

Since version 1.70.0, Boost provides its own CMake package complete with imported targets. These set everything up for you when you link to them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment