Caio Wakamatsu

CMake from scratch / Chapter 2

Your First CMakeLists.txt

The structure we’ll start building off of.

cmake_minimum_required(VERSION 3.20)
set(CMAKE_CXX_STANDARD 20)
project(hello-cmake)

add_executable(hello main.cpp)

Let’s go through what each line is / does.

Minimum version

cmake_minimum_required(VERSION 3.20)

This tells the CMake installed that it must be at least version 3.20. This guide requires 3.20.

C++ standard

set(CMAKE_CXX_STANDARD 20)

This tells the C++ compiler we use to utilise C++20.

Project naming

project(hello-cmake)

This tells CMake that the project that it’s currently “managing” is named hello-cmake

Creating the executable

add_executable(hello main.cpp) This line is doing quite a lot, we’ll go a bit deeper on each part.

  1. add_executable: This tells CMake to “create this executable”
  2. hello: Is the name of the executable, notably; project name does not need to equal executable name.
  3. main.cpp: The name of the file that this executable uses.