cmake
Generate build files from CMakeLists.txt
TLDR
Generate a build recipe in the current directory with CMakeLists.txt from a project directory
Use a generated recipe in a given directory to build artifacts
Install the build artifacts into /usr/local/ and strip debugging symbols
Generate a build recipe, with build type set to Release with CMake variable
Generate a build recipe using generator_name as the underlying build system
Install the build artifacts using a custom prefix for paths
Run a custom build target
Display help
SYNOPSIS
Configuration:
cmake [options] <source-dir>
cmake [options] -S <source-dir> -B <build-dir>
Building:
cmake --build <build-dir> [options]
Scripting/Utilities:
cmake -P <script-file> [args...]
cmake -E <command> [args...]
cmake --help [options]
PARAMETERS
-S <source-dir>
Specifies the root directory of the source tree containing the CMakeLists.txt file.
-B <build-dir>
Specifies the root directory for the build output. This enables out-of-source builds.
-G <generator-name>
Specifies the build system generator to use (e.g., 'Unix Makefiles', 'Ninja', 'Visual Studio 16 2019').
-D <var>[:<type>]=<value>
Defines a CMake variable for the configuration phase. Useful for setting compiler flags, paths, or enabling/disabling features.
--build <dir>
Builds a CMake-generated project within the specified build directory. This is the recommended way to build once configured.
-P <script-file>
Processes the given CMake language file in script mode, without generating a build system.
-E <command> [args...]
Executes one of CMake's bundled command-line utilities (e.g., copy, rm, path).
--install <dir>
Installs the project built in <dir>, as defined by the install() commands in CMakeLists.txt.
--preset <name>
Uses a named configure or build preset from a CMakePresets.json or CMakeUserPresets.json file.
--help
Displays help information for cmake, including a list of generators or specific options.
DESCRIPTION
CMake is a powerful, cross-platform build system generator designed to manage the compilation process of software using a compiler-independent method. Instead of building the code directly, CMake reads CMakeLists.txt files, which describe the project's source files, dependencies, and build instructions, and generates native build tool files specific to your chosen environment. This includes Makefiles for Unix/Linux, Visual Studio projects on Windows, Xcode projects on macOS, and others. This abstraction allows developers to define a single set of build instructions that work across diverse operating systems and compilers, simplifying complex multi-platform software development.
It supports out-of-source builds, ensuring that generated build files and intermediate objects do not clutter the source directory, promoting clean project structures and easy removal of build artifacts. CMake also offers advanced features like testing frameworks (CTest), packaging tools (CPack), and dependency management.
CAVEATS
Understanding the CMake language (CMakeLists.txt) can have a steep learning curve, especially for complex projects or advanced features like custom commands and dependency management. While CMake aims for cross-platform compatibility, subtle differences in compiler behavior, library paths, and operating system specifics can still lead to platform-dependent issues. Debugging CMakeLists.txt issues often requires careful inspection of the generated build files and configuration output. Improper path handling in CMake scripts can lead to non-portable builds.
<B>OUT-OF-SOURCE BUILDS</B>
A fundamental concept in CMake where the build artifacts (executables, libraries, object files, generated build system files) are created in a directory separate from the source code. This keeps the source tree clean, simplifies cleaning up build artifacts, and allows for multiple build configurations (e.g., debug and release) from the same source tree.
<B>GENERATORS</B>
CMake doesn't build code directly; it generates files for a native build system. These are called generators. Examples include 'Unix Makefiles' (for make), 'Ninja' (for Ninja), and various 'Visual Studio' generators for Windows. The choice of generator often depends on the target platform and developer preference.
<B>CMAKELISTS.TXT</B>
The primary input file for CMake. This text file, written in the CMake language, defines the project's structure, source files, dependencies, build rules, and any custom commands or configurations. CMake processes this file to understand how to generate the native build system.
HISTORY
CMake was initially developed by Kitware Inc. in the early 2000s, primarily to support the Insight Segmentation and Registration Toolkit (ITK) and the Visualization Toolkit (VTK). The need arose from the challenges of managing large, cross-platform software projects that required compilation on various operating systems (Windows, Linux, macOS) with different compilers. Its first public release was in 2001. Over the years, CMake has evolved significantly, adding features like modules, presets, and better dependency management, becoming a de-facto standard for build system generation in C++ and other language communities due to its flexibility and widespread adoption.