In Conan 2, what does conan install produce for a conanfile.txt listing fmt and zlib with the CMakeDeps and CMakeToolchain generators?
answer
- resolve the graph first
- binaries land in the local cache
- one config package per dependency
- toolchain file plus presets
- your own code is not built
basics
~10 sIt 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[requires]
fmt/11.0.2
zlib/1.3.1
[generators]
CMakeDeps
CMakeToolchain
[layout]
cmake_layoutgo deeper
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.
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.
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.
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.