c++cmakeboostclionconan

Unable to use Boost 1.87.0 with Conan + CMake + CLion


I'm trying to add the Boost library to my project. I'm using CLion for C++, and CMake, while also having a requirement of using Conan.

I've tried both Conan with CLI and the Conan Plugin, but both fail for me when trying to add Boost.

Here is a repro of that I have:

  1. I've create the conanfile.py as follows:
from conan import ConanFile
from conan.tools.cmake import cmake_layout

class ExampleRecipe(ConanFile):
    settings = "os", "compiler", "build_type", "arch"
    generators = "CMakeDeps", "CMakeToolchain"

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

    def layout(self):
        cmake_layout(self)

(important note that I don't have Python interpreter configured yet, IDK if it is related)

  1. I ran the next command:
conan install . --output-folder=build --build=missing

This is the result:

======== Input profiles ========
Profile host:
[settings]
arch=x86_64
build_type=Release
compiler=msvc
compiler.cppstd=14
compiler.runtime=dynamic
compiler.runtime_type=Release
compiler.version=194
os=Windows

Profile build:
[settings]
arch=x86_64
build_type=Release
compiler=msvc
compiler.cppstd=14
compiler.runtime=dynamic
compiler.runtime_type=Release
compiler.version=194
os=Windows


======== Computing dependency graph ========
Graph root
    conanfile.py: D:\src\ConanBoost\conanfile.py
Requirements
    boost/1.87.0#0c087f18c4e6487235dd10480613cbb5 - Cache
    bzip2/1.0.8#00b4a4658791c1f06914e087f0e792f5 - Cache
    zlib/1.3.1#b8bc2603263cf7eccbd6e17e66b0ed76 - Cache
Build requirements
    b2/5.3.2#7b5fabfe7088ae933fb3e78302343ea0 - Cache
Resolved version ranges
    b2/[>=5.2 <6]: b2/5.3.2
    zlib/[>=1.2.11 <2]: zlib/1.3.1

======== Computing necessary packages ========
Connecting to remote 'conancenter' anonymously
bzip2/1.0.8: Main binary package '885590b8d4960dd9be0d7cd4c17646ce20f8a9aa' missing
bzip2/1.0.8: Checking 1 compatible configurations
bzip2/1.0.8: Found compatible package '67bfcb7b4b78262b9d05495e479dcd92f747316b': compiler.version=193
zlib/1.3.1: Main binary package '0d6dd492a7d31822b2f2686ec67bbaef586416a3' missing
zlib/1.3.1: Checking 1 compatible configurations
zlib/1.3.1: Found compatible package '7bfde258ff4f62f75668d0896dbddedaa7480a0f': compiler.version=193
boost/1.87.0: Main binary package 'bbe666acc775075687a336c0614956d959da514b' missing
boost/1.87.0: Checking 7 compatible configurations
boost/1.87.0: Found compatible package '690d27cb1e8da3ebe38cd3de075871cff714299d': compiler.version=193
Requirements
    boost/1.87.0#0c087f18c4e6487235dd10480613cbb5:690d27cb1e8da3ebe38cd3de075871cff714299d#4fd7afbcbe62cad0959f7646bee0f78e - Cache
    bzip2/1.0.8#00b4a4658791c1f06914e087f0e792f5:67bfcb7b4b78262b9d05495e479dcd92f747316b#c04f63d499585a336129d85d111eed24 - Cache
    zlib/1.3.1#b8bc2603263cf7eccbd6e17e66b0ed76:7bfde258ff4f62f75668d0896dbddedaa7480a0f#79ec8125ebd42591897da4cf270e6146 - Cache
Build requirements
Skipped binaries
    b2/5.3.2

======== Installing packages ========
bzip2/1.0.8: Already installed! (1 of 3)
zlib/1.3.1: Already installed! (2 of 3)
boost/1.87.0: Already installed! (3 of 3)
boost/1.87.0: Disabled magic autolinking (smart and magic decisions)
WARN: deprecated: Usage of deprecated Conan 1.X features that will be removed in Conan 2.X:
WARN: deprecated:     'cpp_info.names' used in: bzip2/1.0.8, zlib/1.3.1, boost/1.87.0
WARN: deprecated:     'cpp_info.build_modules' used in: bzip2/1.0.8
WARN: deprecated:     'env_info' used in: bzip2/1.0.8, boost/1.87.0
WARN: deprecated:     'cpp_info.filenames' used in: boost/1.87.0
WARN: deprecated:     'user_info' used in: boost/1.87.0

======== Finalizing install (deploy, generators) ========
conanfile.py: Writing generators to D:\src\ConanBoost\build\build\generators
conanfile.py: Generator 'CMakeDeps' calling 'generate()'
conanfile.py: CMakeDeps necessary find_package() and targets for your CMakeLists.txt
    find_package(Boost)
    target_link_libraries(... boost::boost)
conanfile.py: Generator 'CMakeToolchain' calling 'generate()'
conanfile.py: CMakeToolchain generated: conan_toolchain.cmake
conanfile.py: CMakeToolchain: Preset 'conan-default' added to CMakePresets.json.
    (cmake>=3.23) cmake --preset conan-default
    (cmake<3.23) cmake <path> -G "Visual Studio 17 2022" -DCMAKE_TOOLCHAIN_FILE=generators\conan_toolchain.cmake  -DCMAKE_POLICY_DEFAULT_CMP0091=NEW
conanfile.py: CMakeToolchain generated: D:\src\ConanBoost\build\build\generators\CMakePresets.json
conanfile.py: CMakeToolchain generated: D:\src\ConanBoost\CMakeUserPresets.json
conanfile.py: Generating aggregated env files
conanfile.py: Generated aggregated env files: ['conanbuild.bat', 'conanrun.bat']
Install finished successfully
  1. I've update CMake to look like this:
cmake_minimum_required(VERSION 3.31)
project(ConanBoost)
set(CMAKE_CXX_STANDARD 20)

message(STATUS "Boost_DIR: ${Boost_DIR}")

find_package(Boost)

add_executable(ConanBoost main.cpp)

And I finally build, sometimes it succeeds sometimes fails, but if I add REQUIRED then it always fails with the next error:

CMake Error at CMakeLists.txt:7 (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" 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.


-- Configuring incomplete, errors occurred!

[Finished]

I'm new to C++, CMake and everything so I don't know all the internals... I figured so far that due to a change in Boost we no longer have FindBoost.cmake generated but BoostConfig.cmake. Changing the CMake with find_package(Boost REQUIRED CONFIG) yields a different but similar error:

[0/1] Re-running CMake...
-- Configuring incomplete, errors occurred!
FAILED: build.ninja 
"C:\Program Files\JetBrains\CLion 2025.1.1\bin\cmake\win\x64\bin\cmake.exe" --regenerate-during-build -SD:\src\ConanBoost -BD:\src\ConanBoost\cmake-build-debug
CMake Error at CMakeLists.txt:7 (find_package):
  Could not find a package configuration file provided by "Boost" 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.


ninja: error: rebuilding 'build.ninja': subcommand failed

Also at some point I noticed that there was a message indicated that Boost_DIR isn't set properly, and indeed if I add message(STATUS "Boost_DIR: ${Boost_DIR}") the result I see is this -- Boost_DIR: Boost_DIR-NOTFOUND

Would appreciate any help in making this work


Solution

  • Okay I've managed to make it work.
    So, once we add conan and get the Boost libraries we get an issue where conan tries to always set the next variables:

    CMAKE_GENERATOR_PLATFORM, CMAKE_GENERATOR_TOOLSET

    Which will cause ninja to fail. Those two are added in the conan_toolchain.cmake and unfortunately, I don't know how to properly fix conan so it doesn't add them.
    So for now the solution and workaround I've found is that each time I run conan install I have to go to the toolchain file, remove the next lines:

    set(CMAKE_GENERATOR_PLATFORM "x64" CACHE STRING "" FORCE)
       
    message(STATUS "Conan toolchain: CMAKE_GENERATOR_TOOLSET=v143")
    set(CMAKE_GENERATOR_TOOLSET "v143" CACHE STRING "" FORCE)
    

    Then go to `CMakeCache.txt` and remove the next lines:

        CMAKE_GENERATOR_PLATFORM:STRING=x64
        
        CMAKE_GENERATOR_TOOLSET:STRING=v143
    

    After that ninja should fail, and the package can be downloaded currectly.
    The final CmakeLists.txt looks like this:

    cmake_minimum_required(VERSION 3.31)
    project(ConanBoost)
    set(CMAKE_CXX_STANDARD 20)
    
    include(${CMAKE_CURRENT_SOURCE_DIR}/build/build/generators/conan_toolchain.cmake)
    
    find_package(Boost REQUIRED)
    
    add_executable(ConanBoost main.cpp)
    
    target_include_directories(ConanBoost PRIVATE ${Boost_INCLUDE_DIRS})
    target_link_libraries(ConanBoost PRIVATE ${Boost_LIBRARIES})