Consuming Packages with conanfile.txt
A consumer is a project that uses packages without being a package itself: your application. The simplest way to describe its dependencies is a conanfile.txt, an INI-style file with a few sections. You commit it next to your CMakeLists.txt.
The sections
[requires]
fmt/12.1.0
spdlog/1.17.0
[test_requires]
catch2/3.16.0
[tool_requires]
# build tools that run on the build machine, e.g. cmake/4.4.4 or ninja/1.13.2
[generators]
CMakeDeps
CMakeToolchain
[options]
spdlog/*:shared=False
[layout]
cmake_layout| Section | Meaning |
|---|---|
[requires] |
libraries your code links against, as name/version (or a version range) |
[test_requires] |
libraries only your tests need; they don't propagate to anyone consuming you |
[tool_requires] |
executables needed during the build (CMake, Ninja, protoc); they are built for the build machine |
[generators] |
files to write for your build system; for CMake: CMakeDeps and CMakeToolchain |
[options] |
option values for dependencies, with a pattern: pkg/*:option=value |
[layout] |
where the generated files go; cmake_layout uses build/<BuildType>/ |
Install, then build
The workflow is always the same three commands: conan install (dependencies + generated files), CMake configure with the generated toolchain, CMake build. Here is an app that uses spdlog (which pulls in fmt for you):
mkdir -p logger && cd logger
cat > conanfile.txt <<'TXT'
[requires]
spdlog/1.17.0
[generators]
CMakeDeps
CMakeToolchain
[layout]
cmake_layout
TXT
cat > CMakeLists.txt <<'TXT'
cmake_minimum_required(VERSION 3.25)
project(logger CXX)
find_package(spdlog REQUIRED CONFIG)
add_executable(logger main.cpp)
target_link_libraries(logger PRIVATE spdlog::spdlog)
TXT
cat > main.cpp <<'SRC'
#include <spdlog/spdlog.h>
int main() {
spdlog::set_pattern("[%l] %v"); // no timestamp, so the output is stable
spdlog::info("spdlog {}.{}.{} is working", SPDLOG_VER_MAJOR, SPDLOG_VER_MINOR, SPDLOG_VER_PATCH);
spdlog::warn("disk is {}% full", 91);
}
SRC
conan install . -c tools.build:jobs=1
======== Input profiles ========
Profile host:
[settings]
arch=x86_64
build_type=Release
compiler=gcc
compiler.cppstd=gnu17
compiler.libcxx=libstdc++11
compiler.version=13
os=Linux
[conf]
tools.build:jobs=1
Profile build:
[settings]
arch=x86_64
build_type=Release
compiler=gcc
compiler.cppstd=gnu17
compiler.libcxx=libstdc++11
compiler.version=13
os=Linux
======== Computing dependency graph ========
Graph root
conanfile.txt: /home/learner/logger/conanfile.txt
Requirements
fmt/12.1.0#6baf0fb8351783472b94ee6e36232391 - Cache
spdlog/1.17.0#bcbaaf7147bda6ad24ffbd1ac3d7142c - Cache
======== Computing necessary packages ========
Requirements
fmt/12.1.0#6baf0fb8351783472b94ee6e36232391:e02956bd268869d4519cb2b90dc7b4e51911a5d0#a857dc2fda2e0c13d61393798f6a9684 - Cache
spdlog/1.17.0#bcbaaf7147bda6ad24ffbd1ac3d7142c:68cf2b888a31d527b3476ac5bf6707b2db1ccd2d#81ce70d6997cc1fbe7ed27c13c7c4bac - Cache
======== Installing packages ========
fmt/12.1.0: Already installed! (1 of 2)
spdlog/1.17.0: Already installed! (2 of 2)
======== Finalizing install (deploy, generators) ========
>>> Generate step for conanfile.txt
Generators folder: /home/learner/logger/build/Release/generators
Generator 'CMakeDeps' calling 'generate()'
CMakeDeps necessary find_package() and targets for your CMakeLists.txt
find_package(spdlog)
target_link_libraries(... spdlog::spdlog)
Generator 'CMakeToolchain' calling 'generate()'
CMakeToolchain generated: conan_toolchain.cmake
CMakeToolchain: Preset 'conan-release' added to CMakePresets.json.
(cmake>=3.23) cmake --preset conan-release
(cmake<3.23) cmake <path> -G "Unix Makefiles"
-DCMAKE_TOOLCHAIN_FILE=<output_folder>/generators/conan_toolchain.cmake
-DCMAKE_POLICY_DEFAULT_CMP0091=NEW -DCMAKE_BUILD_TYPE=Release
CMakeToolchain generated: /home/learner/logger/build/Release/generators/CMakePresets.json
CMakeToolchain generated: /home/learner/logger/CMakeUserPresets.json
Generating aggregated env files
Generated aggregated env files: ['conanbuild.sh', 'conanrun.sh']
Install finished successfully
The install output has four parts:
- Input profiles: the host profile (what you build for) and the build profile (the machine running the build). They're the same here, except that our
-c tools.build:jobs=1shows up as[conf]in the host profile; they differ when cross-compiling. - Computing dependency graph:
spdlogwas requested, and its recipe addedfmtas a transitive requirement. - Computing necessary packages: a binary for each, found in the cache.
- Finalizing install: the generators wrote
fmt-config.cmake,spdlog-config.cmake,conan_toolchain.cmakeandCMakePresets.json, and the last lines tell you how to use them.
Now build and run. CMakeDeps printed the find_package() and target names you need, and they match the CMakeLists.txt:
mkdir -p logger && cd logger
cat > conanfile.txt <<'TXT'
[requires]
spdlog/1.17.0
[generators]
CMakeDeps
CMakeToolchain
[layout]
cmake_layout
TXT
cat > CMakeLists.txt <<'TXT'
cmake_minimum_required(VERSION 3.25)
project(logger CXX)
find_package(spdlog REQUIRED CONFIG)
add_executable(logger main.cpp)
target_link_libraries(logger PRIVATE spdlog::spdlog)
TXT
cat > main.cpp <<'SRC'
#include <spdlog/spdlog.h>
int main() {
spdlog::set_pattern("[%l] %v");
spdlog::info("spdlog {}.{}.{} is working", SPDLOG_VER_MAJOR, SPDLOG_VER_MINOR, SPDLOG_VER_PATCH);
spdlog::warn("disk is {}% full", 91);
}
SRC
conan install . -c tools.build:jobs=1 > /dev/null 2>&1
cmake --preset conan-release
cmake --build --preset conan-release
./build/Release/logger-- Using Conan toolchain: /home/learner/logger/build/Release/generators/conan_toolchain.cmake
-- Conan toolchain: Defining architecture flag: -m64
-- Conan toolchain: C++ Standard 17 with extensions ON
-- The CXX compiler identification is GNU 13.3.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Conan: Component target declared 'spdlog::spdlog'
-- Conan: Component target declared 'fmt::fmt'
-- Configuring done (0.2s)
-- Generating done (0.0s)
-- Build files have been written to: /home/learner/logger/build/Release
[ 50%] Building CXX object CMakeFiles/logger.dir/main.cpp.o
[100%] Linking CXX executable logger
[100%] Built target logger
[info] spdlog 1.17.0 is working
[warning] disk is 91% full
The configure step starts with Using Conan toolchain, and the Conan: Component target declared lines come from the generated config files. Your CMakeLists.txt contains nothing Conan-specific: the same file would work with fmt and spdlog installed by vcpkg or the system. That's the main design goal of Conan 2's CMake integration.
Which files to commit
| File | Commit? |
|---|---|
conanfile.txt |
yes |
CMakeLists.txt |
yes |
build/ (generated files, build tree) |
no, add to .gitignore |
CMakeUserPresets.json |
no: Conan writes it, and it contains absolute paths |
conan.lock |
yes, for applications (Lockfiles) |
When conanfile.txt is not enough
conanfile.txt can't express conditions ("use libuv only on Linux"), can't compute versions, and can't run code. When you need any of that, switch to conanfile.py; the CMake side stays the same.
Test yourself
Which conanfile.txt section is for a library only your unit tests use?
- [requires]
- [tool_requires]
- [test_requires]
- [options]
The app only requires spdlog. Why does the graph also contain fmt?
- Conan always installs fmt
- spdlog's recipe declares fmt as its own requirement, so it is a transitive dependency
- The default profile lists fmt
- CMake added it
Should CMakeUserPresets.json written by conan install be committed?
- Yes, always
- No: it's generated and contains machine-specific absolute paths
- Only on Windows
- Only for libraries