In Conan 2, how do CMakeDeps, CMakeToolchain and the generated conan-release preset hand fmt and zlib to a CMake project?
answer
- two generators, two jobs
- config packages versus toolchain file
- names come from recipe properties
- preset carries toolchain and build type
basics
~10 sCMakeDeps writes a config package per dependency declaring targets such as fmt::fmt; CMakeToolchain writes conan_toolchain.cmake and a conan-release preset that loads it and sets CMAKE_BUILD_TYPE, so plain find_package and target_link_libraries work.
solid answer
~30 sThe two generators split the work. `CMakeDeps` writes, for each dependency, a config package (`fmt-config.cmake` plus target files) that declares an imported target: `fmt::fmt` by default, or whatever the recipe sets with the `cmake_file_name` and `cmake_target_name` properties. `CMakeToolchain` writes `conan_toolchain.cmake`, which sets the C++ standard from `compiler.cppstd`, adds flags such as `-stdlib=libc++`, prepends the generators folder to `CMAKE_PREFIX_PATH` and turns on `CMAKE_FIND_PACKAGE_PREFER_CONFIG`. It also writes `CMakePresets.json`, whose `conan-release` configure preset names the generator, the toolchain file, the binary directory and `CMAKE_BUILD_TYPE=Release` in `cacheVariables`. The `CMakeLists.txt` then needs only `find_package(fmt)`, `find_package(ZLIB)` and `target_link_libraries`, and `cmake --preset conan-release` wires it all up.
code
cmake · 8 linescmake_minimum_required(VERSION 3.23)
project(app CXX)
find_package(fmt REQUIRED)
find_package(ZLIB REQUIRED)
add_executable(app main.cpp)
target_link_libraries(app PRIVATE fmt::fmt ZLIB::ZLIB)go deeper
Remember which generator writes what: CMakeDeps writes the dependency config packages, CMakeToolchain writes the toolchain file and presets.
Walk through the preset fields and the toolchain blocks, and explain why the CMakeLists.txt needs nothing but find_package and target_link_libraries.
Diagnose name mismatches from the cmake_file_name and cmake_target_name properties, and standard mismatches from the toolchain's variable_watch warning.
Defend keeping CMakeLists.txt free of Conan specifics so the build stays portable to other package sources.
## Two generators, two jobs In Conan 2 a **generator** is a piece of code that turns the resolved dependency graph and the profile into files a build system reads. A CMake project normally uses two of them, either listed under `[generators]` in a conanfile.txt or instantiated in the `generate()` method of a conanfile.py. | Generator | Answers the question | Main output | |---|---|---| | `CMakeDeps` | where are my dependencies and what are their targets? | one config package per dependency | | `CMakeToolchain` | which compiler settings and search paths should CMake use? | `conan_toolchain.cmake`, `CMakePresets.json` | ## What CMakeDeps writes For each dependency in the host context, CMakeDeps writes a **config package**: a `<name>-config.cmake` or `<Name>Config.cmake` file, a version file, a targets file and a per-configuration data file such as `fmt-release-armv8-data.cmake`. Loading it declares an **imported target** carrying the include directories, libraries and compile definitions of the package. - The default file name is the package name and the default target is `<name>::<name>`, so a recipe for fmt that sets nothing yields `find_package(fmt)` and `fmt::fmt`. - A recipe can rename both in `package_info()` with `self.cpp_info.set_property("cmake_file_name", ...)` and `set_property("cmake_target_name", ...)`. In the zlib recipe used in the example below these are `ZLIB` and `ZLIB::ZLIB`, so consumers write `find_package(ZLIB)`. - `conan install` prints the exact names at the end, which beats guessing. - Conan 2.33 also ships a newer generator, `CMakeConfigDeps`, which it marks as experimental. ## What CMakeToolchain writes `conan_toolchain.cmake` translates the profile into CMake variables. Reading the generated file shows blocks such as: - `CMAKE_CXX_STANDARD`, `CMAKE_CXX_EXTENSIONS` and `CMAKE_CXX_STANDARD_REQUIRED` from `compiler.cppstd` (`gnu17` becomes standard 17 with extensions ON), plus a `variable_watch` that prints a warning if a `CMakeLists.txt` later changes the standard; - the standard library flag from `compiler.libcxx`, for example `-stdlib=libc++`; - `CMAKE_FIND_PACKAGE_PREFER_CONFIG ON`, so the CMakeDeps config packages win over CMake's own Find modules; - the generators folder prepended to `CMAKE_PREFIX_PATH` and `CMAKE_MODULE_PATH`. For a single-config generator the toolchain file does **not** set `CMAKE_BUILD_TYPE`; that comes from the preset or the command line. ## The preset that ties them together CMakeToolchain also writes `CMakePresets.json` in the generators folder. Its `conan-release` configure preset records: 1. `generator`, such as `Unix Makefiles` or the value of `tools.cmake.cmaketoolchain:generator`; 2. `toolchainFile`, pointing at `conan_toolchain.cmake`; 3. `binaryDir`, the build folder chosen by the layout; 4. `cacheVariables` with `CMAKE_BUILD_TYPE` and `CMAKE_POLICY_DEFAULT_CMP0091`. Matching build and test presets share the name. When a `CMakeLists.txt` sits next to the conanfile, Conan writes a `CMakeUserPresets.json` there whose `include` list points at the generated file, so `cmake --preset conan-release` works from the project root. Preset `include` needs CMake 3.23; for older CMake, Conan prints the equivalent `-DCMAKE_TOOLCHAIN_FILE=... -DCMAKE_BUILD_TYPE=Release` command line. ## Debug and Release side by side Each `conan install` writes presets for one build type. Running it again with `-s build_type=Debug` adds a `conan-debug` preset in `build/Debug/generators`, and Conan appends that file to the `include` list of the same `CMakeUserPresets.json`, so `cmake --list-presets` shows both `conan-release` and `conan-debug`. A preset that was never installed does not exist, which is why `cmake --preset conan-debug` fails on a fresh checkout until the Debug install has run. ## Common failure modes - `find_package(fmt)` fails: CMake was configured without the toolchain file, so the generators folder never reached `CMAKE_PREFIX_PATH`. - `find_package(zlib)` fails while `find_package(ZLIB)` would work: the recipe renamed the file; copy the names conan install printed. - The project builds against Release libraries in a Debug build: the Debug install was skipped, so only Release data files exist. ## The CMake side stays plain The project's `CMakeLists.txt` names no Conan variable at all, which is the point of the design: the same file builds with any other way of providing `fmt` and `ZLIB` packages. How `find_package` searches and how imported targets propagate usage requirements belong to the CMake material on package discovery and targets. The Conan 1 `cmake` generator, with its `conanbuildinfo.cmake` and `CONAN_PKG::` targets, is the pattern this replaced.
- What happens if CMakeLists.txt sets CMAKE_CXX_STANDARD to 20 while the profile says gnu17?The CMakeLists value wins for your project, but `conan_toolchain.cmake` watches the variable and prints a warning that the standard it defined was modified. The dependencies were still built for the profile's `compiler.cppstd`, so the profile is the place to change it.
- How do you build with CMake older than 3.23, which cannot read the include in CMakeUserPresets.json?Use the command line Conan prints after install: `cmake <path> -G "Unix Makefiles" -DCMAKE_TOOLCHAIN_FILE=<generators>/conan_toolchain.cmake -DCMAKE_POLICY_DEFAULT_CMP0091=NEW -DCMAKE_BUILD_TYPE=Release`, then `cmake --build`.
- The CMakeLists.txt must include a Conan-generated file before find_package works.
- conan_toolchain.cmake sets CMAKE_BUILD_TYPE for single-config generators.
- CMakeDeps always names the target after the package, whatever the recipe says.
- CMakeUserPresets.json is written even when no CMakeLists.txt is next to the conanfile.
- CMakeToolchain links the dependency libraries into my targets.