Creating a Package: conan new & conan create
So far you've consumed packages someone else made. Packaging your own library is how a team shares code between repositories with the same tooling: version it, build binaries for each configuration once, upload them, and let every consumer requires it. The recipe format is the same conanfile.py you've used, with a few more methods.
Start from a template
conan new writes a working recipe plus a library and a test project. cmake_lib is the usual starting point (others: cmake_exe, header_lib, meson_lib, autotools_lib, msbuild_lib, bazel_lib):
mkdir -p greet && cd greet
conan new cmake_lib -d name=greet -d version=1.0.0 2>&1
echo "--- the recipe's key lines"
grep -nE 'name =|version =|package_type|settings =|options =|default_options|exports_sources|def [a-z_]+|self\.cpp_info' conanfile.pyFile saved: CMakeLists.txt
File saved: conanfile.py
File saved: include/greet.h
File saved: src/greet.cpp
File saved: test_package/CMakeLists.txt
File saved: test_package/conanfile.py
File saved: test_package/src/example.cpp
--- the recipe's key lines
6: name = "greet"
7: version = "1.0.0"
8: package_type = "library"
18: settings = "os", "compiler", "build_type", "arch"
19: options = {"shared": [True, False], "fPIC": [True, False]}
20: default_options = {"shared": False, "fPIC": True}
23: exports_sources = "CMakeLists.txt", "src/*", "include/*"
25: def config_options(self):
29: def configure(self):
33: def layout(self):
36: def generate(self):
42: def build(self):
47: def package(self):
51: def package_info(self):
52: self.cpp_info.libs = ["greet"]
| Part | Role |
|---|---|
name, version, package_type |
identity; package_type = "library" means "static or shared, depending on the shared option" |
settings |
which profile values affect the binary (they go into the package ID) |
options / default_options |
shared and fPIC, the two options nearly every library has |
exports_sources |
files copied into the cache with the recipe, because the source lives next to it |
config_options() / configure() |
remove options that don't apply (fPIC on Windows, or when building shared) |
layout(), generate(), build() |
the same as for a consumer: CMake does the work |
package() |
copy the results into the package folder (cmake.install()) |
package_info() |
tell consumers what to link: self.cpp_info.libs = ["greet"] |
The template's greet.cpp prints a lot of compiler information. To keep things short, the next session replaces it with a one-liner first.
conan create: export, build, package, test
conan create . runs the whole pipeline for one configuration (your profile):
mkdir -p greet && cd greet
conan new cmake_lib -d name=greet -d version=1.0.0 > /dev/null 2>&1
cat > src/greet.cpp <<'SRC'
#include <iostream>
#include "greet.h"
void greet() { std::cout << "greet/1.0.0: hello from a Conan package\n"; }
void greet_print_vector(const std::vector<std::string>&) {}
SRC
conan create . -c tools.build:jobs=1 2>&1 | grep -E '^(========|-------- )|Exported:|: Package |Building from source|RUN: ./example|hello from'======== Exporting recipe to the cache ========
Exported: greet/1.0.0#2f0bb808f38098efad0cf2a66ca3df04 (2026-10-23 16:13:17 UTC)
======== Input profiles ========
======== Computing dependency graph ========
======== Computing necessary packages ========
======== Installing packages ========
-------- Installing package greet/1.0.0 (1 of 1) --------
Building from source greet/1.0.0:838c313f1b7acdfd23b222f0156e7c264dcc2515
greet/1.0.0: Package '838c313f1b7acdfd23b222f0156e7c264dcc2515' built
======== Launching test_package ========
======== Computing dependency graph ========
======== Computing necessary packages ========
======== Installing packages ========
======== Testing the package ========
======== Testing the package: Building ========
======== Testing the package: Executing test ========
greet/1.0.0 (test package): RUN: ./example
greet/1.0.0: hello from a Conan package
Reading the banners top to bottom:
- Exporting the recipe:
conanfile.pyand theexports_sourcesare copied into the cache asgreet/1.0.0#<rrev>. The recipe revision is a hash of those files, so changing any of them gives a new revision. - Computing the graph / necessary packages:
greethas no binary for this configuration yet, so it's markedBuild. - Installing packages: Conan builds it in the cache's build folder and runs
package(), which produces the package ID shown inPackage '…' built. - Testing the package: Conan builds the small project in
test_package/against the new package and runs it. Seeinghello from a Conan packageproves the package can be found, linked and run by a consumer.
Checking the result
mkdir -p greet && cd greet
conan new cmake_lib -d name=greet -d version=1.0.0 > /dev/null 2>&1
conan create . -c tools.build:jobs=1 > /dev/null 2>&1
conan list "greet/1.0.0:*" --format=compact 2>&1 | sed -n '3,$p' | sed 's/%[0-9.]* (.*)//'
cd "$(conan cache path greet/1.0.0:838c313f1b7acdfd23b222f0156e7c264dcc2515)" && find . -type f | sort greet/1.0.0#985a47244e0721362a6bfde1360bb9d4
greet/1.0.0#985a47244e0721362a6bfde1360bb9d4:838c313f1b7acdfd23b222f0156e7c264dcc2515
settings: Linux, x86_64, Release, gcc, gnu17, libstdc++11, 13
options: fPIC=True, shared=False
./conaninfo.txt
./conanmanifest.txt
./include/greet.h
./lib/libgreet.a
The package folder is exactly what cmake.install() produced (header, static library) plus Conan's conaninfo.txt and conanmanifest.txt. (The second sed drops a timestamp that changes on every run.)
Building more configurations
One conan create builds one binary. A CI job typically loops over the profiles you support:
for p in linux-gcc13-release linux-gcc13-debug linux-clang18-release; do
conan create . -pr:h "profiles/$p" --build=missing
done
conan create . -o "greet/*:shared=True" # a shared-library variantThen upload the recipe and all binaries in one go.
Versioning your package
- Bump
versionfor every release; never change what a published version means. - Fix only the packaging (recipe, patches) without changing the library? Keep the version: the new recipe revision is enough, and consumers get it automatically unless they've locked the old one.
- Pass the version from CI instead of hard-coding it:
conan create . --version=1.2.3, or compute it in aset_version()method from a Git tag or a file.
Test yourself
What does conan create . do, in order?
- Upload, build, test
- Export the recipe, build the package for your profile, run test_package
- Only builds the library
- Creates a new project from a template
Which method tells consumers which libraries to link?
- package()
- build()
- package_info()
- layout()
You change only a patch in the recipe, not the library's code. What changes?
- The version
- The recipe revision
- Nothing
- The name