If you've ever tried to build a C++ project and thought "why is this so much harder than every other language," you're not crazy. C++ doesn't come with a built-in way to grab libraries or remember your build settings. Two tools fix that: Conan (fetches libraries) and CMake Presets (remembers your settings). Let's build something real together.
First, What Even Are These Two Things?
Think of building a C++ project like cooking a meal.
- Conan is your grocery delivery service. You tell it "I need fmt, I need Boost, I need zlib" and it shows up with those ingredients already prepped, instead of you driving to five different stores.
- CMake is your recipe/instructions for actually cooking (compiling) the meal (your program).
- CMake Presets are like saving your favorite recipe settings — "always preheat to 375, always use the big pot" — so you don't have to type the same long commands every single time.
Put together: Conan gets your ingredients, CMake Presets remember how you like to cook, and CMake does the cooking.
Why Not Just Use CMake Alone?
You can. But without Conan, every time you need a library like fmt (a popular string-formatting library), you have to:
- Find it online
- Download it
- Compile it yourself
- Hope it works on your OS
- Wire it into your CMake file by hand
Conan automates all of that. You write down what you need, run one command, and it hands CMake everything ready to go.
And without CMake Presets, every teammate on your project has to remember (or ask you) the exact magic command to configure the build — which folder, which compiler, which build type. Presets turn that into a one-word command anyone can run.
The Project We're Building
A tiny "hello" program that uses the fmt library to print colorful-ish formatted text. Simple on purpose — the goal is understanding the plumbing, not the code.
Here's our folder layout:
myapp/
├── CMakeLists.txt
├── conanfile.txt
└── src/
└── main.cpp
Step 1: The Actual C++ Code
// src/main.cpp
#include <fmt/core.h>
int main() {
fmt::print("Hello, {}! You are learning Conan + CMake Presets.\n", "friend");
return 0;
}
Nothing scary here — we're just using fmt::print instead of the clunkier std::cout.
Step 2: Tell Conan What You Need (conanfile.txt)
This file is your grocery list.
# conanfile.txt
[requires]
fmt/10.2.1
[generators]
CMakeDeps
CMakeToolchain
[layout]
cmake_layout
Let's break that down like you're new here:
[requires]— "I need thefmtlibrary, version 10.2.1." That's it. Conan will download and build it for you.[generators]— This tells Conan how to hand the library over to CMake.CMakeDepscreates files so CMake canfind_package()the library like normal.CMakeToolchaincreates a file that tells CMake about your compiler settings so everything matches up.[layout]—cmake_layoutjust tells Conan to organize its output files in a predictable folder structure that plays nicely with CMake. Think of it as "keep the kitchen organized."
Step 3: The CMakeLists.txt
This is the "recipe" file — it says what to build and what it depends on.
# CMakeLists.txt
cmake_minimum_required(VERSION 3.20)
project(myapp CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(fmt REQUIRED)
add_executable(myapp src/main.cpp)
target_link_libraries(myapp PRIVATE fmt::fmt)
Plain English version:
- "This project needs at least CMake 3.20."
- "The project is called myapp and it's written in C++."
- "Use the C++17 standard."
- "Go find the
fmtlibrary" (Conan already made sure it's findable). - "Build an executable called
myappfrommain.cpp." - "Link that executable to the
fmtlibrary so it actually compiles."
Step 4: Install Dependencies With Conan
Run this in your terminal from the myapp/ folder:
conan install . --output-folder=build --build=missing
What's happening here, translated:
conan install .— "Look at my conanfile.txt in this folder and get everything it asks for."--output-folder=build— "Put all the generated files inside a folder calledbuild."--build=missing— "If a prebuilt version of a library isn't available for my system, build it from source instead of failing."
After this runs, Conan will have generated some very important files inside build/, including — and this is the fun part — a CMakePresets.json file, automatically, for you. You didn't write it. Conan wrote it based on what you asked for.
Step 5: What CMake Presets Actually Look Like
Here's a simplified version of what Conan generates:
{
"version": 4,
"cmakeMinimumRequired": {
"major": 3,
"minor": 20,
"patch": 0
},
"configurePresets": [
{
"name": "conan-release",
"displayName": "Conan Release",
"generator": "Ninja",
"binaryDir": "${sourceDir}/build/Release",
"cacheVariables": {
"CMAKE_TOOLCHAIN_FILE": "generators/conan_toolchain.cmake",
"CMAKE_BUILD_TYPE": "Release"
}
}
],
"buildPresets": [
{
"name": "conan-release",
"configurePreset": "conan-release"
}
]
}
Translated into human:
"configurePresets"— a saved recipe for setting up the build. This one is namedconan-release."generator": "Ninja"— "use the Ninja build tool" (a fast build system; CMake can also use Makefiles, Visual Studio, etc.)"binaryDir"— "put all the build output in this specific folder.""CMAKE_TOOLCHAIN_FILE"— this points to the file Conan generated that tells CMake exactly which compiler settings match the libraries it downloaded. This is the glue that makes Conan and CMake agree with each other."buildPresets"— a saved recipe for actually building after it's configured, linked to the configure preset above by name.
The beautiful part: you never have to type this out yourself. Conan hands it to you.
Step 6: Configure and Build Using the Preset
Now, instead of some long ugly command with a dozen flags, you just say:
cmake --preset conan-release
cmake --build --preset conan-release
That's it. Two lines. Translated:
cmake --preset conan-release— "Set up the project using the saved recipe named conan-release."cmake --build --preset conan-release— "Now actually compile it, using that same recipe."
Step 7: Run It
./build/Release/myapp
Output:
Hello, friend! You are learning Conan + CMake Presets.
That's the whole loop.
Putting the Whole Workflow Together
Here's the entire thing from an empty folder to a running program:
mkdir myapp && cd myapp
# (create src/main.cpp, conanfile.txt, CMakeLists.txt as shown above)
conan install . --output-folder=build --build=missing
cmake --preset conan-release
cmake --build --preset conan-release
./build/Release/myapp
Five commands. No hunting for libraries. No remembering compiler flags. No "works on my machine" arguments with your teammate, because the presets file travels with the project.
Why This Combo Is Worth Learning
- Reproducibility — anyone who clones your repo runs the same two or three commands and gets the same result, on Linux, macOS, or Windows.
- No manual flag-juggling — you're not typing
-DCMAKE_TOOLCHAIN_FILE=some/long/pathfrom memory every time. - Version control friendly — you can commit
CMakeUserPresets.json(or generate it fresh each time) so your whole team stays in sync. - Editor integration — tools like VS Code and CLion can read
CMakePresets.jsondirectly and show you a dropdown of build configurations. No setup needed on their end.
A Couple of Beginner Gotchas
- If
cmake --preset conan-releasesays it can't find the preset, double check you're running it from the folder that actually hasCMakePresets.json— Conan usually drops it in your project root or yourbuildfolder depending on your Conan version, so check where it landed. - Conan Presets are usually named
conan-releaseorconan-debugdepending on the build type you used. If you installed with-s build_type=Debug, look forconan-debuginstead. - Ninja not installed? Either install it (
sudo apt install ninja-buildon Ubuntu,brew install ninjaon Mac), or tell Conan to use a different generator with-c tools.cmake.cmaketoolchain:generator=Unix Makefilesduringconan install.