CMake with Boost and policy CMP0167

Hi all,

I have seen various posts across the Internet about getting CMake to work with Boost 1.70+ and it seems like everyone has an easier time than I am. I think I must be doing something wrong.

So, I am using CMake 3.30.3 and Boost 1.85.0 under Ubuntu 24.10. Boost is compiled from source and not installed as Ubuntu packages. BOOST_ROOT has the value of /usr/local/boost, which is a symbolic link to /usr/local/boost_1_85_0/.

Removing various things I have tried, this is a minimum example, up to where I am stuck at:

if (POLICY CMP0167)
  cmake_policy (SET CMP0167 NEW)
endif ()

set (Boost_VERBOSE ON)
set (Boost_DEBUG ON)

set (BOOST_MIN_VERSION 1.85.0)
set (BOOST_REQUIRED_COMPONENTS program_options)

find_package (Boost ${BOOST_MIN_VERSION} REQUIRED COMPONENTS ${BOOST_COMPILED_LIBRARIES})

If I change the policy to OLD, then there aren’t any problems. When it is set to NEW, I get the following error:

CMake Error at cmake/boost.cmake:66 (find_package):
By not providing “FindBoost.cmake” in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by “Boost”, but
CMake did not find one.

Could not find a package configuration file provided by “Boost” (requested
version 1.85.0) with any of the following names:

BoostConfig.cmake
boost-config.cmake

Add the installation prefix of “Boost” to CMAKE_PREFIX_PATH or set
“Boost_DIR” to a directory containing one of the above files. If “Boost”
provides a separate development package or SDK, be sure it has been
installed.

My system doesn’t have a boost-config.cmake. But BoostConfig.cmake are in these locations:

$ locate BoostConfig.cmake
/usr/local/boost_1_85_0/tools/boost_install/BoostConfig.cmake
/usr/local/boost_1_85_0/tools/cmake/config/BoostConfig.cmake

I thought I should try setting Boost_DIR to what the error message is suggesting to me. So, if add set (Boost_DIR "/usr/local/boost_1_85_0/tools/cmake/config/"), then the error message becomes:

CMake Error at cmake/boost.cmake:66 (find_package):
Could not find a configuration file for package “Boost” that is compatible
with requested version “1.85.0”.

The following configuration files were considered but not accepted:

/usr/local/boost_1_85_0/tools/cmake/config/BoostConfig.cmake, version: unknown

I feel like I’m doing something wrong because:

  1. Others having similar problems to me don’t seem to set Boost_DIR. Instead, setting BOOST_ROOT as an shell environment variable was sufficient.
  2. Under the old way, I only had to set BOOST_ROOT and I thought the old way and the new way were interchangeable – i.e., without many changes to code.

The last error I received seems to indicate that it isn’t happy with the BoostConfig.cmake. I thought maybe there’s a problem with that file, but if there was, then others would have this problem. I also tried version 1.87.0 before this and it gave me the same problem. So, I’m fairly convinced it is my problem…

It is possible I installed Boost incorrectly, but if that were the case, then the OLD policy wouldn’t work.

Any suggestions would be appreciated! Thank you!

Ray

