Using Boost with CMake

I’m curious to hear common setup of Boost with CMake by the community.

I used to only be aware of FindBoost but have recently learned of some more options. I’d be interested to hear pros/cons people have run into.

  • FindBoost
    • The CMake documents using Boost through FindBoost. This has been adequate on my project so far but does require something else (package manager, containers, scripts) to manage the installation of Boost. Furthermore, this option can pose challenging for embedded projects that need to build Boost libraries for multiple platforms.
  • Github Boost Project CMake File
    • I recently discovevered there is a top level CMakeLists.txt in the Boost Github Project which is not included in Boost releases on the Boost Website (or from apt install). However, this Infrastructure does setup more granular targets (like Boost::signals2 instead of Boost::headers) and provide the ability to build compile-necessary targets too (like filesystem). That infrastructure does warn in their README that building Boost with CMake does not work yet and is not supported. However, despite “does not work yet”, I have been able to successfully cross-compile some targets (filesystem) using this CMake setup. Yet even for header only usage, this does provide a more granular way to access Boost. Furthermore, this lets Boost be used with FetchContent or CPM allowing us to manage Boost more directly through CMake. It is important to note though, when this option is used, it does appear that the find_package(Boost) is not supported. Which can create some inconsistent use of Boost between CMake projects.
  • Other options?

I’m curious to hear about others experience with any of these options or new options? Have others succesfully used the CMake Infrastructure present in the Boost Github Project. Does anyone know reasons why Boost still claims it experimental (I might ping their mailing lists). Have others been found a more effective way to manage cross platform builds of Boost with CMake?

I’ve used the official CMakeLists.txt of Boost (via CPM), and for the most part it works fine. I think they still label it as experimental because there are not sure if they’ve covered all the edge cases. I’ve not faced many, but for instance, I had some issues with some of the preprocessor flags here and there, as well as some failed compilations. At the end though, I ended up using Conan for dependency manager so that I don’t have to deal with all the cross-platform build issues as you noted.

If you want to get a consistent result, I would recommend using Conan. It’s the most stable, and effective way that I found dealing with Boost.

One of the most flexible and fasted way I know is to use this Cmake Project

I may be used with CPM.cmake like in this example: