Lesson 1 · Foundations

What is CMake and why use it

A plain-language introduction to CMake for C and C++ developers. What a build system is, what CMakeLists.txt does, and why CMake is the standard.

CMake is a build system generator.

To understand that, you need two things: what a build system is, and what “generator” adds to it.

What is a build system

A build system is the software that turns your source code into a program.

Your .cpp files are not a program yet. Something has to compile them, link them, and produce an executable. That something is the build system.

Different platforms use different build systems:

PlatformBuild systemIts configuration files
Linuxmake (or Ninja)Makefile, or build.ninja
WindowsMSBuild.vcxproj, .sln
macOSXcode.xcodeproj

What CMake does

You write one file that describes your project, called CMakeLists.txt. CMake reads it and writes the build files that your platform’s build system needs:

Your CMakeLists.txt


     CMake

        ├──► Linux:   Makefile or build.ninja
        ├──► Windows: Visual Studio solution (.sln)
        └──► macOS:   Xcode project

Same description, every platform. Move the project to another system, run CMake again, and you get that system’s build files. You never write a Makefile, .sln or Xcode project by hand.

What CMake does not do

CMake does not compile or link your code. Its job stops at the build files. Compiling is the build system’s job, which is why you run two separate steps:

  1. Configure. CMake reads CMakeLists.txt and writes the build files.
  2. Build. The build system (make, Ninja, MSBuild) compiles your code.

You will run both steps constantly, starting in lesson 3.

Why CMake is the standard

  • It works everywhere. When a library says “build from source”, the instructions almost always start with CMake. If you know CMake, you can build almost anything.
  • Libraries provide CMake files. A library ships files that let your project find and link it in a few lines (lesson 17).
  • It scales. The same approach works for a three-file program and for huge projects like LLVM.
  • Your IDE reads it. VS Code, CLion, Visual Studio and Qt Creator all understand CMake. Add a file there and your IDE knows about it immediately.

Common questions

Is CMake only for C and C++?

No. CMake supports C, C++, Fortran, CUDA and more. C and C++ are its main use, and this tutorial focuses on C++.

Do I need to learn make or Ninja first?

No. You rarely touch them. CMake writes their files for you, and you build with cmake --build.

Does CMake replace my IDE?

No. Your IDE reads CMake. You keep writing code in the IDE, and it understands your project from CMake.

Read the docs