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(). The full grammar,
argument kinds, escapes, conditions and scopes are on the
language syntax page, and every built-in
command is on the commands page.
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)
Not described
Known, not yet covered
- Reference: the keywords and options of each command beyond the signatures, the generator expressions, the built-in variables, the properties and the policies, all in CMake’s own manuals; they would go on the commands page and new ones.
- Viewer: there is no viewer for CMake files yet, so scripts are not opened, parsed or shown.
Links
Software
- CMake – the build system generator, with
cmakeand thecmake-guiwindow (Windows, macOS, Linux) - Visual Studio – Microsoft’s IDE, which opens CMake projects directly (Windows)
- CLion – JetBrains’ C and C++ IDE, built around CMake (Windows, macOS, Linux)
- Visual Studio Code – code editor that builds CMake projects with the CMake Tools extension (Windows, macOS, Linux)
- Qt Creator – Qt’s IDE, which opens CMake projects (Windows, macOS, Linux)
Reference
- CMake Reference Documentation – every command, variable and module