skip to content

Conan

Conan 2 fetches or builds C and C++ packages as binaries keyed by settings and options, and feeds them to CMake. Interviewers ask how it solves binary compatibility.

on this pageshow

explore

questions

15

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 does a requirement like zlib/[>=1.3 <2] resolve, and what do the ~ and ^ range operators mean?

level: middleimportance: must knowfreq 50%

basics

~20 s

A bracketed range resolves to the newest matching version, taken from the local cache if one matches there, otherwise from the first remote with a match. ~1.3 means >=1.3 <1.4, ^1.3 means >=1.3 <2, and pre-releases are skipped unless requested.

open as a page

In Conan 2, what do --build=missing, --build=never, --build="*" and a --build pattern each tell conan install to do?

level: middleimportance: must knowfreq 55%

basics

~20 s

Without --build, conan install never compiles a dependency and fails on a missing binary. --build=missing builds only packages with no usable binary, --build="*" rebuilds everything, a pattern rebuilds matching packages, and --build=never forbids building outright.

open as a page

In Conan 2, how do settings, options and conf differ, and which of them change a package's package_id?

level: middleimportance: must knowfreq 58%

basics

~20 s

Settings are project-wide values such as compiler and build_type; options are per-package switches such as shared; conf configures Conan and its tools. Settings and options a recipe declares enter its package_id, while conf stays out unless tools.info.package_id:confs lists it.

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

A release branch's Conan 2 build changed overnight because zlib/[>=1.3 <2] resolved to a new zlib release. How do you use a lockfile to stop this in CI?

level: seniorimportance: must knowfreq 45%

basics

~20 s

Run conan lock create on the branch, commit the resulting conan.lock beside the conanfile, and let CI install with it: Conan 2 loads conan.lock automatically and resolves every range to the recorded version and recipe revision, failing if a requirement is missing.

open as a page

In Conan 2, what is a profile, and what does conan profile detect write into the default one?

level: juniorimportance: should knowfreq 55%

basics

~20 s

A profile is a text file of sections such as [settings], [options], [conf] and [tool_requires] that configures a Conan 2 command. conan profile detect guesses only [settings]: os, arch, compiler, compiler.version, compiler.cppstd, compiler.libcxx and build_type=Release.

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 a Version conflict error mean, and how do force=True and override=True on self.requires resolve it?

level: middleimportance: should knowfreq 40%

basics

~20 s

Two requirements in one context ask for different versions of the same package, and Conan 2 refuses to guess. force=True in the consumer makes its version win and adds it as a dependency; override=True only replaces the version others request, without adding one.

open as a page

In Conan 2, in what order does conan install look in the local cache and the configured remotes for a recipe and its binaries?

level: middleimportance: should knowfreq 40%

basics

~20 s

Conan 2 looks in the local cache first and uses what it finds without contacting remotes. Otherwise it queries remotes in the order conan remote list shows and takes the first hit; --update checks all remotes for newer revisions.

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 Conan 2, what are recipe revisions and package revisions, and how can zlib/1.3 resolve to a different recipe without any version change?

level: seniorimportance: should knowfreq 32%

basics

~20 s

A recipe revision hashes the exported recipe files, or is a git commit in scm mode; a package revision hashes a built package's contents. Re-exporting a changed recipe adds a revision under the same version, and the latest wins by default.

open as a page

After a developer bumps compiler.version in the shared Conan 2 CI profile, every compiled dependency rebuilds from source. Why, and how do you confirm and contain it?

level: seniorimportance: should knowfreq 45%

basics

~20 s

compiler.version is a setting, so every compiled package gets a new package_id with no binary on the remote, and --build=missing builds them all. conan graph explain shows the diff; contain it by prebuilding and uploading once, or by an explicit compatibility rule.

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

As a lead, how would you set a team's Conan 2 policy for pinning and updating third-party C++ packages across the main branch and release branches?

level: principalimportance: should knowfreq 30%

basics

~20 s

There is no single answer, but a common policy is: libraries declare ranges, applications commit lockfiles, CI consumes only a vetted internal remote, main refreshes its lock on a schedule through reviewed changes, and release branches stay frozen except for deliberate security updates.

open as a page