Understanding
CMake
Learn what CMake is, why every C++ developer needs it, and how it automates the build process across platforms.
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.
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.
The Core Concept
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
Why Developers Use CMake
Anatomy of CMakeLists.txt
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
Example: Single File Project
The simplest possible project: one source file, one executable.
cmake_minimum_required(VERSION 3.16)
project(HelloWorld)
add_executable(hello main.cpp)
mkdir build && cd build
cmake ..
make
./hello
Example: Multi-File Project
When your project grows, you simply list more source files or use libraries.
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.
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
Common Mistakes
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?