I can`t see any problems. How dit you build/install the boost libraries?

iMac:test clausklein$ cmake -S . -B build
-- Found Boost 1.87.0 at /usr/local/lib/cmake/Boost-1.87.0
--   Requested configuration: REQUIRED COMPONENTS program_options;filesystem
-- BoostConfig: find_package(boost_headers 1.87.0 EXACT CONFIG REQUIRED  HINTS /usr/local/lib/cmake)
-- Found boost_headers 1.87.0 at /usr/local/lib/cmake/boost_headers-1.87.0
-- BoostConfig: find_package(boost_program_options 1.87.0 EXACT CONFIG REQUIRED  HINTS /usr/local/lib/cmake)
-- Found boost_program_options 1.87.0 at /usr/local/lib/cmake/boost_program_options-1.87.0
-- Boost toolset is clang-darwin16 (AppleClang 16.0.0.16000026)
-- Scanning /usr/local/lib/cmake/boost_program_options-1.87.0/libboost_program_options-variant*.cmake
--   Including /usr/local/lib/cmake/boost_program_options-1.87.0/libboost_program_options-variant-shared.cmake
--   [x] libboost_program_options.dylib
--   Including /usr/local/lib/cmake/boost_program_options-1.87.0/libboost_program_options-variant-static.cmake
--   [ ] libboost_program_options.a
-- Adding boost_program_options dependencies: headers
-- BoostConfig: find_package(boost_filesystem 1.87.0 EXACT CONFIG REQUIRED  HINTS /usr/local/lib/cmake)
-- Found boost_filesystem 1.87.0 at /usr/local/lib/cmake/boost_filesystem-1.87.0
-- Boost toolset is clang-darwin16 (AppleClang 16.0.0.16000026)
-- Scanning /usr/local/lib/cmake/boost_filesystem-1.87.0/libboost_filesystem-variant*.cmake
--   Including /usr/local/lib/cmake/boost_filesystem-1.87.0/libboost_filesystem-variant-shared.cmake
--   [x] libboost_filesystem.dylib
--   Including /usr/local/lib/cmake/boost_filesystem-1.87.0/libboost_filesystem-variant-static.cmake
--   [ ] libboost_filesystem.a
-- Adding boost_filesystem dependencies: atomic;system;headers
-- Found boost_atomic 1.87.0 at /usr/local/lib/cmake/boost_atomic-1.87.0
-- Boost toolset is clang-darwin16 (AppleClang 16.0.0.16000026)
-- Scanning /usr/local/lib/cmake/boost_atomic-1.87.0/libboost_atomic-variant*.cmake
--   Including /usr/local/lib/cmake/boost_atomic-1.87.0/libboost_atomic-variant-shared.cmake
--   [x] libboost_atomic.dylib
--   Including /usr/local/lib/cmake/boost_atomic-1.87.0/libboost_atomic-variant-static.cmake
--   [ ] libboost_atomic.a
-- Adding boost_atomic dependencies: headers
-- Found boost_system 1.87.0 at /usr/local/lib/cmake/boost_system-1.87.0
-- Boost toolset is clang-darwin16 (AppleClang 16.0.0.16000026)
-- Scanning /usr/local/lib/cmake/boost_system-1.87.0/libboost_system-variant*.cmake
--   Including /usr/local/lib/cmake/boost_system-1.87.0/libboost_system-variant-shared.cmake
--   [x] libboost_system.dylib
--   Including /usr/local/lib/cmake/boost_system-1.87.0/libboost_system-variant-static.cmake
--   [ ] libboost_system.a
-- Adding boost_system dependencies: headers
-- Configuring done (0.5s)
-- Generating done (0.0s)
-- Build files have been written to: /Users/clausklein/cmake/test/build
iMac:test clausklein$ 

Hi Claus,

Thanks for the follow-up! I built Boost using b2, which I think are still the main method of building it.

Did you build it with CMake using these instructions: https://github.com/boostorg/cmake (I just found it with Google)? Or something else?

It would be great if I could build and install it with CMake, so that I don’t have to mess with b2

Thank you!

Ray

It is not so easy as it should!

My boost is installed with brew.

Try this first GitHub - ClausKlein/boost-cmake: Easy Boost integration in CMake projects

See too the original make-build cmake build docs

But to use is, do not clone the git repo, download cmake archive from GitHub

Hi @ClausKlein,

Thank you for your reply and the link to your repository! I’ve been playing with it (as well as other things), so sorry for the delay!

I almost got it working. And indeed, if it works, it is better than what I was struggling with before, so thank you!

I have this so far, which I’ve obviously copied and changed slightly from your GitHub example:

include (GNUInstallDirs)

set (FETCHCONTENT_SOURCE_DIR_BOOST /usr/local/boost_1_87_0/)
add_subdirectory (${PROJECT_SOURCE_DIR}/cmake/boost-cmake)

set (CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)

option (BUILD_SHARED_LIBS "Build shared libraries" NO)
set (BOOST_INCLUDE_LIBRARIES program_options filesystem headers)
target_include_directories(
  foo PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
  $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)

target_link_libraries (foo Boost::filesystem)
target_link_libraries (foo Boost::program_options)

During the execution of cmake, it is complaining it doesn’t know about Boost::filesystem nor Boost::program_options.

I’ve tried other values, including boost_filesystem, libboost_filesystem, and just filesystem. cmake gets further, but then the problem occurs during linking.

I suppose previously, I “cheated” by just using the values provided by find_package ():

link_directories (${Boost_LIBRARY_DIRS})
target_include_directories (foo PUBLIC "${Boost_INCLUDE_DIRS}")
target_link_libraries (foo ${Boost_LIBRARIES})

What should I be using or have I done something incorrectly? (Also, if I used your default code example and automatically download from https://archive.boost.io/release instead of fetching from a local directory, the result is the same.)

Thank you!

Ray

see boost-cmake/examples/CMakeLists.txt at refs/heads/feature/bump-to-boost-v1.87.0 · ClausKlein/boost-cmake · GitHub

Hi @ClausKlein,

Sorry to disappear for a few days! While working on this, I realised I had serious problems with my many CMakeLists.txt. I had to fix up those problems as well.

Thank you for providing the link to that file in your repository. It took me some time to figure out what I was doing wrong, but I managed to get it all working.

Downloading and setting up Boost wasn’t an option that I thought was possible (I haven’t been following discussions around this in recent years). Thank you for showing this to me!

Ray