C++ · Module 17 · Lesson 71 min read

CMake Basics

What is CMake?

A build system generator: you describe your project in CMakeLists.txt, and CMake generates Makefiles, Ninja files, or Visual Studio/Xcode projects. It's the de-facto standard for C++ projects.

A minimal CMakeLists.txt

scss
cmake_minimum_required(VERSION 3.20)
project(BudgetBuddy LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_executable(app main.cpp)

Building (out-of-source ✅)

ini
cmake -S . -B build                      # configure
cmake --build build                      # compile
./build/app
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release    # optimized build

Libraries and targets

scss
add_library(budget src/account.cpp src/report.cpp)
target_include_directories(budget PUBLIC include)
target_compile_options(budget PRIVATE -Wall -Wextra)

add_executable(app apps/main.cpp)
target_link_libraries(app PRIVATE budget)

Modern CMake is target-based: properties (include folders, flags, dependencies) are attached to targets, and PUBLIC ones flow to whatever links to them.

KeywordMeaning
PRIVATEonly for this target
PUBLICthis target and its users
INTERFACEonly its users

Tests

scss
enable_testing()
add_executable(tests tests/test_account.cpp)
target_link_libraries(tests PRIVATE budget)
add_test(NAME account_tests COMMAND tests)

Run with ctest --test-dir build.

Dependencies

  • find_package(fmt REQUIRED) for installed libraries
  • FetchContent to download a library during configuration
  • Package managers: vcpkg, Conan

Other build systems

Make (by hand), Meson, Bazel, Visual Studio solutions. IDEs like CLion and VS Code open CMake projects directly.

Example

Terminal
// ===== file: CMakeLists.txt =====
cmake_minimum_required(VERSION 3.20)
project(BudgetBuddy LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_library(budget src/budget.cpp)
target_include_directories(budget PUBLIC include)

add_executable(app apps/main.cpp)
target_link_libraries(app PRIVATE budget)
// ===== file: include/budget/budget.h =====
#pragma once
#include <string>
#include <vector>

namespace budget {
struct Expense { std::string category; double amount; };
double total(const std::vector<Expense>& items);
double total_for(const std::vector<Expense>& items, const std::string& category);
}
// ===== file: src/budget.cpp =====
#include "budget/budget.h"

namespace budget {
double total(const std::vector<Expense>& items) {
    double t = 0;
    for (const auto& e : items) t += e.amount;
    return t;
}
double total_for(const std::vector<Expense>& items, const std::string& category) {
    double t = 0;
    for (const auto& e : items) if (e.category == category) t += e.amount;
    return t;
}
}
// ===== file: apps/main.cpp =====
#include <iostream>
#include "budget/budget.h"

int main() {
    std::vector<budget::Expense> items = { { "Food", 450 }, { "Rent", 15000 }, { "Food", 1200 } };
    std::cout << "total: " << budget::total(items) << '\n';
    std::cout << "food: " << budget::total_for(items, "Food") << '\n';
    return 0;
}

/* Build:
cmake -S . -B build
cmake --build build
./build/app
*/

/* Output:
total: 16650
food: 1650
*/