CMakeDeps, CMakeToolchain & Presets
Conan doesn't build your project; CMake does. Conan's job ends with two generators that tell CMake where the packages are and how to compile against them. Knowing what they write makes most "it can't find my package" problems easy to fix. CMake basics such as find_package, toolchain files and presets are covered in Modern CMake.
CMakeDeps: one config package per dependency
CMakeDeps writes a CMake config package for every dependency, the same kind of file a library installs itself (<Name>Config.cmake or <name>-config.cmake), so your find_package(fmt CONFIG) works unchanged. The target names come from the recipe's package_info(), which mirrors the upstream project's names (fmt::fmt, spdlog::spdlog, Catch2::Catch2WithMain).
CMakeToolchain: settings become CMake variables
CMakeToolchain writes conan_toolchain.cmake, a toolchain file that turns the profile into CMake: the C++ standard and extensions (gnu17 becomes standard 17 with extensions on), architecture and compiler flags, the search paths that point find_package at the CMakeDeps files, and, for packages that have the option, position-independent code from fPIC. It also writes presets, which carry the build type (CMAKE_BUILD_TYPE for single-config generators) and the toolchain path, so you don't have to type them.
Here are both, generated for a fmt consumer:
mkdir -p demo && cd demo
cat > conanfile.txt <<'TXT'
[requires]
fmt/12.1.0
[generators]
CMakeDeps
CMakeToolchain
[layout]
cmake_layout
TXT
conan install . -c tools.build:jobs=1 > /dev/null 2>&1
ls build/Release/generators
echo "--- what the toolchain sets"
grep -E '^set\((CMAKE_CXX_STANDARD|CMAKE_CXX_EXTENSIONS|CMAKE_BUILD_TYPE|CMAKE_POSITION)' build/Release/generators/conan_toolchain.cmake
echo "--- the target CMakeDeps defines"
grep -o 'add_library(fmt::fmt [A-Z]*' build/Release/generators/*.cmake | sort -uCMakePresets.json
cmakedeps_macros.cmake
conan_toolchain.cmake
conanbuild.sh
conanbuildenv-release-x86_64.sh
conandeps_legacy.cmake
conanrun.sh
conanrunenv-release-x86_64.sh
deactivate_conanbuild.sh
deactivate_conanrun.sh
fmt-Target-release.cmake
fmt-config-version.cmake
fmt-config.cmake
fmt-release-x86_64-data.cmake
fmtTargets.cmake
--- what the toolchain sets
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_EXTENSIONS ON)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
--- the target CMakeDeps defines
build/Release/generators/fmtTargets.cmake:add_library(fmt::fmt INTERFACE
The -data.cmake and -Target-release.cmake files hold the paths for one configuration. Install again with -s build_type=Debug and CMakeDeps adds -debug files next to them, which is how one build folder can serve multi-config generators (Visual Studio, Ninja Multi-Config).
The presets
CMakeToolchain writes CMakePresets.json into the generators folder and a CMakeUserPresets.json in your project that includes it. That's where --preset conan-release comes from:
mkdir -p demo && cd demo
cat > conanfile.txt <<'TXT'
[requires]
fmt/12.1.0
[generators]
CMakeDeps
CMakeToolchain
[layout]
cmake_layout
TXT
printf 'cmake_minimum_required(VERSION 3.25)\nproject(demo CXX)\n' > CMakeLists.txt
conan install . -c tools.build:jobs=1 > /dev/null 2>&1
cat CMakeUserPresets.json; echo
python3 -c "
import json
p = json.load(open('build/Release/generators/CMakePresets.json'))
for kind in ('configurePresets', 'buildPresets', 'testPresets'):
for preset in p[kind]:
extra = {k: preset[k] for k in ('generator', 'toolchainFile', 'jobs', 'execution') if k in preset}
print(kind, preset['name'], extra)
"{
"version": 4,
"vendor": {
"conan": {}
},
"include": [
"build/Release/generators/CMakePresets.json"
]
}
configurePresets conan-release {'generator': 'Unix Makefiles', 'toolchainFile': 'generators/conan_toolchain.cmake'}
buildPresets conan-release {'jobs': 1}
testPresets conan-release {'execution': {'jobs': 1}}
The build and test presets carry the job count from tools.build:jobs (we pinned it to 1; by default it is the number of CPUs). If your project already has its own CMakePresets.json, Conan's CMakeUserPresets.json sits alongside it, and your presets can inherit from conan-release.
Without presets
Presets need CMake 3.23 or newer (our image has 4.4). With an older CMake, or in a script that doesn't want presets, pass the toolchain yourself, exactly as the conan install output suggests:
cmake -S . -B build/Release -DCMAKE_TOOLCHAIN_FILE=build/Release/generators/conan_toolchain.cmake \
-DCMAKE_BUILD_TYPE=Release
cmake --build build/ReleaseChoosing the generator
The presets use whatever tools.cmake.cmaketoolchain:generator says, defaulting to Unix Makefiles on Linux and the newest Visual Studio on Windows. Ninja is a good choice everywhere:
mkdir -p demo && cd demo
cat > conanfile.txt <<'TXT'
[requires]
fmt/12.1.0
[generators]
CMakeDeps
CMakeToolchain
[layout]
cmake_layout
TXT
cat > CMakeLists.txt <<'TXT'
cmake_minimum_required(VERSION 3.25)
project(demo CXX)
find_package(fmt REQUIRED CONFIG)
add_executable(demo main.cpp)
target_link_libraries(demo PRIVATE fmt::fmt)
TXT
cat > main.cpp <<'SRC'
#include <fmt/ranges.h>
#include <vector>
int main() { fmt::print("{}\n", std::vector<int>{1, 2, 3}); }
SRC
conan install . -c tools.build:jobs=1 -c tools.cmake.cmaketoolchain:generator=Ninja > /dev/null 2>&1
cmake --preset conan-release | grep -E 'Using Conan|Build files'
cmake --build --preset conan-release
./build/Release/demo-- Using Conan toolchain: /home/learner/demo/build/Release/generators/conan_toolchain.cmake
-- Build files have been written to: /home/learner/demo/build/Release
[1/2] Building CXX object CMakeFiles/demo.dir/main.cpp.o
[2/2] Linking CXX executable demo
[1, 2, 3]
Put the generator in your profile's [conf] section rather than on the command line, so every developer gets the same one.
Typical problems
| Symptom | Usual cause |
|---|---|
Could not find a package configuration file provided by "fmt" |
configured without the Conan toolchain (forgot --preset or -DCMAKE_TOOLCHAIN_FILE) |
Could not find a configuration file for package "fmt" ... requested version |
find_package(fmt 13) but the graph has 12.x; fix the requirement or the CMake version check |
target "fmt::fmt" not found |
wrong target name; read the CMakeDeps necessary find_package() and targets lines |
| Debug app links Release libraries | installed only Release; run conan install once per build type |
Test yourself
What does CMakeDeps generate?
- A Makefile
- A CMake config package (xxx-config.cmake + targets) for each dependency
- A toolchain file
- A lockfile
Where does the conan-release preset come from?
- CMake defines it
- CMakeToolchain writes CMakePresets.json and a CMakeUserPresets.json that includes it
- You must write it by hand
- The default profile
How do you make Conan's presets use Ninja?
cmake -G Ninjaafter install- Set the conf
tools.cmake.cmaketoolchain:generator=Ninja(profile or -c) - Add Ninja to [requires]
- It can't