In Conan 2, when does a C++ project outgrow conanfile.txt and need a conanfile.py instead?
answer
- declarative sections versus Python methods
- no conditions in the text file
- packaging needs a recipe class
- generate() customises the toolchain
basics
~20 sconanfile.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.
solid answer
~40 sA `conanfile.txt` is a fixed set of sections: `[requires]`, `[tool_requires]`, `[test_requires]`, `[generators]`, `[options]` and `[layout]`, where the layout is one of `cmake_layout`, `vs_layout` or `bazel_layout`. It is fine for an application that just consumes packages. A `conanfile.py` is a Python class deriving from `ConanFile`, so it can make a requirement depend on `self.settings.os` or an option, set CMake variables in `generate()` through `CMakeToolchain`, define `layout()` with custom folders, and provide `build()` so `conan build .` works. Above all, only a `conanfile.py` can be a package recipe: `conan create` needs a name, version, `package()` and `package_info()`, and it fails outright when the folder holds only a conanfile.txt.
code
python · 25 linesfrom conan import ConanFile
from conan.tools.cmake import CMake, CMakeDeps, CMakeToolchain, cmake_layout
class AppRecipe(ConanFile):
settings = "os", "compiler", "build_type", "arch"
def requirements(self):
self.requires("fmt/11.0.2")
if self.settings.os != "Windows":
self.requires("zlib/1.3.1")
def layout(self):
cmake_layout(self)
def generate(self):
CMakeDeps(self).generate()
tc = CMakeToolchain(self)
tc.cache_variables["APP_WITH_TELEMETRY"] = "OFF"
tc.generate()
def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()go deeper
Know the sections of conanfile.txt and that it only lists things, while conanfile.py is Python code that can decide things.
List the concrete triggers for switching: conditional requirements, generate() customisation, build() for conan build, and packaging with conan create.
Keep consumer recipes small: put platform conditions in requirements(), CMake knobs in generate(), and avoid logic that belongs in profiles.
Set a team default for when an application keeps the text file and when it must move to a recipe, so repositories stay consistent.
## Two formats for one job Conan 2 reads a project's dependencies from either a **conanfile.txt** or a **conanfile.py**. Both can feed `conan install`, and both can use the CMakeDeps and CMakeToolchain generators. They differ in what else they can express. | Need | conanfile.txt | conanfile.py | |---|---|---| | list regular, tool and test requirements | yes, three sections | yes, attributes or methods | | set dependency options such as `fmt/*:shared=True` | `[options]` | `default_options` or `configure()` | | pick a layout | one predefined name | any `layout()` code | | requirements that depend on OS, compiler or options | no | `requirements()` with `if` | | customise CMake variables or presets | no | `generate()` | | run `conan build .` | no | `build()` | | be packaged with `conan create` | no | yes | ## What conanfile.txt can hold The Conan 2 loader accepts these sections: - `[requires]`: one reference per line, such as `fmt/11.0.2`; - `[tool_requires]` and `[test_requires]`: build tools and test frameworks; - `[generators]`: names such as `CMakeDeps` and `CMakeToolchain`; - `[options]`: written as `pattern:option=value`, for example `zlib/*:shared=True`; - `[layout]`: exactly one of `cmake_layout`, `vs_layout` or `bazel_layout`; a second line is an error. There is no way to say "only on Windows" or "only when building tests", no hook to set a CMake cache variable, and no name or version, so the file can never describe a package. ## What conanfile.py adds A conanfile.py is a Python module containing a class derived from `conan.ConanFile`. The methods most consumer projects grow into are: 1. `requirements()`, where `self.requires("zlib/1.3.1")` can sit inside `if self.settings.os != "Windows":` or behind an option; 2. `build_requirements()`, for `self.tool_requires(...)` and `self.test_requires(...)`; 3. `layout()`, usually `cmake_layout(self)` or `cmake_layout(self, src_folder="src")`; 4. `generate()`, where `tc = CMakeToolchain(self)` lets you set `tc.cache_variables["APP_WITH_TELEMETRY"] = "OFF"` before `tc.generate()`, and `CMakeDeps(self).generate()` writes the dependency files; 5. `build()`, which uses the `CMake` helper to configure and build, so `conan build .` installs dependencies and builds in one command. ## The packaging line The deciding case is usually packaging. An internal library that other teams consume must be exported to the cache and built into a binary package by `conan create`, and that command loads only a `conanfile.py`; pointed at a folder with only a conanfile.txt it stops with `Conanfile not found`. The same holds for `conan build .`. The recipe then needs a `name` and `version`, `settings`, usually `options` for `shared` and `fPIC`, `exports_sources` or a `source()` method, and `package()` and `package_info()`. How those methods run under `conan create` is its own topic; here the point is that a text file cannot carry them. ## Mistakes that send people to the wrong format - Trying to express a platform difference by keeping two conanfile.txt files and copying the right one in CI. It works until they drift; one conanfile.py with an `if` is easier to review. - Putting compiler or build-type choices into the conanfile at all. Those belong in profiles and on the command line, in either format, because the same conanfile should serve every configuration. - Writing a conanfile.py for a consumer and forgetting `settings`. Without `build_type` declared, `cmake_layout()` stops with an error that the setting is not defined. - Assuming a conanfile.txt `[options]` line applies to the consumer itself; it sets options on the dependencies that match the pattern. ## Choosing between them - Keep a **conanfile.txt** for a leaf application whose dependency list is the same on every platform: it is shorter and nobody needs to read Python. - Move to a **conanfile.py** the first time you add a platform condition, need a CMake variable set from Conan, want `conan build`, or need to publish the project as a package. - Converting is mechanical: each section maps to an attribute or method, and the generators move into `generate()` or the `generators` attribute.
- Can conanfile.txt set an option on a dependency?Yes. The `[options]` section takes `pattern:option=value` lines such as `zlib/*:shared=True`. What it cannot do is make that option depend on a setting; for that you need `configure()` or `default_options` logic in a conanfile.py.
- What does conan build . do that conan install does not?It requires a conanfile.py, runs the same install and generate steps, and then calls the recipe's `build()` method in the local folder, so dependencies and your own build happen in one command. Nothing is exported to the cache.
- conanfile.txt can be packaged with conan create if you add a name line.
- conanfile.txt supports if-conditions on settings in the requires section.
- conanfile.py is only for package authors, never for consuming applications.
- The two formats need different generators to produce CMake files.