01 / 21

CMake
Basics

Learn to configure and build C++ projects using CMake: the industry-standard cross-platform build system that powers everything from small utilities to massive codebases.

C++

What Is CMake?

CMake is a build system generator: it reads a configuration file you write and produces native build files for your platform: Makefiles on Linux, Visual Studio projects on Windows, Xcode projects on macOS, and more.

Cross-Platform
One config works on Linux, macOS, and Windows.
Industry Standard
Used by Qt, LLVM, KDE, OpenCV, and thousands of projects.

Why Not Just Use g++ Directly?

For a single-file program, running g++ main.cpp works fine. But real projects have dozens of source files, libraries, and compiler flags. Managing all of this by hand quickly becomes unmanageable.

1
Multiple source files: You need to compile each .cpp separately, then link them together.
2
External libraries: Finding and linking libraries like Boost or SDL requires platform-specific flags.
3
Incremental builds: Recompiling only changed files saves massive time on large projects.

The CMake Workflow

WRITE CMakeLists.txt CONFIGURE cmake -B build BUILD cmake --build build RUN Step 1 Step 2 Step 3 Step 4

The CMakeLists.txt File

Every CMake project needs at least one CMakeLists.txt file in the project root. This file contains commands that tell CMake what to build and how.

CMakeLists.txt (minimal)
cmake_minimum_required(VERSION 3.16) project(MyApp) add_executable(my_app main.cpp)
cmake_minimum_required
Sets the minimum CMake version. Ensures compatibility.
project()
Names your project. Also sets language defaults (C, C++).
add_executable
Creates a build target from the listed source files.

Project Structure

CMake encourages out-of-source builds: you keep generated files in a separate build/ directory so they don't pollute your source tree.

my_project/ CMakeLists.txt main.cpp build/ ← generated files go here

CMake vs. Make vs. Compiler

CMake Make g++ / clang++
Role Build system generator Build executor Compiler
Input CMakeLists.txt Makefile .cpp / .h files
Output Makefile / .sln / etc. Binary / .o files .o / executable
Cross-platform? Yes No (Linux/macOS) Varies

Step-by-Step: Your First Build

1
Create a project folder with main.cpp and CMakeLists.txt.
2
Write your CMakeLists.txt with the three essential commands: cmake_minimum_required, project, and add_executable.
3
Configure: Run cmake -B build from the project root to generate build files.
4
Build: Run cmake --build build to compile your executable.
5
Run: Execute ./build/my_app (Linux/macOS) or build\Debug\my_app.exe (Windows).

Example 1: Hello World

main.cpp
#include <iostream> int main() { std::cout << "Hello, CMake!" << std::endl; return 0; }
CMakeLists.txt
cmake_minimum_required(VERSION 3.16) project(HelloWorld) add_executable(hello main.cpp)

Example 2: Multiple Source Files

calculator/ CMakeLists.txt main.cpp math_utils.cpp math_utils.h
CMakeLists.txt
cmake_minimum_required(VERSION 3.16) project(Calculator) add_executable(calc main.cpp math_utils.cpp )

Example 3: Setting the C++ Standard

Modern C++ features (like auto, lambdas, std::optional) require specifying the language standard. CMake makes this clean and portable.

CMakeLists.txt
cmake_minimum_required(VERSION 3.16) project(ModernApp) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) add_executable(modern_app main.cpp)
CMAKE_CXX_STANDARD
Sets the desired C++ version: 11, 14, 17, 20, or 23.
STANDARD_REQUIRED ON
Forces an error if the compiler doesn't support the requested standard.

Inside the Build Directory

After running cmake -B build, the build/ folder contains generated files. You should never edit these: they are regenerated each time you reconfigure.

build/ CMakeCache.txt Makefile CMakeFiles/ Cached variables (paths, options) Build instructions (or .sln on Windows) Internal CMake working files my_app (executable) Created after running cmake --build build

Key CMake Commands

1
cmake_minimum_required(VERSION x.y): Declares the oldest CMake version your project supports.
2
project(Name): Names the project. Optionally sets VERSION, LANGUAGES.
3
add_executable(target src1 src2 ...): Defines a binary target from source files.
4
set(VAR value): Sets a variable, like CMAKE_CXX_STANDARD.
5
target_link_libraries(target lib): Links a library to your target. Essential for external dependencies.

target_link_libraries In Action

When your code uses an external library (like the math library or a threading library), you need to tell CMake to link it.

CMakeLists.txt (linking pthread)
cmake_minimum_required(VERSION 3.16) project(ThreadDemo) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) find_package(Threads REQUIRED) add_executable(thread_demo main.cpp) target_link_libraries(thread_demo PRIVATE Threads::Threads)

Common Mistakes

✕ In-Source Build
Running cmake . in the source directory mixes generated files with your code. Always use cmake -B build.
✕ Forgetting Sources
Not listing all .cpp files in add_executable causes undefined reference errors at link time.
✕ Listing Headers
Adding .h files to add_executable is unnecessary and can cause confusion. Only list .cpp files.
✕ Wrong File Name
The file must be named exactly CMakeLists.txt (capital C, M, L). cmakelists.txt will be silently ignored.

CMake Cheat Sheet

Command What It Does
cmake -B build Configure: generate build files into build/
cmake --build build Build: compile the project
cmake --build build --clean-first Clean then rebuild from scratch
cmake -B build -DCMAKE_BUILD_TYPE=Release Configure with optimizations enabled
cmake -B build -G Ninja Use Ninja instead of Make as the generator

Question 1 of 5

What does CMake directly produce when you run cmake -B build?

Question 2 of 5

Which three commands are the minimum required in a CMakeLists.txt file?

Question 3 of 5

Your project has main.cpp, utils.cpp, and utils.h. Which add_executable call follows best practice?

Question 4 of 5

Why should you always use an out-of-source build (e.g., cmake -B build) instead of running cmake .?

Question 5 of 5

Which CMake variable sets the C++ language standard for all targets in the project?