01 / 21

Understanding
CMake

Learn what CMake is, why every C++ developer needs it, and how it automates the build process across platforms.

C++

What is CMake?

CMake is an open-source, cross-platform build system generator. It does not compile your code directly: instead, it produces the native build files (like Makefiles or Visual Studio projects) that your compiler toolchain actually uses.

The Build Problem

A simple C++ project with one file is easy to compile. But real projects have hundreds of source files, external libraries, platform-specific settings, and complex dependencies. Managing all of this by hand is error-prone and painful.

main.cpp utils.cpp math.cpp headers... ? Compiler g++ / MSVC / clang .exe

CMake: the Solution

CMake lets you write a single configuration file, CMakeLists.txt, that describes your entire project. From that one file, CMake generates the correct build instructions for any platform.

CMakeLists.txt CMake Engine Makefile Linux / macOS .sln / .vcxproj Windows (MSVC) build.ninja Cross-platform

The Core Concept

Configure
CMake reads your CMakeLists.txt and detects your system: compiler, OS, libraries.
Generate
It produces native build files (Makefiles, Ninja files, or IDE projects).
Build
Your native build tool (make, ninja, msbuild) compiles and links using those files.

CMake vs. the Compiler

A common misconception is that CMake compiles code. It does not. Here is the distinction:

CMake Compiler (g++, clang++)
Role Generates build files Compiles source code
Input CMakeLists.txt .cpp / .h files
Output Makefile, .sln, build.ninja .o, .exe, .so
Platform aware? Yes: generates per-platform Specific to one toolchain
Manages deps? Yes: find_package, targets No: needs manual flags

Build System vs. Generator

Build System
Executes the actual compilation steps. Examples: Make, Ninja, MSBuild.
Build System Generator
Creates the files that a build system reads. Examples: CMake, Meson, Premake.

Why Developers Use CMake

1
Cross-platform: One CMakeLists.txt works on Windows, macOS, and Linux without changes.
2
Dependency management: CMake can locate and link libraries automatically with find_package().
3
Out-of-source builds: Build artifacts go in a separate folder, keeping your source tree clean.
4
IDE integration: CLion, Visual Studio, and VS Code all understand CMake natively.
5
Industry standard: Used by Qt, OpenCV, LLVM, KDE, and thousands of open-source projects.

Anatomy of CMakeLists.txt

# Minimum CMake version required
cmake_minimum_required(VERSION 3.16)

# Name your project
project(MyApp)

# Create an executable from source files
add_executable(my_app main.cpp utils.cpp math.cpp)

The CMake Workflow

1
Write a CMakeLists.txt describing your project targets and dependencies.
2
Configure by running cmake -S . -B build to generate build files in a build/ directory.
3
Build by running cmake --build build to compile and link your project.
4
Run the resulting executable from the build directory.
Write Configure Build Run

Example: Single File Project

The simplest possible project: one source file, one executable.

# CMakeLists.txt
cmake_minimum_required(VERSION 3.16)
project(HelloWorld)
add_executable(hello main.cpp)
# Terminal commands
mkdir build && cd build
cmake ..
make
./hello

Example: Multi-File Project

When your project grows, you simply list more source files or use libraries.

# CMakeLists.txt
cmake_minimum_required(VERSION 3.16)
project(Calculator)

# Create a library from shared code
add_library(math_lib math.cpp utils.cpp)

# Create the main executable
add_executable(calc main.cpp)

# Link the library to the executable
target_link_libraries(calc PRIVATE math_lib)

Example: External Libraries

CMake's find_package searches for pre-installed libraries and sets up all include paths and link flags automatically.

cmake_minimum_required(VERSION 3.16)
project(ImageViewer)

# Find OpenCV, requiring specific components
find_package(OpenCV REQUIRED COMPONENTS core highgui)

add_executable(viewer main.cpp)

# Link using modern imported targets
target_link_libraries(viewer PRIVATE OpenCV::core OpenCV::highgui)

Key CMake Commands

project()
Declares the project name, version, and language (C, C++, etc.).
add_executable()
Defines a build target: an executable program from listed source files.
add_library()
Creates a reusable library (static or shared) from source files.
target_link_libraries()
Connects a target to its dependencies: other libraries or external packages.

Common Mistakes

Building in source
Running cmake . in the source directory clutters it with build files. Always use a separate build directory.
Thinking CMake compiles
CMake generates build files. The actual compilation is done by the compiler toolchain (g++, clang++, MSVC).
Hardcoding paths
Writing absolute paths to libraries breaks portability. Use find_package() and CMake variables instead.

Summary Table

Concept Key Point
What CMake is A cross-platform build system generator
What it produces Makefiles, Ninja files, IDE projects
Config file CMakeLists.txt
Core workflow Write → Configure → Build → Run
Key advantage One description, every platform
Does NOT do Compile code (that is the compiler's job)

Question 1 of 5

What is the primary purpose of CMake?

Question 2 of 5

What is the name of the configuration file that CMake reads?

Question 3 of 5

Which of the following is NOT something CMake can generate?

Question 4 of 5

What does the CMake command find_package() do?

Question 5 of 5

Why is it recommended to use an out-of-source build directory (e.g., build/) with CMake?