Introduction
CMake is a cross-platform build system generator,
widely used for C and C++. A project describes itself in files named
CMakeLists.txt, one at the top and usually one in each folder of source.
CMake reads them and writes the native build files for the chosen tool: a
Makefile, a Ninja file, or a Visual Studio or Xcode
project.
Files with the .cmake extension hold reusable code: modules that find
libraries, toolchain files that describe a compiler, and package configuration files
that let other projects use a library.
Structure
A CMake file is plain text in the CMake language. Everything is a command followed by
arguments in parentheses, separated by spaces. Variables are set with
set() and read as ${NAME}. A modern project declares targets
(programs and libraries) and attaches settings to each, such as include folders,
compile options and the other targets it links to. Control flow uses commands too:
if(), foreach() and function().
cmake_minimum_required(VERSION 3.20)
project(App LANGUAGES CXX)
add_library(util util.cpp)
add_executable(app main.cpp)
target_link_libraries(app PRIVATE util)
Links
Software
- CMake, with
cmakeand thecmake-guiwindow (Windows, macOS, Linux) - Visual Studio, which opens CMake projects directly (Windows)
- CLion (Windows, macOS, Linux)
- Visual Studio Code, with the CMake Tools extension (Windows, macOS, Linux)
- Qt Creator (Windows, macOS, Linux)