Is there a way to download and include just a header from Boost?

I am using boost::dynamic_bitset in one of my projects.

Is there a way to tell CMake to just download that header and have it available, without 1) having to download the whole of Boost, and 2) having to compile anything (I’m not going to link against anything)?

Then, later, I would target_include_directories(my_target PRIVATE ${Boost_INCLUDE_HEADERS}).

Many thanks @ClausKlein.

Just a confirmation, because I don’t really see it in those threads. Both with CPM and FetchContent with Boost you always have to download the whole of Boost, right?

Yes, at least the source distribution without root CMakeLists.txt

Most boost libs depend on other boost libs!
The boost::dynamic_bitset is not a standalone git submodule like boost::asio!

bash-5.2$ head -25 *.hpp */*.hpp | grep -w include | sort -u | grep -w boost
#include "boost/assert.hpp"
#include "boost/config.hpp"
#include "boost/detail/workaround.hpp"
#include "boost/dynamic_bitset/dynamic_bitset.hpp"
#include "boost/integer/integer_log2.hpp"
#include <boost/core/allocator_access.hpp>
#include <boost/core/nvp.hpp>
bash-5.2$ 

Ah, OK, I understand. Thanks for the explanation @ClausKlein.

In that case, I will probably stick to what I’m doing now, that is to:

  • request boost/1.85.0 via Conan,
  • indicating that I only want headers (option boost/*:header_only=True), i.e., that I don’t want to compile boost and link against anything, and
  • add Boost_INCLUDE_DIRS to my target’s include directories.

So this in conanfile.py:

    def requirements(self):
        self.requires("boost/1.85.0")

This in a conan profile:

[options]
boost/*:header_only=True

Then this in CMakeLists.txt:

find_package(Boost REQUIRED)
target_include_directories(my_target PUBLIC
    "${Boost_INCLUDE_DIRS}"
)