In Conan 2, what does conan create do for an internal C++ library that conan install does not, and which recipe methods run?
answer
- creator versus consumer command
- export to the cache first
- the created reference always rebuilds
- test_package runs last
basics
~20 sconan 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.
solid answer
~40 s`conan install` is a consumer command: it installs a project's dependencies and runs its generators, and the project itself never enters the cache. `conan create .` treats the folder as a package recipe. It exports `conanfile.py` and its `exports_sources` to the cache, which yields a recipe revision, resolves the graph and builds the created reference from source even if a binary for that package ID already exists, unless you pass your own `--build`. In the cache it runs `source()` if defined, `generate()`, `build()` and `package()`, which for a CMake recipe calls `cmake.install()` into the package folder, and then records a package revision. Finally it builds and runs the `test_package` consumer, unless you pass `--test-folder=""`. The library is then ready for `conan upload` to the team's remote.
code
console · 12 lines$ conan create .
======== Exporting recipe to the cache ========
Exported: mathcore/1.4.0#<rrev>
======== Installing packages ========
mathcore/1.4.0: Calling build()
mathcore/1.4.0: RUN: cmake -G "Unix Makefiles" -DCMAKE_TOOLCHAIN_FILE="generators/conan_toolchain.cmake" ...
Calling package()
Created package revision <prev>
Package '<package_id>' created
======== Launching test_package ========
======== Testing the package ========
mathcore/1.4.0 (test package): Running test()go deeper
Know that conan install prepares dependencies, while conan create turns a recipe into a package in the local cache.
Explain the create sequence: export and recipe revision, forced build of the created reference, generate, build, package, package revision, then test_package.
Use test_package to catch packaging defects such as missing headers or wrong cpp_info names, and know when export-pkg fits better than create.
Decide how internal libraries are published: who runs conan create, for which configurations, and which remote other teams are allowed to consume from.
## Consumer and creator commands Conan 2 separates commands that **consume** packages from commands that **create** them. | Command | Reads | Puts into the cache | Builds your code | |---|---|---|---| | `conan install .` | conanfile.txt or conanfile.py | only dependencies | no | | `conan build .` | conanfile.py | only dependencies | yes, locally, by calling `build()` | | `conan export .` | conanfile.py | the recipe only | no | | `conan export-pkg .` | conanfile.py | recipe plus a package from local build output | no, it packages what is there | | `conan create .` | conanfile.py | recipe plus a freshly built package | yes, in the cache | For a library that other teams consume, `conan create` is the command that turns sources into something they can `requires`. ## A minimal internal library recipe `conan new cmake_lib -d name=mathcore -d version=1.4.0` generates a recipe close to this shape: - `name`, `version` and `package_type = "library"`; - `settings = "os", "compiler", "build_type", "arch"`, which enter the package ID; - `options = {"shared": [True, False], "fPIC": [True, False]}` with defaults; - `exports_sources`, listing the CMakeLists.txt, sources and headers to copy along with the recipe; - `config_options()` and `configure()`, which remove `fPIC` on Windows or when `shared` is set; - `layout()` calling `cmake_layout(self)`; - `generate()` running `CMakeDeps` and `CMakeToolchain`; - `build()` running the `CMake` helper's `configure()` and `build()`; - `package()` running `cmake.install()`, so the CMake install rules decide what ships; - `package_info()` setting `self.cpp_info.libs = ["mathcore"]` and, if needed, `cmake_file_name` and `cmake_target_name`. ## What conan create runs, in order 1. **Export.** The recipe and `exports_sources` are copied into the cache and hashed into a **recipe revision**; the output shows `Exported: mathcore/1.4.0#<rrev>`. 2. **Graph.** Dependencies are resolved as for install, with the host and build profiles. 3. **Binary analysis.** If `--build` is not given, Conan forces a build of the reference being created and treats its dependencies as with a plain install. An existing binary for the same package ID is rebuilt, not reused. 4. **Build in the cache.** Conan runs `source()` when defined, then `generate()`, then `build()`; the build folder is inside the cache, not in your checkout. 5. **Package.** `package()` copies or installs the artifacts into the package folder, Conan hashes them into a **package revision**, and prints `Package '<package_id>' created`. 6. **Test.** The `test_package/` folder holds a small consumer whose `requirements()` uses `self.tested_reference_str`. Conan installs it, builds it and runs its `test()` method, which proves that a consumer can find, link and run the package. `--test-folder=""` skips it. `package_info()` is not a build step: it runs whenever a consumer loads the package, and its `cpp_info` is what CMakeDeps turns into targets. ## Why the test_package matters A package can build and still be unusable: a header missing from `package()`, a wrong library name in `cpp_info.libs`, or a target name consumers do not expect. The test_package catches these at creation time instead of in another team's build. It should stay tiny, one source file that includes a header and calls one function, because it tests packaging, not the library's behaviour; unit tests belong in the library's own build. ## Mistakes that break consumers 1. Listing too little in `exports_sources`, so the build in the cache cannot find a source file that exists in the checkout. A local `conan build .` hides this; `conan create` exposes it because it builds from the exported copy. 2. Writing `package()` by hand with `copy()` calls and forgetting the headers of a subdirectory, which the test_package then fails to include. 3. Setting `self.cpp_info.libs` to a name that differs from the built library file, so the generated target points at a library that does not exist and the test_package fails to link. 4. Changing the recipe without changing the version, then wondering why consumers see a new recipe revision; that is expected behaviour, and revisions are their own topic. ## After create The package now sits in the local cache as `mathcore/1.4.0#<rrev>:<package_id>#<prev>`. `conan upload "mathcore/1.4.0" -r <team-remote>` publishes the recipe and that binary. Other configurations, such as Debug or shared, need their own `conan create` runs with different settings or options, each producing another package ID under the same recipe revision. Why those settings yield distinct binaries is the subject of package IDs and profiles.
- When would you use conan export-pkg instead of conan create?When the binaries are already built outside Conan's cache, for example by an existing CI build or a vendor drop. `conan export-pkg` exports the recipe and runs `package()` over the local build output, creating a package without rebuilding.
- Why does conan create rebuild the library even when a binary with the same package ID is already in the cache?Without `--build`, `conan create` adds the created reference itself as a build pattern. The command's purpose is to produce a fresh package from these sources, so it never silently reuses a stale binary.
- conan create builds the library in my working copy, like CMake does.
- conan create reuses an existing binary for the created package if the package ID matches.
- package_info() runs once at the end of conan create and is then forgotten.
- test_package should hold the library's full unit test suite.