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.
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.
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.
.cpp separately, then link them together.The CMake Workflow
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.
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.
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
main.cpp and CMakeLists.txt.cmake_minimum_required, project, and add_executable.cmake -B build from the project root to generate build files.cmake --build build to compile your executable../build/my_app (Linux/macOS) or build\Debug\my_app.exe (Windows).Example 1: Hello World
Example 2: Multiple Source Files
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.
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.
Key CMake Commands
VERSION, LANGUAGES.CMAKE_CXX_STANDARD.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.
Common Mistakes
cmake . in the source directory mixes generated files with your code. Always use cmake -B build..cpp files in add_executable causes undefined reference errors at link time..h files to add_executable is unnecessary and can cause confusion. Only list .cpp files.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?
#include. Option A is not ideal as it unnecessarily lists headers. Option C misses a source file. Option D uses a glob pattern (*.cpp), which is strongly discouraged because new files may not be detected automatically.Question 4 of 5
Why should you always use an out-of-source build (e.g., cmake -B build) instead of running cmake .?
rm -rf build/ to start fresh.Question 5 of 5
Which CMake variable sets the C++ language standard for all targets in the project?