CMake is a free,
cross-platform
Within computing, cross-platform software (also called multi-platform software, platform-agnostic software, or platform-independent software) is computer software that is designed to work in several Computing platform, computing platforms. Some ...
,
software development tool
A programming tool or software development tool is a computer program that is used to develop another computer program, usually by helping the developer manage computer files. For example, a programmer may use a tool called a source code editor ...
for
building
A building or edifice is an enclosed Structure#Load-bearing, structure with a roof, walls and window, windows, usually standing permanently in one place, such as a house or factory. Buildings come in a variety of sizes, shapes, and functions, a ...
applications via
compiler
In computing, a compiler is a computer program that Translator (computing), translates computer code written in one programming language (the ''source'' language) into another language (the ''target'' language). The name "compiler" is primaril ...
-independent instructions. It also can
automate testing,
packaging
Packaging is the science, art and technology of enclosing or protecting products for distribution, storage, sale, and use. Packaging also refers to the process of designing, evaluating, and producing packages. Packaging can be described as a coo ...
and
installation. It runs on a variety of
platforms and supports many
programming languages
A programming language is a system of notation for writing computer programs.
Programming languages are described in terms of their syntax (form) and semantics (meaning), usually defined by a formal language. Languages usually provide features ...
.
As a meta-build tool, CMake configures
native build tools which in turn build the
codebase
In software development, a codebase (or code base) is a collection of source code used to build a particular software system, application, or software component. Typically, a codebase includes only human-written source code system files; thu ...
. CMake generates
configuration file
A configuration file, a.k.a. config file, is a computer file, file that stores computer data, data used to configure a software system such as an application software, application, a server (computing), server or an operating system.
Some applic ...
s for other build tools based on CMake-specific configuration files. The other tools are responsible for more directly building; using the generated files. A single set of CMake-specific configuration files can be used to build a codebase using the native build tools of multiple platforms.
Notable native build tools supported by CMake include:
Make,
Qt Creator
Qt Creator is a cross-platform C++, JavaScript, Python and QML integrated development environment (IDE) which simplifies GUI application development. It is part of the SDK for the Qt GUI application development framework and uses the Q ...
,
Ninja
A , or was a spy and infiltrator in pre-modern Japan. The functions of a ninja included siege and infiltration, ambush, reconnaissance, espionage, deception, and later bodyguarding.Kawakami, pp. 21–22 Antecedents may have existed as ear ...
,
Android Studio
Android Studio is the official integrated development environment (IDE) for Google's Android operating system, built on JetBrains' IntelliJ IDEA software and designed specifically for Android development. This is available for download on W ...
,
Xcode
Xcode is a suite of developer tools for building apps on Apple devices. It includes an integrated development environment (IDE) of the same name for macOS, used to develop software for macOS, iOS, iPadOS, watchOS, tvOS, and visionOS. It w ...
, and
Visual Studio.
CMake is distributed as
free and open-source software
Free and open-source software (FOSS) is software available under a license that grants users the right to use, modify, and distribute the software modified or not to everyone free of charge. FOSS is an inclusive umbrella term encompassing free ...
under a permissive
BSD-3-Clause license.
History
Initial development began in 1999 at
Kitware with funding from the
United States National Library of Medicine
The United States National Library of Medicine (NLM), operated by the United States federal government, is the world's largest medical library.
Located in Bethesda, Maryland, the NLM is an institute within the National Institutes of Health. I ...
as part of the
Visible Human Project.
CMake was first released in 2000.
CMake was developed to support building the
Insight Segmentation and Registration Toolkit (ITK) for multiple platforms. Stated goals included addressing weaknesses while maintaining strengths of contemporary tools such as
autoconf and
libtool, and to align with state of the art build technology of the time:
configure scripts and
Make files for
Unix
Unix (, ; trademarked as UNIX) is a family of multitasking, multi-user computer operating systems that derive from the original AT&T Unix, whose development started in 1969 at the Bell Labs research center by Ken Thompson, Dennis Ritchie, a ...
platforms, and
Visual Studio project files for
Windows
Windows is a Product lining, product line of Proprietary software, proprietary graphical user interface, graphical operating systems developed and marketed by Microsoft. It is grouped into families and subfamilies that cater to particular sec ...
.
CMake was inspired by multiple contemporary tools. pcmaker developed by Ken Martin and others to support building the
Visualization Toolkit (VTK) converted Unix Make files into NMake files for building on Windows.
gmake supported Unix and Windows compilers, but its design led to issues that were hard to resolve. Both tools were working examples of a build tool that supported both Unix and Windows, but they suffered from a serious flaw: they required Windows developers to use the command line even though many prefer to use an integrated development environment (IDE) such as Visual Studio.
CMake was to provide similar cross-platform support but to better satisfy the preferences of the developers on each platform.
The design goals of the first version included:
* Depend only on host C++ compiler; no other third-party tools or libraries required
* Generate Visual Studio project files (as well as Unix files)
* Support building targets:
program,
static library
A static library or statically linked library contains functions and data that can be included in a consuming computer program at build-time such that the library does not need to be accessible in a separate file at run-time. If all libraries a ...
,
shared library
In computing, a library is a collection of System resource, resources that can be leveraged during software development to implement a computer program. Commonly, a library consists of executable code such as compiled function (computer scienc ...
* Run build-time code generators
* Support separate
directory trees for
source vs. build files
* Support host computer capability introspection
* Support automatic dependency scanning of C/C++ header files
* All features must work consistently and equally well on all supported platforms
For various reasons, CMake developers chose to develop a scripting language for CMake instead of using
Tcl a popular language for building at the time. Use of Tcl would have then added a dependency to the host machine which is counter to the goal of no dependencies other than a compiler. Also, Tcl was not well supported on Windows and some Unix systems at the time of initial development.
Subsequent development and improvements were fueled by the incorporation of CMake into developers’ own systems, including the
VXL Project, the CABLE features added by Brad King, and
GE Corporate R&D for support of DART. Additional features were created when VTK transitioned to CMake for its build environment and for supporting
ParaView.
Version 3.0 was released in June 2014. It has been described as the beginning of "Modern CMake". Experts now advise to avoid variables in favor of ''targets'' and ''properties''.
The commands , , , that were at the core of CMake 2 should now be replaced by target-specific commands.
Name
CMake developer Brad King stated that "the 'C' in CMake stands for 'cross-platform.
Features
Generators and IDE support
CMake can generate project files for
IDEs like
Microsoft Visual Studio
Visual Studio is an integrated development environment (IDE) developed by Microsoft. It is used to develop computer programs including websites, web apps, web services and mobile apps. Visual Studio uses Microsoft software development platforms ...
,
Xcode
Xcode is a suite of developer tools for building apps on Apple devices. It includes an integrated development environment (IDE) of the same name for macOS, used to develop software for macOS, iOS, iPadOS, watchOS, tvOS, and visionOS. It w ...
,
Eclipse CDT and build scripts for MSBuild or NMake on Windows;
Unix Make on Unix-like platforms such as
Linux
Linux ( ) is a family of open source Unix-like operating systems based on the Linux kernel, an kernel (operating system), operating system kernel first released on September 17, 1991, by Linus Torvalds. Linux is typically package manager, pac ...
,
macOS
macOS, previously OS X and originally Mac OS X, is a Unix, Unix-based operating system developed and marketed by Apple Inc., Apple since 2001. It is the current operating system for Apple's Mac (computer), Mac computers. With ...
, and
Cygwin; and
Ninja
A , or was a spy and infiltrator in pre-modern Japan. The functions of a ninja included siege and infiltration, ambush, reconnaissance, espionage, deception, and later bodyguarding.Kawakami, pp. 21–22 Antecedents may have existed as ear ...
on both Windows and Unix-like platforms by specifying generator for a platform-specific build tool. By default, CMake automatically determines default generator for the host environment it runs on. Command line option can be used to specify alternative generator. E.g. forces CMake to create build scripts for
make.
CMake does not support custom generators without modifying the CMake implementation. None-the-less, the CMake source code could be modified to include a custom generator.
Build targets
CMake supports building executables, libraries (e.g. , etc.),
object file
An object file is a file that contains machine code or bytecode, as well as other data and metadata, generated by a compiler or assembler from source code during the compilation or assembly process. The machine code that is generated is kno ...
libraries and pseudo-targets (including aliases). CMake can produce object files that can be linked against by executable binaries/libraries, avoiding dynamic (run-time) linking and using static (compile-time) linking instead. This enables flexibility in configuration of various optimizations.
Target generation can be configured via target properties. With older versions, this was done via -prefixed global variables, but this approach is deprecated.
Hierarchical configuration
CMake configuration files can be structured according the hierarchical structure of the source code; the source tree. A in a root source directory serves as the root of the configuration. It may include sub-directories which each contain a . Repeating this, results in a hierarchical structure of configuration that follows the structure of the source code.
Separate build tree
CMake can store generated files (both by CMake and the native build tools) in a
directory tree that is separate from the source tree.
This enables multiple builds from the same source tree since each has non-overlapping file system space. This may be leveraged to build different or even incompatible configurations such as for different platforms.
This also simplifies file management by allowing removing generated files by deleting a single directory tree instead of removing multiple files and directories throughout the source tree. This tends to prevent accidentally deleting source files or accidentally adding generated files to
source control.
Dependency management
CMake ensures that downstream components are re-built when its sources are changed or built.
Flexible project structure
CMake can locate system-wide and user-specified executables, files, and libraries. These locations are stored in a
cache, which can then be tailored before generating the target build files. The cache can be edited with a graphical editor, which is shipped with CMake.
Complicated directory hierarchies and applications that rely on several libraries are well supported by CMake. For instance, CMake is able to accommodate a project that has multiple toolkits, or libraries that each have multiple directories. In addition, CMake can work with projects that require executables to be created before generating code to be compiled for the final application. Its open-source, extensible design allows CMake to be adapted as necessary for specific projects.
Compiler feature detection
CMake allows specification of features that the compiler is required to support in order to get the target program or library compiled.
Compiler support
CMake supports many compilers, including: Apple Clang,
Clang
Clang () is a compiler front end for the programming languages C, C++, Objective-C, Objective-C++, and the software frameworks OpenMP, OpenCL, RenderScript, CUDA, SYCL, and HIP. It acts as a drop-in replacement for the GNU Compiler ...
,
GNU GCC,
MSVC
Microsoft Visual C++ (MSVC) is a compiler for the C, C++, C++/CLI and C++/CX programming languages by Microsoft. MSVC is proprietary software; it was originally a standalone product but later became a part of Visual Studio and made available ...
,
Oracle Developer Studio, and
Intel C++ Compiler.
Packaging
CMake can produce packages that can be consumed both by end-user and third-party cmake-based project. Via CPack, built files may be packed into an
archive file
In computing, an archive file stores the content of one or more files, possibly compressed, with associated metadata such as file name, directory structure, error detection and correction information, commentary, compressed data archives, sto ...
for a target system's package manager (e.g.
dpkg) or
installer supported by the target platform.
CMake provides functions for pulling packages from a remote server that can be used as part of the build process or link previously installed cmake packages.
GUI
Cmake may be run by using a
ncurses program like that can be used to configure projects via command-line interface.
Precompiled headers and modules
It's possible to generate
precompiled headers via CMake since version 3.6. As of version 3.28, CMake can also compile
modules, but not header units.
JSON strings
CMake supports extracting values into variables from
JSON
JSON (JavaScript Object Notation, pronounced or ) is an open standard file format and electronic data interchange, data interchange format that uses Human-readable medium and data, human-readable text to store and transmit data objects consi ...
-data strings (since version 3.19).
Language
CMake includes an
interpreter for a relatively simple, custom, imperative
scripting language
In computing, a script is a relatively short and simple set of instructions that typically automation, automate an otherwise manual process. The act of writing a script is called scripting. A scripting language or script language is a programming ...
that supports variables, string manipulation, arrays, function and macro declaration, and module inclusion (importing).
The interpreter reads CMake language commands from files named which specify source files and build preferences. CMake uses this information to generate native tool configuration files. Additionally, files with suffix can be used for storing additional script.
Command syntax
CMake language commands are formatted as:
''name''(''argument'' ...)
Arguments are
whitespace-separated and can include keywords to separate groups of arguments. For instance, in the following command, the keyword delimits a list of source files from compiler flags.
set_source_file_properties(''filename'' ... COMPILE_FLAGS ''compile_flag'' ...)
Implementation
The CMake scripting language is implemented via
Yacc
Yacc (Yet Another Compiler-Compiler) is a computer program for the Unix operating system developed by Stephen C. Johnson. It is a lookahead left-to-right rightmost derivation (LALR) parser generator, generating a LALR parser (the part of a co ...
and
Lex generators.
The executable programs CMake, CPack, and CTest are written in
C++.
Much of CMake's functionality is implemented in modules written in the
CMake language.
CMake documentation (since release 3.0) uses
reStructuredText markup. HTML pages and man pages are generated by the
Sphinx
A sphinx ( ; , ; or sphinges ) is a mythical creature with the head of a human, the body of a lion, and the wings of an eagle.
In Culture of Greece, Greek tradition, the sphinx is a treacherous and merciless being with the head of a woman, th ...
documentation generator.
Additional tools
CMake ships with numerous script files and development tools that facilitate tasks such as finding dependencies (both built-in and external, e.g. modules), testing the toolchain environment and executables, packaging releases (CPack), and managing dependencies on external projects ( module). Additional development tools include:
* ccmake and cmake-gui — for updating configuration variables intended for a native build tool
* CPack — for packaging software as Linux
RPM,
deb, and
gzip
gzip is a file format and a software application used for file compression and decompression. The program was created by Jean-loup Gailly and Mark Adler as a free software replacement for the compress program used in early Unix systems, and ...
packages,
NSIS files (for Windows), and
macOS
macOS, previously OS X and originally Mac OS X, is a Unix, Unix-based operating system developed and marketed by Apple Inc., Apple since 2001. It is the current operating system for Apple's Mac (computer), Mac computers. With ...
packages
* CTest and CDash — for software testing and reporting
Adoption
CMake has been very widely adopted among commercial, open source, and academic software projects. A few notable users include
Android NDK
The Android Native Development Kit (NDK) provides a cross-compiling tool for compiling code written in C (programming language), C/C++ can be compiler, compiled to ARM architecture, ARM, or x86 native code (or their x86-64, 64-bit variants) for An ...
,
Netflix
Netflix is an American subscription video on-demand over-the-top streaming service. The service primarily distributes original and acquired films and television shows from various genres, and it is available internationally in multiple lang ...
,
Inria
The National Institute for Research in Digital Science and Technology (Inria) () is a French national research institution focusing on computer science and applied mathematics.
It was created under the name French Institute for Research in Comp ...
,
MySQL
MySQL () is an Open-source software, open-source relational database management system (RDBMS). Its name is a combination of "My", the name of co-founder Michael Widenius's daughter My, and "SQL", the acronym for Structured Query Language. A rel ...
,
Boost (C++ libraries)
Boost, boosted or boosting may refer to:
Science, technology and mathematics
* Boost, positive manifold pressure in Turbocharger, turbocharged engines
* Boost (C++ libraries), a set of free peer-reviewed portable C++ libraries
* Boost (material), ...
,
KeePassXC,
KDE
KDE is an international free software community that develops free and open-source software. As a central development hub, it provides tools and resources that enable collaborative work on its projects. Its products include the KDE Plasma gra ...
,
KiCAD
KiCad ( ) is a free software suite for electronic design automation (EDA). It facilitates the design and simulation of electronic hardware for PCB manufacturing. It features an integrated environment for schematic capture, Printed circuit boar ...
,
FreeCAD,
Webkit
WebKit is a browser engine primarily used in Apple's Safari web browser, as well as all web browsers on iOS and iPadOS. WebKit is also used by the PlayStation consoles starting with the PS3, the Tizen mobile operating systems, the Amazon K ...
,
Blender, Biicode,
ReactOS
ReactOS is a Free and open-source software, free and open-source operating system for i586/amd64 personal computers that is intended to be binary-code compatibility, binary-compatible with computer programs and device drivers developed for Wind ...
,
Apache Qpid, the
ATLAS experiment, and
Second Life
''Second Life'' is a multiplayer virtual world that allows people to create an Avatar (computing), avatar for themselves and then interact with other users and user-created content within a multi-user online environment. Developed for person ...
.
Build process
Building via CMake has a two major stages.
First, native build tool configuration files are generated from CMake configuration files written in the CMake scripting language. The command line syntax is where
is a directory that contains a file. Then, the native build tools are invoked either via CMake () or directly via the native tool's interface. The native build tools use the generated files.
Examples
Hello world
The following demonstrates configuring CMake to build a hello world program written in C++, and using CMake to build the program.
hello.cpp:
#include
int main()
CMakeLists.txt:
cmake_minimum_required(VERSION 3.22)
project(HelloWorld CXX)
add_executable(hello hello.cpp)
To build via CMake, first cd to the directory containing the two files above. Then, generate the native build config files via the cross-platform CMake command:
cmake -B out .
All generated files will be under the directory ''out'' as specified via .
Then, build via the native build tool as supported thru CMake:
cmake --build out
The program is then available for running. Via Bash, the command is like . On Windows, the output file ends with .
Include
This example demonstrates configuring the preprocessor include path.
hello.cpp:
#include "hello.h"
#include
int main()
hello.h:
const int Times = 10;
CMakeLists.txt:
cmake_minimum_required(VERSION 3.22)
project(HelloWorld CXX)
include_directories($)
add_executable(hello hello.cpp)
See also
*
References
External links
* {{Official website
2000 software
Build automation
Compiling tools
Software that uses Qt
Software using the BSD license