skip to content

Recipes and Generators

A conanfile.txt or conanfile.py declares requirements, and CMakeDeps and CMakeToolchain hand them to CMake. Interviewers ask what conan install produces and how CMake consumes it.

on this pageshow

explore

questions

5

In Conan 2, what does conan install produce for a conanfile.txt listing fmt and zlib with the CMakeDeps and CMakeToolchain generators?

level: juniorimportance: must knowfreq 62%

answer

  1. resolve the graph first
  2. binaries land in the local cache
  3. one config package per dependency
  4. toolchain file plus presets
  5. your own code is not built

basics

~10 s

It resolves the dependency graph, puts matching fmt and zlib binaries in the local cache and writes CMake input files: a config package per dependency, conan_toolchain.cmake and CMakePresets.json. It never compiles your own project.

solid answer

~40 s

`conan install .` reads the `conanfile.txt`, resolves `fmt` and `zlib` to exact references, and for each one finds a binary whose package ID matches the profile, first in the local cache and then in the remotes. If none matches it stops with a missing-binary error unless you pass `--build`. Then the generators run: `CMakeDeps` writes a config package per dependency (`fmt-config.cmake`, `ZLIBConfig.cmake` and their target files), and `CMakeToolchain` writes `conan_toolchain.cmake` and a `CMakePresets.json` with a `conan-release` preset, plus a `CMakeUserPresets.json` that includes it when a `CMakeLists.txt` sits beside the conanfile. With `[layout] cmake_layout` they go to `build/Release/generators`. It also writes `conanbuild.sh` and `conanrun.sh` environment scripts. Your own project is still unbuilt: `cmake --preset conan-release` and `cmake --build --preset conan-release` do that.

code

ini · 10 lines
ini
[requires]
fmt/11.0.2
zlib/1.3.1

[generators]
CMakeDeps
CMakeToolchain

[layout]
cmake_layout

go deeper

for a junior

Recall the order: resolve, find binaries, generate files. Name the three files CMake needs: the per-dependency config packages, conan_toolchain.cmake and the preset.

for a middle

Explain where the files land with and without cmake_layout, why the toolchain file is what makes find_package succeed, and when CMakeUserPresets.json appears.

for a senior

Show you can read the install output: the resolved references, the package IDs, Cache versus Download versus Missing, and the printed find_package names you copy into CMake.

for a principal

Argue where the boundary sits: Conan prepares inputs and CMake builds, which keeps a project buildable by developers who only know CMake presets.

