Post

04. CMake Flow

04. CMake Flow

CMake Flow


Prerequisites


1. What is flow of CMake

CMake follows a structured three-step workflow: Configure → Build → Install Each step has a clear and distinct purpose.

2. Workflow

2-1. Project Structure

A typical CMake project contains:

  • CMakeLists.txt
  • Source files (.cpp)
  • Header files (.h / .hpp)

CMake uses CMakeLists.txt as the main entry point.

2-2. Out-of-Source Build

Create a separate build directory to keep generated files isolated:

1
/build

This prevents mixing source files with build artifacts.

2-3. Configure Step

Run the following command:

1
cmake -S . -B build
What happens during configure:
  • Parses CMakeLists.txt
  • Detects and configures compiler
  • Generates build system files:

    • Makefile
    • .sln (Visual Studio)
    • build.ninja
  • Creates:

    • CMakeCache.txt
    • CMakeFiles/

⚠️ No compilation happens at this stage.

2-3 (Optional). Configure Step Using CMakePresets

Instead of writing long CLI commands, you can define presets in:

1
CMakePresets.json

Usually placed at the same level as CMakeLists.txt.

1
cmake --preset <preset-name>
Important:
  • cmake -S -B and cmake --preset both perform configure
  • You should use only one of them

Example (CLI):

1
2
3
4
cmake -S . -B build \
  -DCMAKE_BUILD_TYPE=Release \
  -DOpenCV_DIR=... \
  -G Ninja

Example (Preset):

1
cmake --preset <preset-name>
  • CLI → manual input
  • Preset → predefined configuration (JSON)
1
cmake --build build
  • .cpp → .o / .obj (compile)
  • .o / .obj → executable / library (link)

Inside the build/ directory, you may see:

  • Object files (.o, .obj)
  • Executables (.exe, binary)
  • Static libraries (.lib, .a)
  • Shared libraries (.dll, .so)

2-5 Install Step

1
cmake --build build --target install

Install takes build outputs and organizes them based on rules defined in CMakeLists.txt. When we process install, it calls cmake_install.cmake files

Typical structure:

1
2
3
4
install/
 ├── bin/
 ├── lib/
 ├── include/

Install is NOT just “cleaning up”.

It is copying and organizing build outputs into a structured layout for distribution or reuse.

3. Summary

1
2
3
Configure → Build → Install
   ↓          ↓        ↓
Setup     Compile   Package/Deploy
  • cmake -S -B and cmake --preset both perform configure
  • Presets are just a way to store configuration
  • Build step performs actual compilation
  • Install step prepares a clean, usable structure
This post is licensed under CC BY 4.0 by the author.