Skip to main content

C++ Conan + CMake Presets

 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:

  1. Find it online
  2. Download it
  3. Compile it yourself
  4. Hope it works on your OS
  5. 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 the fmt library, 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. CMakeDeps creates files so CMake can find_package() the library like normal. CMakeToolchain creates a file that tells CMake about your compiler settings so everything matches up.
  • [layout]cmake_layout just 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 fmt library" (Conan already made sure it's findable).
  • "Build an executable called myapp from main.cpp."
  • "Link that executable to the fmt library 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 called build."
  • --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 named conan-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/path from 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.json directly and show you a dropdown of build configurations. No setup needed on their end.

A Couple of Beginner Gotchas

  • If cmake --preset conan-release says it can't find the preset, double check you're running it from the folder that actually has CMakePresets.json — Conan usually drops it in your project root or your build folder depending on your Conan version, so check where it landed.
  • Conan Presets are usually named conan-release or conan-debug depending on the build type you used. If you installed with -s build_type=Debug, look for conan-debug instead.
  • Ninja not installed? Either install it (sudo apt install ninja-build on Ubuntu, brew install ninja on Mac), or tell Conan to use a different generator with -c tools.cmake.cmaketoolchain:generator=Unix Makefiles during conan install.

Popular posts from this blog

How to Check if Someone is Connected to Your Machine in Linux

Picture this: you glance at your system monitor and notice your CPU is humming along even though you're not running anything demanding. Or maybe your internet feels sluggish for no obvious reason. A small, uneasy thought creeps in — is someone else on my machine right now? For Linux users, this isn't something you have to wonder about. Linux ships with a powerful set of built-in tools that let you see exactly who's connected, who's logged in, and what your network is doing at any given moment. You don't need to be a security expert to use them — you just need to know where to look. This guide walks you through the practical, no-nonsense steps to check for unauthorized connections on your Linux system, with real commands you can run right now. Why Monitoring Network Connections Matters Every device on a network — including your own Linux machine — communicates using an IP address. When another device or user connects to your system, that connection shows up as a trac...

How to Set Up a Linux Web Server and Host an HTML Page Easily

Setting up a web server on Linux means spending a fair amount of time in the terminal — Linux leans heavily on the command line rather than clicking through menus, so you'll be typing out instructions more often than not.  If you're new to this, it can feel a little intimidating at first, but the good news is you don't need to become a Linux wizard overnight. A handful of core commands will get you surprisingly far. A few you'll lean on constantly: cd — move between directories ls — see what's in the current directory mkdir — create a new folder nano or vim — edit files right there in the terminal sudo — run something with administrator privileges Get comfortable with these and you'll be able to navigate around, tweak configuration files, and install software without much trouble. You don't need to memorize everything — you just need to be confident enough to follow along with clear instructions, which is exactly what this guide aims to give you....

Linux Network Troubleshooting

If you've spent any time as a sysadmin — or honestly, just as someone who's had to fix their own home network at 11pm — you know that connectivity issues are one of the most common headaches out there. The good news is that a handful of core tools and a methodical approach can take you from "why isn't this working" to a root cause pretty quickly.  This guide walks through the essentials: configuring interfaces, managing routes, and diagnosing problems when things go sideways. Configuring Network Interfaces Your network interfaces are the actual bridge between your machine and the outside world, so getting them configured correctly is step one for any kind of reliable connectivity. Doing It Manually ifconfig is the old-school, tried-and-true tool for this on Unix-like systems. To see everything currently configured, run: ifconfig -a If you need to manually set up a specific interface — assigning an IP, a netmask, and bringing it online — it looks like this: ifconf...