CMake for C++: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction to CMake: What It Is and Why You Need It



CMake (Cross-Platform Make) is a powerful open-source cross-platform system for automating software build processes. Unlike traditional Makefiles, CMake does not build the project directly; instead, it generates build files for native tools (Make, Ninja, Visual Studio, Xcode) based on simple configuration files called CMakeLists.txt.



Why do you need CMake?

  • Cross-platform: The same code can be built on Windows, Linux, and macOS without changing the configuration.
  • Dependency management: Built-in support for finding and linking external libraries (Boost, OpenCV, Qt, etc.).
  • Flexibility: Support for various project generators (Visual Studio, Xcode, Ninja, Unix Makefiles).
  • Modularity: Ability to create libraries (static and dynamic), executables, and tests.
  • IDE integration: Modern IDEs (CLion, VS Code, Qt Creator) have built-in CMake support.


CMake has become the de facto standard for C++ projects. It is used by giants such as Google (TensorFlow, Chrome), Microsoft (VS Code), KDE, LLVM/Clang, and many others.



Installing CMake



Unlike Python libraries, CMake is not installed via pip. It is a standalone application. Let's look at installation for the main operating systems:



Windows

  1. Download the installer from the official website (choose the Windows version — .msi or .zip).
  2. Run the installer. Be sure to check the option "Add CMake to the system PATH for all users".
  3. After installation, verify: cmake --version in the command line.


Linux (Ubuntu/Debian)

sudo apt updatesudo apt install cmakecmake --version


macOS (Homebrew)

brew install cmakecmake --version


Verifying Installation

After installation, run in the terminal:

cmake --version# Output should look like:# cmake version 3.27.0


Quick Start: Your First CMake Project



Let's create a simple "Hello World" project using CMake.



Project Structure

my_project/├── CMakeLists.txt└── main.cpp


File main.cpp

#include <iostream>

int main() { std::cout << "Hello, CMake!" << std::endl; return 0;}


File CMakeLists.txt

cmake_minimum_required(VERSION 3.10)project(HelloWorld VERSION 1.0 LANGUAGES CXX)

add_executable(hello main.cpp)


Building the Project

mkdir buildcd buildcmake ..cmake --build ../hello


Explanation:

  • cmake_minimum_required — sets the minimum CMake version.
  • project — defines the project name and language (CXX for C++).
  • add_executable — creates an executable file from source files.


Key CMake Methods and Functions



1. cmake_minimum_required()

Description: Sets the minimum CMake version required to build the project. If the CMake version is lower than specified, an error will be issued.

Parameters:

  • VERSION — version in the format major.minor[.patch[.tweak]]
  • FATAL_ERROR — optional, causes a fatal error if the version does not match

Example:

cmake_minimum_required(VERSION 3.16 FATAL_ERROR)


2. project()

Description: Defines the project name and its parameters. Must be

Recommendations