If you have written much C++, you have reached for Boost, and probably lost an afternoon to linker errors getting it installed. Here are the practical ways to add Boost to a project, with the snippets that actually work.
Header-only vs compiled
Boost is ~160 libraries in two camps. Header-only ones (asio, beast, mp11, hana, pfr) need only an #include. Compiled ones (filesystem, program_options, thread, regex, serialization, log) ship .so/.a files you must link. One gotcha: boost::system has been mostly header-only since 1.69, so you rarely need -lboost_system anymore, despite what older tutorials say.
Method 1: CMake find_package (system Boost)
Install through your package manager, then let CMake find it:
find_package(Boost 1.71 REQUIRED COMPONENTS filesystem program_options)
target_link_libraries(my_app PRIVATE
Boost::filesystem
Boost::program_options
Boost::headers # header-only libs
)
Install commands per platform:
sudo apt install libboost-all-dev # Ubuntu/Debian (~500 MB; prefer per-component)
sudo dnf install boost-devel # Fedora/RHEL
sudo pacman -S boost boost-libs # Arch
brew install boost # macOS
Method 2: FetchContent (and why it bites)
FetchContent works, but Boost's modular CMake means you must declare component dependencies explicitly or you hit cryptic missing-target errors. It also compiles Boost as part of your build, which is slow. Good for reproducibility, bad for iteration speed.
Methods 3 & 4: vcpkg and Conan
Package managers give you pinned, reproducible Boost that plugs into CMake's find_package:
# vcpkg manifest mode: list boost in vcpkg.json, then configure with the toolchain file
cmake -B build -DCMAKE_TOOLCHAIN_FILE=.../vcpkg/scripts/buildsystems/vcpkg.cmake
Conan is the same idea with a conanfile.txt.
Method 5: Manual g++ linking
No build system, just the compiler:
g++ main.cpp -o app # header-only
g++ main.cpp -o app -lboost_filesystem # compiled lib
g++ main.cpp -o app -l:libboost_filesystem.a # static
Link order matters: dependents come before dependencies.
Which method should you use?
-
System
find_packagefor quick local builds. - vcpkg or Conan for reproducible, cross-platform projects.
- Build from source with b2 only when you need a specific version or custom variant.
For the complete walkthrough, including building from source with b2, Nix, Docker dev containers, static-linking details, and an FAQ: How to Install Boost in Any C++ Project.
Top comments (0)