## The input A **conanfile.txt** is the declarative way for a consumer project to tell Conan 2 what it needs. For an application that uses the fmt formatting library and zlib compression, built with CMake, it looks like this: ```ini [requires] fmt/11.0.2 zlib/1.3.1 [generators] CMakeDeps CMakeToolchain [layout] cmake_layout ``` `conan install .` is run from the folder that holds it. The command belongs to the **consumer** side of Conan: it prepares everything the build needs and then stops. ## What conan install does, step by step 1. **Loads the profiles.** The host profile (by default the `default` profile in the Conan home) supplies settings such as compiler, compiler version, `compiler.cppstd`, `build_type` and `arch`. 2. **Resolves the graph.** Each requirement becomes an exact reference with a recipe revision, looked up in the local cache first and then in the configured remotes. 3. **Computes a package ID** for every dependency from its settings, options and dependencies, and looks for a binary with that ID, again cache first, then remotes. 4. **Stops on a missing binary.** If no matching binary exists, the command fails with `ERROR: Missing prebuilt package` and suggests building that reference from source with `--build`. It never silently compiles a dependency. 5. **Runs the generators** and writes the files listed below. ## The files it writes With `cmake_layout`, a single-config generator such as Unix Makefiles or Ninja and `build_type=Release`, everything goes to `build/Release/generators`: | File | Written by | Purpose | |---|---|---| | `fmt-config.cmake`, `fmtTargets.cmake`, `fmt-Target-release.cmake` | CMakeDeps | config package that `find_package(fmt)` loads and that declares `fmt::fmt` | | `ZLIBConfig.cmake` and friends | CMakeDeps | the same for zlib, under the names its recipe declares | | `conan_toolchain.cmake` | CMakeToolchain | compiler-related variables, the C++ standard, search paths | | `CMakePresets.json` | CMakeToolchain | a `conan-release` configure, build and test preset | | `conanbuild.sh`, `conanrun.sh` | environment generators | build-time and run-time environment scripts | - A `CMakeUserPresets.json` that includes the generated presets is written next to the conanfile, but only when a `CMakeLists.txt` sits in that same folder. - Without a `[layout]` section the files land in the folder given by `-of`/`--output-folder`, or in the current folder. - With a multi-config generator (Visual Studio, Xcode, Ninja Multi-Config) `cmake_layout` drops the build-type level and uses `build/generators`. At the end, CMakeDeps prints the exact `find_package()` names and targets to use, which is the fastest way to learn what a recipe declares. ## How CMake consumes them Your `CMakeLists.txt` stays ordinary CMake: `find_package(fmt REQUIRED)`, `find_package(ZLIB REQUIRED)` and `target_link_libraries(app PRIVATE fmt::fmt ZLIB::ZLIB)`. The toolchain file prepends the generators folder to `CMAKE_PREFIX_PATH` and `CMAKE_MODULE_PATH`, so `find_package` finds the CMakeDeps files. With CMake 3.23 or newer, `cmake --preset conan-release` passes the toolchain file and `CMAKE_BUILD_TYPE` in one step; `cmake --build --preset conan-release` then builds. How `find_package` searches is CMake's own subject, covered with package discovery in the CMake material. ## Reading the install output The command prints its decisions before it writes anything, and reading them is most of debugging an install: - **Requirements** lists each dependency as `name/version#recipe_revision:package_id#package_revision`, followed by where the binary comes from. - `Cache` means the binary was already in the local cache; `Download` means it is fetched from a remote. - `Missing` means no binary with that package ID exists anywhere Conan looked, which ends the command unless `--build` covers that package. - `Build` means the package will be compiled from source, because a `--build` argument or the create command itself selected it. - **Build requirements**, when present, lists tool requirements separately, because they are resolved for the build machine. When the same project installs instantly on one machine and fails with `Missing` on another, the two machines almost always differ in a profile setting, which the profile material explains. ## What it does not do - It does **not** build your project; that is CMake's job, or `conan build .` for a `conanfile.py` that has a `build()` method. - It does **not** put your project into the cache; packaging is `conan create`. - It does **not** write files into the cache next to the packages: generated files live in your build tree. - The Conan 1 `cmake` generator and its `conanbuildinfo.cmake` are gone in Conan 2; CMakeDeps plus CMakeToolchain replace them.

  • Where do the generated files go if the conanfile.txt has no [layout] section?
    Into the folder named by `-of`/`--output-folder`, or into the current folder when that is omitted. That is why `[layout] cmake_layout` is worth adding: it puts them in `build/Release/generators` (or `build/generators` for a multi-config generator) and keeps the source tree clean.
  • Your colleague runs plain `cmake -B build` after conan install and find_package(fmt) fails. Why?
    Nothing told CMake about the generators folder. The toolchain file is what prepends it to `CMAKE_PREFIX_PATH`, so configure with `cmake --preset conan-release`, or pass `-DCMAKE_TOOLCHAIN_FILE=<generators>/conan_toolchain.cmake` and `-DCMAKE_BUILD_TYPE=Release` on CMake older than 3.23.

  • conan install compiles my application as well as the dependencies.
  • If a binary is missing, conan install quietly builds it from source.
  • The generated CMake files are written into the Conan cache beside each package.
  • CMakeDeps always writes FindXXX.cmake modules for CMake's module mode.
  • Conan 2 still generates conanbuildinfo.cmake for CMake projects.

Assumes Conan 2.33; Conan 2 replaced the 1.x cmake generator and conanbuildinfo.cmake with CMakeDeps and CMakeToolchain.

open as a page

In Conan 2, how do CMakeDeps, CMakeToolchain and the generated conan-release preset hand fmt and zlib to a CMake project?

level: middleimportance: must knowfreq 55%

basics

~10 s

CMakeDeps 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.

open as a page

In Conan 2, when does a C++ project outgrow conanfile.txt and need a conanfile.py instead?

level: juniorimportance: should knowfreq 50%

basics

~20 s

conanfile.txt only lists requirements, generators, options and one predefined layout. As soon as you need conditional dependencies, a customised toolchain, a build() method for conan build, or to package the project with conan create, you need conanfile.py.

open as a page

In Conan 2, what does conan create do for an internal C++ library that conan install does not, and which recipe methods run?

level: middleimportance: should knowfreq 48%

basics

~20 s

conan create exports the recipe into the cache, builds the library from source there through generate(), build() and package(), records a package revision, then builds and runs test_package. conan install only prepares a consumer's dependencies.

open as a page

In a Conan 2 recipe, what changes when a code generator is declared with tool_requires instead of requires, especially when cross-building for an ARM target?

level: seniorimportance: should knowfreq 40%

basics

~20 s

tool_requires puts the generator in the build context, built for the build profile so it runs on the build machine, with headers and libraries not propagated, visible=False and no effect on package_id by default. requires would build it for the target.

open as a page