Jump to content

CMake: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
ММЛ (talk | contribs)
m History: fix typos
Compilers: Clarify and simplify
 
(49 intermediate revisions by 22 users not shown)
Line 1: Line 1:
{{short description|Cross-platform, compiler-independent build system generator}}
{{short description|Cross-platform build tool for configuring platform-specific builds}}
{{Use dmy dates|date=January 2022}}
{{Use dmy dates|date=January 2022}}
{{Infobox software
{{Infobox software
| name = CMake
| name = CMake
| logo = Cmake.svg
| logo = CMake logo.svg
| logo size = 96px
| logo size = 96px
| screenshot = Cmake with Clang screenshot.png
| screenshot = Cmake with Clang screenshot.png
| developer = Andy Cedilnik, Bill Hoffman, Brad King, Ken Martin, Alexander Neundorf
| developer = Andy Cedilnik, Bill Hoffman, Brad King, Ken Martin, Alexander Neundorf
| released = {{Start date and age|2000}}
| released = {{Start date and age|2000}}
| latest release version = {{wikidata|property|preferred|references|edit|Q1023904|P348|P548=Q2804309}}
| latest release version = {{wikidata|property|preferred|references|edit|Q1023904|P348|P548=Q2804309}}
| latest release date = {{wikidata|qualifier|preferred|single|Q1023904|P348|P548=Q2804309|P577}}
| latest release date = {{wikidata|qualifier|preferred|single|Q1023904|P348|P548=Q2804309|P577}}
| latest preview version = {{wikidata|property|preferred|references|edit|Q1023904|P348|P548=Q51930650}}
| latest preview version = {{wikidata|property|preferred|references|edit|Q1023904|P348|P548=Q1072356}}
| latest preview date = {{wikidata|qualifier|preferred|single|Q1023904|P348|P548=Q51930650|P577}}
| latest preview date = {{wikidata|qualifier|preferred|single|Q1023904|P348|P548=Q1072356|P577}}
| programming language = [[C (programming language)|C]], [[C++]]<ref>{{cite web
| programming language = [[C (programming language)|C]], [[C++]]<ref>{{cite web
| url = https://www.openhub.net/p/cmake
| url = https://www.openhub.net/p/cmake
| title = The CMake Open Source Project on OpenHub
| title = The CMake Open Source Project on OpenHub
| publisher = OpenHub
| publisher = OpenHub
| access-date = 2016-04-09}}</ref>
| access-date = 2016-04-09}}</ref>
| operating system = [[Cross-platform]]
| operating system = [[Cross-platform]]
| genre = [[Software development tools]]
| genre = [[Software development tools]]
| license = [[BSD licenses|BSD-3-Clause]]
| license = [[BSD licenses|BSD-3-Clause]]
}}
}}


In [[software development]], '''CMake''' is [[cross-platform]] [[free and open-source software]] for [[build automation]], [[software testing|testing]], [[package (package management system)|packaging]] and [[Installation (computer programs)|installation]] of software by using a [[compiler]]-independent method.<ref>{{Cite web|url=https://cmake.org/|title=CMake}}</ref> CMake is not a build system itself; it generates another system's build files.<ref name=":1">{{Cite web |title=The Architecture of Open Source Applications (Volume 1)CMake |url=https://aosabook.org/en/v1/cmake.html |access-date=2023-06-11 |website=aosabook.org}}</ref> It supports directory hierarchies and applications that depend on multiple libraries. It is used in conjunction with native build environments such as [[make (software)|Make]], [[Qt Creator]], [[Ninja (build system)|Ninja]], [[Android Studio]], Apple's [[Xcode]], and [[Microsoft Visual Studio]].<ref name=":1" /> It has minimal dependencies, requiring only a [[C++]] compiler on its own build system.<ref name=":1" />
'''CMake''' is a free, [[cross-platform]], [[software development tool]] for [[build automation|building]] [[application software|applications]] via [[compiler]]-independent instructions. It also can [[automation|automate]] [[software testing|testing]], [[package (package management system)|packaging]] and [[Installation (computer programs)|installation]]. It runs on a variety of [[computing platform|platforms]] and supports many [[programming languages]].<ref>{{Cite web|url=https://cmake.org/|title=CMake}}</ref>


As a meta-build tool, CMake configures [[Native (computing)|native]] build tools which in turn build the [[codebase]]. CMake generates [[configuration file]]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.<ref name=":1">{{Cite web |title=The Architecture of Open Source Applications (Volume 1)CMake |url=https://aosabook.org/en/v1/cmake.html |access-date=2023-06-11 |website=aosabook.org}}</ref>
CMake is distributed as [[open-source software]] under a permissive [[BSD licenses|BSD-3-Clause]] license.<ref>{{Cite web|title=Licenses · master · CMake / CMake|url=https://gitlab.kitware.com/cmake/cmake/-/tree/master/Licenses|access-date=2020-11-13|website=GitLab|language=en}}</ref>

Notable native build tools supported by CMake include: [[make (software)|Make]], [[Qt Creator]], [[Ninja (build system)|Ninja]], [[Android Studio]], [[Xcode]], and [[Microsoft Visual Studio|Visual Studio]].<ref name=":1" />

CMake is distributed as [[free and open-source software]] under a permissive [[BSD licenses|BSD-3-Clause]] license.<ref>{{Cite web|title=Licenses · master · CMake / CMake|url=https://gitlab.kitware.com/cmake/cmake/-/tree/master/Licenses|access-date=2020-11-13|website=GitLab|language=en}}</ref>


==History==
==History==
CMake development began in 1999, in response to the need for a cross-platform build environment for the [[Insight Segmentation and Registration Toolkit]] (ITK).<ref>{{cite web |title = FLOSS Weekly 111: CMake |url = http://twit.tv/floss111 |work = podcast |publisher = TWiT Network |access-date = 27 February 2011}}</ref><ref name=":1" /> The project is funded by the [[United States National Library of Medicine]] as part of the [[Visible Human Project]].<ref name=":1" /> It was partially inspired by {{Code|pcmaker}}, a predecessor to CMake, which was made by Ken Martin and other developers to support building of the [[VTK|Visualization Toolkit (VTK)]]. pcmaker was a C program that converted Make files into MS Windows' NMake counterparts<ref name=":1" /> At [[Kitware]], Bill Hoffman blended components of pcmaker with his own ideas, striving to mimic the functionality of [[Unix]] [[configure script]]s. CMake was first implemented in 2000 and further developed in 2001.


Initial development began in 1999 at [[Kitware]] with funding from the [[United States National Library of Medicine]] as part of the [[Visible Human Project]].<ref name=":1" /> CMake was first released in 2000.
Historically CMake was conceived with the following major features in mind:<ref name=":1" />


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 script]]s and [[Make (software)|Make]] files for [[Unix]] platforms, and [[Visual Studio]] project files for [[Microsoft Windows|Windows]].<ref>{{cite web |title = FLOSS Weekly 111: CMake |url = http://twit.tv/floss111 |work = podcast |publisher = TWiT Network |access-date = 27 February 2011}}</ref><ref name=":1" />
* depending only on system C++ compiler, meaning no third-party libraries
* to be able to generate VS Studio IDE input files
* capable to produce executable and linkable binary libraries (static and shared)
* to be able to run build-time code generators
* separate source/build file trees
* system checks and introspection (similar to [[GNU Autotools|Autotool]]<nowiki/>s): what system could and could not do
* automatically scan C/C++ deps
* cross-platform


CMake was inspired by multiple contemporary tools. pcmaker {{endash}} developed by Ken Martin and others to support building the [[VTK|Visualization Toolkit (VTK)]] {{endash}} converted Unix Make files into NMake files for building on Windows.<ref name=":1" /> gmake supported Unix and Windows compilers, but was its design lead to hard to resolve environment issues. 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.
Because of these constraints CMake didn't choose to use [[Tcl]] (popular at the time) scripting language as its default and instead, developers decided to create a simpler scripting language.<ref name=":1" />


CMake was to provide similar cross-platform support but to better satisfy the preferences of the developers on each platform.
Continued development and improvements were fueled by the incorporation of CMake into developers’ own systems, including the [[VXL]] Project,{{clarify|date=August 2016}} the CABLE<ref>{{Cite web |url=http://public.kitware.com/Cable/HTML/Index.html |title=The CABLE |access-date=2010-11-10 |archive-url=https://web.archive.org/web/20130619224333/http://public.kitware.com/Cable/HTML/Index.html |archive-date=2013-06-19 |url-status=dead }}</ref> features added by Brad King,{{clarify|date=August 2016}} and [[GE Global Research|GE Corporate R&D]] for support of DART.{{clarify|date=August 2016}} Additional features were created when VTK transitioned to CMake for its build environment and for supporting [[ParaView]].

The design goals of the first version included:<ref name=":1" />

* 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: [[computer program|program]], [[static library]], [[shared library]]
* Run build-time code generators
* Support separate [[directory tree]]s for [[source code|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]] {{endash}} 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.<ref name=":1" />

Subsequent development and improvements were fueled by the incorporation of CMake into developers’ own systems, including the [[VXL]] Project,{{clarify|date=August 2016}} the CABLE<ref>{{Cite web |url=http://public.kitware.com/Cable/HTML/Index.html |title=The CABLE |access-date=2010-11-10 |archive-url=https://web.archive.org/web/20130619224333/http://public.kitware.com/Cable/HTML/Index.html |archive-date=2013-06-19 |url-status=dead }}</ref> features added by Brad King,{{clarify|date=August 2016}} and [[GE Global Research|GE Corporate R&D]] for support of DART.{{clarify|date=August 2016}} 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.<ref>{{Cite web|url=https://cmake.org/pipermail/cmake/2014-June/057793.html|title=[CMake] [ANNOUNCE] CMake 3.0.0 Released.|first=Robert|last=Maynard|date=June 10, 2014}}</ref> It has been described as the beginning of "Modern CMake".<ref>{{Cite web|url=https://gist.github.com/mbinna/c61dbb39bca0e4fb7d1f73b0d66a4fd1|title=Effective Modern CMake|website=Gist}}</ref> Experts now advise to avoid variables in favor of ''targets'' and ''properties''.<ref name=":0">{{Cite web |last=Binna |first=Manuel |date=Jul 22, 2018 |title=Effective Modern CMake |url=https://gist.github.com/mbinna/c61dbb39bca0e4fb7d1f73b0d66a4fd1}}</ref> The commands {{code|add_compile_options}}, {{code|include_directories}}, {{code|link_directories}}, {{code|link_libraries}} that were at the core of CMake 2 should now be replaced by target-specific commands.
Version 3.0 was released in June 2014.<ref>{{Cite web|url=https://cmake.org/pipermail/cmake/2014-June/057793.html|title=[CMake] [ANNOUNCE] CMake 3.0.0 Released.|first=Robert|last=Maynard|date=June 10, 2014}}</ref> It has been described as the beginning of "Modern CMake".<ref>{{Cite web|url=https://gist.github.com/mbinna/c61dbb39bca0e4fb7d1f73b0d66a4fd1|title=Effective Modern CMake|website=Gist}}</ref> Experts now advise to avoid variables in favor of ''targets'' and ''properties''.<ref name=":0">{{Cite web |last=Binna |first=Manuel |date=Jul 22, 2018 |title=Effective Modern CMake |url=https://gist.github.com/mbinna/c61dbb39bca0e4fb7d1f73b0d66a4fd1}}</ref> The commands {{code|add_compile_options}}, {{code|include_directories}}, {{code|link_directories}}, {{code|link_libraries}} 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{{' "}}.<ref>https://public.kitware.com/Bug/view.php?id=14012#c32631 {{Bare URL inline|date=August 2024}}</ref>


==Features==
==Features==

=== Generators ===
CMake generates configuration files for a particular native build tool via one of its generators. A user can select a generator via the CMake command line.<ref name=":1" />

The build files are configured depending on the generator used (e.g. ''Unix Makefiles'' for [[Make (software)|make]]) and associated toolchain files. Advanced users can also create and incorporate additional makefile generators to support their specific compiler and OS needs. Generated files are typically placed (by using {{code|cmake}}'s flag) into a folder outside of the source's one (out of source build), e.g., {{code|build/}}.

Each build project in turn contains its own{{code|CMakeCache.txt}} file and {{code|CMakeFiles}} directory in every project (sub-)directory of included by the {{code|add_subdirectory(...)}} command, helping to avoid or speed up regeneration when it is run repeatedly.

The generation process and the output can be fine-tuned via target properties. Previously it was done via {{code|CMAKE_...}}-prefixed global variables that are also used to configure CMake itself and to set up initial defaults.<ref name=":0" /><ref>{{Cite conference |last=Pfeifer |first=Daniel |date=May 19, 2017 |title=Effective Cmake |url=https://github.com/boostcon/cppnow_presentations_2017/blob/master/05-19-2017_friday/effective_cmake__daniel_pfeifer__cppnow_05-19-2017.pdf |conference=CPPNow}}</ref> The older approach is discouraged now.

=== Build targets ===
CMake supports building executables, libraries (e.g. {{Code|libxyz}}, {{Code|xyz.dll}} etc.), [[object file]] 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.<ref>{{Cite web|title=cmake-buildsystem(7) — CMake 3.19.0-rc3 Documentation|url=https://cmake.org/cmake/help/latest/manual/cmake-buildsystem.7.html#object-libraries|access-date=2020-11-14|website=cmake.org}}</ref>


=== Separate build tree ===
=== Separate build tree ===
Line 52: Line 79:


=== Dependency management ===
=== Dependency management ===
CMake keeps track and recompiles all the upstream dependencies of a given sub-module if its sources are changed.<ref name=":1" />{{Efn|Upstream dependecies - dependent sources that depend upon a modified source file}}
CMake tracks changes and recompiles upstream dependencies of a given sub-module if its sources are changed.<ref name=":1" />{{Efn|Upstream dependencies - dependent sources that depend upon a modified source file}}


=== Flexible project structure ===
=== Flexible project structure ===
Line 65: Line 92:
CMake allows specification of features that the compiler is required to support in order to get the target program or library compiled.<ref>{{Cite web|date=2020-11-15|title=CMake compiler feature detect|url=https://www.scivision.dev/cmake-compiler-language-feature-detect/|access-date=2022-01-22|website=www.scivision.dev|language=en-us}}</ref>
CMake allows specification of features that the compiler is required to support in order to get the target program or library compiled.<ref>{{Cite web|date=2020-11-15|title=CMake compiler feature detect|url=https://www.scivision.dev/cmake-compiler-language-feature-detect/|access-date=2022-01-22|website=www.scivision.dev|language=en-us}}</ref>


=== Compilers ===
=== Compiler support ===
CMake supports an extensive list of compilers, including: Apple Clang, [[Clang]], [[GNU GCC]], [[Microsoft Visual C++|MSVC]], [[Oracle Developer Studio]], and [[Intel C++ Compiler]].<ref>{{Cite web|title=Supported Compilers|url=https://cmake.org/cmake/help/latest/manual/cmake-compile-features.7.html#supported-compilers|website=CMake Documentation|access-date=January 22, 2022}}</ref>
CMake supports many compilers, including: Apple Clang, [[Clang]], [[GNU GCC]], [[Microsoft Visual C++|MSVC]], [[Oracle Developer Studio]], and [[Intel C++ Compiler]].<ref>{{Cite web|title=Supported Compilers|url=https://cmake.org/cmake/help/latest/manual/cmake-compile-features.7.html#supported-compilers|website=CMake Documentation|access-date=January 22, 2022}}</ref>


=== Packaging system ===
=== Packaging system ===
Even though CMake is not a [[package manager]], it provides basic modules (see CPack) functions for installing binaries and package information declared by the {{code|CMakeList.txt}} script to be used by consumer CMake projects. The package may also be packed into an [[archive file]] for package manager or [[Installation (computer programs)#Installer|installer]] supported by a target platform. Third-party packages may also be imported via configured CMake files which are either provided by the same third-party or created manually.<ref>{{Cite book |last=Berner |first=Dominik |url=https://www.worldcat.org/oclc/1319218167 |title=CMake Best Practices : Discover Proven Techniques for Creating and Maintaining Programming Projects with CMake |date=2022 |publisher=Packt Publishing, Limited |others=Mustafa Kemal Gilor |isbn=978-1-80324-424-2 |location=Birmingham |oclc=1319218167}}</ref>{{Rp|pages=132,142}}<ref>{{Cite web |title=cmake-packages(7) — CMake 3.24.1 Documentation |url=https://cmake.org/cmake/help/latest/manual/cmake-packages.7.html |access-date=2022-09-11 |website=cmake.org}}</ref><ref>{{Cite web |title=Exposing a module's configuration (advanced) — CMake build procedure 1.0 documentation |url=https://docs.salome-platform.org/latest/dev/cmake/html/config.html |access-date=2022-09-11 |website=docs.salome-platform.org}}</ref>
Even though CMake is not a [[package manager]], it provides basic modules (see CPack) functions for installing binaries and package information declared by the {{code|CMakeLists.txt}} script to be used by consumer CMake projects. The package may also be packed into an [[archive file]] for package manager or [[Installation (computer programs)#Installer|installer]] supported by a target platform. Third-party packages may also be imported via configured CMake files which are either provided by the same third-party or created manually.<ref>{{Cite book |last=Berner |first=Dominik |url=https://www.worldcat.org/oclc/1319218167 |title=CMake Best Practices : Discover Proven Techniques for Creating and Maintaining Programming Projects with CMake |date=2022 |publisher=Packt Publishing, Limited |others=Mustafa Kemal Gilor |isbn=978-1-80324-424-2 |location=Birmingham |oclc=1319218167}}</ref>{{Rp|pages=132,142}}<ref>{{Cite web |title=cmake-packages(7) — CMake 3.24.1 Documentation |url=https://cmake.org/cmake/help/latest/manual/cmake-packages.7.html |access-date=2022-09-11 |website=cmake.org}}</ref><ref>{{Cite web |title=Exposing a module's configuration (advanced) — CMake build procedure 1.0 documentation |url=https://docs.salome-platform.org/latest/dev/cmake/html/config.html |access-date=2022-09-11 |website=docs.salome-platform.org}}</ref>


=== GUI ===
=== GUI ===
Cmake may be run by using a [[ncurses]] program like {{Code|ccmake}} that can be used to configure projects via command-line interface.
Cmake may be run by using a [[ncurses]] program like {{Code|ccmake}} that can be used to configure projects via command-line interface.


=== Precompiled headers ===
==Build process==
It's possible to generate [[precompiled header]]s via CMake since version 3.6.<ref>{{Cite web|title=target_precompile_headers — CMake 3.21.20210925-gb818e3c Documentation|url=https://cmake.org/cmake/help/git-stage/command/target_precompile_headers.html|access-date=2021-09-25|website=cmake.org}}</ref>
The build of a program or library with CMake is a two-stage process.<ref name=":1" /> First, build files (usually scripts) are created (generated) from configuration files (CMakeLists.txt scripts) written in [[CMake#Language|CMake language]]. Then the platform's native build tools that can read these build files (native toolchain) are used for actual building of programs (build targets).<ref name="kde" /><ref>{{Cite web|title=cmake-toolchains(7) — CMake 3.19.0-rc2 Documentation|url=https://cmake.org/cmake/help/latest/manual/cmake-toolchains.7.html|access-date=2020-10-29|website=cmake.org}}</ref> The generator specified by the user on the commandline determines which build tool chain to use.<ref name=":1" />


=== Generators ===
=== JSON strings ===
CMake supports extracting values into variables from [[JSON]]-data strings (since version 3.19).<ref>{{Cite web|title=CMake 3.19 Release Notes — CMake 3.19.7 Documentation|url=https://cmake.org/cmake/help/v3.19/release/3.19.html|access-date=2021-03-15|website=cmake.org}}</ref>
The build files are configured depending on the used generator (e.g. ''Unix Makefiles'' for [[Make (software)|make]]) and associated toolchain files. Advanced users can also create and incorporate additional makefile generators to support their specific compiler and OS needs. Generated files are typically placed (by using {{code|cmake}}'s flag) into a folder outside of the source's one (out of source build), e.g., {{code|build/}}.


==Build process==
Each build project in turn contains its own{{code|CMakeCache.txt}} file and {{code|CMakeFiles}} directory in every project (sub-)directory of included by the {{code|add_subdirectory(...)}} command, helping to avoid or speed up regeneration when it is run repeatedly.
Building via CMake has a two major stages.<ref name=":1" /> First, native build tool configuration files are generated from CMake configuration files {{endash}} written in the CMake scripting language. The command line syntax is {{code|cmake <dir>}} where <dir> is a directory that contains a {{code|CMakeLists.txt}} file. Then, the native build tools are invoked either via CMake ({{code|cmake --build}}) or directly via the native tools interface. The native build tools use the generated files.<ref name="kde" /><ref>{{Cite web|title=cmake-toolchains(7) — CMake 3.19.0-rc2 Documentation|url=https://cmake.org/cmake/help/latest/manual/cmake-toolchains.7.html|access-date=2020-10-29|website=cmake.org}}</ref>


== Language ==
The generation process and the output could fine-tuned via target properties. Previously it was done via {{code|CMAKE_...}}-prefixed global variables that are also used to configure CMake itself and to set up initial defaults.<ref name=":0" /><ref>{{Cite conference |last=Pfeifer |first=Daniel |date=May 19, 2017 |title=Effective Cmake |url=https://github.com/boostcon/cppnow_presentations_2017/blob/master/05-19-2017_friday/effective_cmake__daniel_pfeifer__cppnow_05-19-2017.pdf |conference=CPPNow}}</ref> The older approach is discouraged now.
{{Anchor|CMake language}}


CMake includes an [[interpreter (computing)|interpreter]] for a relatively simple, custom, imperative [[scripting language]] that supports variables, string manipulation, arrays, function and macro declaration, and module inclusion (importing).
=== Types of build targets ===
Depending on CMakeLists.txt configuration the build files may be either executables, libraries (e.g. {{Code|libxyz}}, {{Code|xyz.dll}} etc.), [[object file]] libraries or 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.<ref>{{Cite web|title=cmake-buildsystem(7) — CMake 3.19.0-rc3 Documentation|url=https://cmake.org/cmake/help/latest/manual/cmake-buildsystem.7.html#object-libraries|access-date=2020-11-14|website=cmake.org}}</ref>


The interpreter reads CMake language commands from files named {{code|CMakeLists.txt}} which specify source files and build preferences. CMake uses this information to generate native tool configuration files. Additionally, files with suffix {{code|.cmake}} can be used for storing additional script.<ref>{{Cite web|title=cmake-language(7) — CMake 3.19.0-rc2 Documentation|url=https://cmake.org/cmake/help/latest/manual/cmake-language.7.html|access-date=2020-10-29|website=cmake.org}}</ref>
Build dependencies may be determined automatically.


=== Precompiled headers ===
=== Command syntax ===
CMake language commands are formatted as:
It's possible to generate [[precompiled header]]s by using CMake since version 3.6.<ref>{{Cite web|title=target_precompile_headers — CMake 3.21.20210925-gb818e3c Documentation|url=https://cmake.org/cmake/help/git-stage/command/target_precompile_headers.html|access-date=2021-09-25|website=cmake.org}}</ref>


''name''(''argument'' ...)
== {{Anchor|CMake language}}Language ==


Arguments are [[Whitespace character|whitespace]]-separated&nbsp;and can include keywords to separate groups of arguments. For instance, in the following command, the keyword {{code|COMPILE_FLAGS}} delimits a list of source files from compiler flags.<ref>{{cite web|access-date=2021-01-29|url=https://www.linuxjournal.com/article/6700|title=Cross-Platform Software Development Using CMake Software|first=Andrej|last=Cedilnik|publisher=Linux Journal|publication-date=2003-10-30}}</ref>
=== CMakeLists.txt ===
CMake has a relatively simple [[Interpreted language|interpreted]], imperative scripting language. It supports variables, string manipulation methods, arrays, function/macro declarations, and module inclusion (importing). CMake Language commands (or directives) are read by {{code|cmake}} from a file named {{code|CMakeLists.txt}}. This file specifies the source files and build parameters, which CMake will place in the project's build specification (such as a Makefile). Additionally, {{code|.cmake}}-suffixed files can contain scripts used by CMake.<ref>{{Cite web|title=cmake-language(7) — CMake 3.19.0-rc2 Documentation|url=https://cmake.org/cmake/help/latest/manual/cmake-language.7.html|access-date=2020-10-29|website=cmake.org}}</ref>


set_source_file_properties(''filename'' ... COMPILE_FLAGS ''compile_flag'' ...)
To generate a project's build files, one invokes {{code|cmake}} in terminal and specifies the directory that contains {{code|CMakeLists.txt}}. This file contains one or more commands in the form of {{code|COMMAND(argument ...)}}.


Some commonly used commands include:<ref>{{Cite web|title=add_executable — CMake 3.19.0-rc1 Documentation|url=https://cmake.org/cmake/help/latest/command/add_executable.html|access-date=2020-10-25|website=cmake.org}}</ref><ref>{{Cite web|title=add_library — CMake 3.19.0-rc1 Documentation|url=https://cmake.org/cmake/help/latest/command/add_library.html|access-date=2020-10-25|website=cmake.org}}</ref><ref>{{Cite web|title=target_link_directories — CMake 3.20.2 Documentation|url=https://cmake.org/cmake/help/latest/command/target_link_directories.html|access-date=2021-05-10|website=cmake.org}}</ref>
=== Command syntax ===
The arguments of the commands are [[Whitespace character|whitespace]]-separated&nbsp;and can include keywords to separate groups of arguments. Commands can take keywords. For instance, in the command {{code|SET_SOURCE_FILE_PROPERTIES(source_file ... COMPILE_FLAGS compiler_option ...)|lang=cmake}} the keyword is {{code|COMPILE_FLAGS}}. It serves as a delimiter between the list of source files and some other options.<ref>{{cite web|access-date=2021-01-29|url=https://www.linuxjournal.com/article/6700|title=Cross-Platform Software Development Using CMake Software|first=Andrej|last=Cedilnik|publisher=Linux Journal|publication-date=2003-10-30}}</ref>


* {{code|add_executable()|lang=cmake}}— declares an executable binary target and the source files to use to build it
Examples of commands that CMake offers to specify targets and their dependencies and which serve as the starting point of the CMakeLists.txt:<ref>{{Cite web|title=add_executable — CMake 3.19.0-rc1 Documentation|url=https://cmake.org/cmake/help/latest/command/add_executable.html|access-date=2020-10-25|website=cmake.org}}</ref><ref>{{Cite web|title=add_library — CMake 3.19.0-rc1 Documentation|url=https://cmake.org/cmake/help/latest/command/add_library.html|access-date=2020-10-25|website=cmake.org}}</ref><ref>{{Cite web|title=target_link_directories — CMake 3.20.2 Documentation|url=https://cmake.org/cmake/help/latest/command/target_link_directories.html|access-date=2021-05-10|website=cmake.org}}</ref>
* {{code|add_library()|lang=cmake}} — the same but for a library

* {{Code|target_link_libraries()||lang=cmake}} — adds dependencies etc.
* {{code|add_executable(...)|lang=cmake}}— declares an executable binary target with sources (depend on language chosen) to be built
* {{code|add_library(...)|lang=cmake}} — the same but for a library
* {{Code|target_link_libraries(...)||lang=cmake}} — adds dependencies etc.

=== JSON strings ===
CMake supports extracting values into variables from [[JSON]]-data strings (since version 3.19).<ref>{{Cite web|title=CMake 3.19 Release Notes — CMake 3.19.7 Documentation|url=https://cmake.org/cmake/help/v3.19/release/3.19.html|access-date=2021-03-15|website=cmake.org}}</ref>


==Internals==
==Implementation==
The CMake scripting language is implemented by using [[Yacc]] and [[Lex (software)|Lex]] generators.{{Efn|As evident from the CMake source code repository.|name=n0:}}
The CMake scripting language is implemented via [[Yacc]] and [[Lex (software)|Lex]] generators.{{Efn|As evident from the CMake source code repository.|name=n0:}}


The executable programs CMake, CPack, and CTest are written in the [[C++]] programming language.{{Efn|name=n0:}}
The executable programs CMake, CPack, and CTest are written in [[C++]].{{Efn|name=n0:}}


Much of CMake's functionality is implemented in modules that are written in the [[CMake#Language|CMake language]].<ref>{{Cite web|title=cmake-language(7) — CMake 3.19.0-rc1 Documentation|url=https://cmake.org/cmake/help/latest/manual/cmake-language.7.html|access-date=2020-10-25|website=cmake.org}}</ref>
Much of CMake's functionality is implemented in modules written in the [[#Language|CMake language]].<ref>{{Cite web|title=cmake-language(7) — CMake 3.19.0-rc1 Documentation|url=https://cmake.org/cmake/help/latest/manual/cmake-language.7.html|access-date=2020-10-25|website=cmake.org}}</ref>


Since release 3.0, CMake's documentation uses [[reStructuredText]] markup. HTML pages and man pages are generated by the [[Sphinx (documentation generator)|Sphinx]] documentation generator.
Since release 3.0, CMake's documentation uses [[reStructuredText]] markup. HTML pages and man pages are generated by the [[Sphinx (documentation generator)|Sphinx]] documentation generator.


== Modules and tools ==
==Additional tools==
CMake ships with numerous {{code|.cmake}} modules and tools. These facilitate work such as finding dependencies (both built-in and external, e.g. {{code|FindXYZ}} modules), testing the toolchain environment and executables, packaging releases ({{code|CPack}} module and {{code|cpack}} command), and managing dependencies on external projects ({{code|ExternalProject}} module):<ref>{{Cite web |title=cmake-modules(7) — CMake 3.14.7 Documentation |url=https://cmake.org/cmake/help/v3.14/manual/cmake-modules.7.html |access-date=2020-10-24 |website=cmake.org}}</ref><ref>{{Cite web |title=ExternalProject — CMake 3.14.7 Documentation |url=https://cmake.org/cmake/help/v3.14/module/ExternalProject.html |access-date=2020-10-24 |website=cmake.org}}</ref>
CMake ships with numerous {{code|.cmake}} script files and development tools that facilitate tasks such as finding dependencies (both built-in and external, e.g. {{code|FindXYZ}} modules), testing the toolchain environment and executables, packaging releases (CPack), and managing dependencies on external projects ({{code|ExternalProject}} module). Additional development tools include:<ref>{{Cite web |title=cmake-modules(7) — CMake 3.14.7 Documentation |url=https://cmake.org/cmake/help/v3.14/manual/cmake-modules.7.html |access-date=2020-10-24 |website=cmake.org}}</ref><ref>{{Cite web |title=ExternalProject — CMake 3.14.7 Documentation |url=https://cmake.org/cmake/help/v3.14/module/ExternalProject.html |access-date=2020-10-24 |website=cmake.org}}</ref>
* '''ctest''' — is used for target testing commands specified by CMakeLists.txt
* '''ccmake''' and '''cmake-gui''' — tweaks and updates configuration variables intended for the native build system
* '''cpack''' — helps to package software


* '''ccmake''' and '''cmake-gui''' — for updating configuration variables intended for a native build tool
===CPack===
* '''CPack''' — for packaging software as Linux [[RPM package|RPM]], [[Debian package|deb]], and [[gzip]] packages, [[Nullsoft Scriptable Install System|NSIS]] files (for Windows), and [[macOS]] packages <ref>{{cite web |title=Packaging With CPack |url=https://gitlab.kitware.com/cmake/community/-/wikis/doc/cpack/Packaging-With-CPack |website=CMake Community Wiki |language=en}}</ref><ref>{{man|1|cpack|ManKier}}.</ref>
'''CPack''' is a [[packaging]] system for software distributions. It is tightly integrated with CMake but can function without it.
* '''CTest''' and '''CDash''' — for software testing and reporting
<ref>{{cite web |title=Packaging With CPack |url=https://gitlab.kitware.com/cmake/community/-/wikis/doc/cpack/Packaging-With-CPack |website=CMake Community Wiki |language=en}}</ref><ref>{{man|1|cpack|ManKier}}.</ref>

It can be used to generate:
* Linux [[RPM package|RPM]], [[Debian package|deb]], and [[gzip]] packages (for both [[Binary file|binaries]] and [[source code]]).
* [[Nullsoft Scriptable Install System|NSIS]] files (for [[Microsoft Windows]]).
* [[macOS]] packages.


== Adoption ==
== Adoption ==
CMake has been very widely adopted among commercial, open source, and academic software projects. A few notable users include
CMake has been very widely adopted among commercial, open source, and academic software projects. A few notable users include
[[Android NDK]], [[Netflix]], [[Inria]], [[MySQL]], [[Boost (C++ libraries)]], [[KeePassXC]], [[KDE]], [[KiCad|KiCAD]], [[FreeCAD]], [[WebKit|Webkit]], [[Blender (software)|Blender]],<ref>{{Cite web|title=Building Blender - Blender Developer Wiki|url=https://wiki.blender.org/wiki/Building_Blender|access-date=2021-07-01|website=wiki.blender.org}}</ref> Biicode, [[ReactOS]], Apache QPid, the [[ATLAS experiment]],<ref>{{Cite web|year=2017|title=Large Scale Software Building with CMake in ATLAS|url=https://cds.cern.ch/record/2243765/files/ATL-SOFT-PROC-2017-033.pdf|url-status=live|publisher=CERN
[[Android NDK]], [[Netflix]], [[Inria]], [[MySQL]], [[Boost (C++ libraries)]], [[KeePassXC]], [[KDE]], [[KiCad|KiCAD]], [[FreeCAD]], [[WebKit|Webkit]], [[Blender (software)|Blender]],<ref>{{Cite web|title=Building Blender - Blender Developer Wiki|url=https://wiki.blender.org/wiki/Building_Blender|access-date=2021-07-01|website=wiki.blender.org}}</ref> Biicode, [[ReactOS]], [[Apache Qpid]], the [[ATLAS experiment]],<ref>{{Cite web|year=2017|title=Large Scale Software Building with CMake in ATLAS|url=https://cds.cern.ch/record/2243765/files/ATL-SOFT-PROC-2017-033.pdf|url-status=live|publisher=CERN
|first1=J|last1=Elmsheuser
|first1=J|last1=Elmsheuser
|first2=A|last2=Krasznahorkay
|first2=A|last2=Krasznahorkay
Line 145: Line 158:
==Examples==
==Examples==
===Hello world===
===Hello world===
The following source code files demonstrate how to build a simple [[C++#Language|hello world]] program written in C++ by using CMake.
The following demonstrates how to configure CMake to build a [[C++#Language|hello world]] program written in C++.


hello.cpp:
====Example 1====


<syntaxhighlight lang="c++">
<syntaxhighlight lang="c++">
// hello.cpp
#include <iostream>
#include <iostream>
int main() {

std::cout << "Hello, world!" << std::endl;
int main()
return 0;
{
std::cout << "Hello, world!\n";
}
}

return 0;
</syntaxhighlight>
</syntaxhighlight>


CMakeLists.txt:
<syntaxhighlight lang="CMake">
<syntaxhighlight lang="CMake">
# CMakeLists.txt
cmake_minimum_required(VERSION 3.5)
cmake_minimum_required(VERSION 3.5)
project(HelloWorld CXX)
project(HelloWorld CXX)
Line 167: Line 178:
</syntaxhighlight>
</syntaxhighlight>


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:
====Example 2====
<syntaxhighlight lang="bash">
cmake -B build .
</syntaxhighlight>
Then, build the program via the native build tool as supported thru CMake:
<syntaxhighlight lang="bash">
cmake --build build
</syntaxhighlight>

The program is then available for running. Via [[Bash (shell)|Bash]], the command is like {{code|./build/hello}}. On Windows, the output file ends with {{code|.exe}}.

===Include===

This example demonstrates configuring the preprocessor include path.


hello.cpp:
<syntaxhighlight lang="c++">
<syntaxhighlight lang="c++">
// hello.cpp
#include "hello.h"
#include <iostream>
#include <iostream>
int main() {
#include "hello_heading.h"
for (int i = 0; i < Times; i++) {
int main()
std::cout << "Hello, world!" << std::endl;
{
for (int i = 0; i < Times; i++)
{
std::cout << "Hello, Word!";
}
}
return 0;
}
}
return 0;
</syntaxhighlight>
</syntaxhighlight>

hello.h:
<syntaxhighlight lang="c++">
<syntaxhighlight lang="c++">
// hello_heading.h
#ifndef HELLO_HEADING_H_
#define HELLO_HEADING_H_
const int Times = 10;
const int Times = 10;
#endif
</syntaxhighlight>
</syntaxhighlight>

CMakeLists.txt:
<syntaxhighlight lang="CMake">
<syntaxhighlight lang="CMake">
# CMakeLists.txt
cmake_minimum_required(VERSION 3.5)
cmake_minimum_required(VERSION 3.5)
project(HelloWorld CXX)
project(HelloWorld CXX)
Line 196: Line 217:
add_executable(hello hello.cpp)
add_executable(hello hello.cpp)
</syntaxhighlight>
</syntaxhighlight>

[[Bash (shell)|Shell]] commands to run CMake on a [[Linux]] system (to be entered in the directory that contains the two files above):
<syntaxhighlight lang="bash">
cmake -B build . # Configure the build directory.
cmake --build build # Build the program in the build directory.
./build/hello # Run the program (outputs "Hello, world!")
</syntaxhighlight>

<!-- TODO: Add script for windows and other platforms to show true power of CMake -->


==See also==
==See also==
{{Portal|Free and open-source software}}
{{Portal|Free and open-source software}}
* {{section link|List of build automation software|Build script generation}}
* {{section link|List of build automation software|Build script generation}}
* {{Annotated link|Qmake}}
* [[configure script]]
*[[Make_(software)|Make]]
*[[Monorepo]]
*[[Qmake]]


== Notes ==
== Notes ==
Line 222: Line 231:
==External links==
==External links==
* {{Official website}}
* {{Official website}}

* {{GitHub|Kitware/CMake}}
* [https://cmake.org/cmake/help/latest/ CMake Online Documentation]
** [https://cmake.org/cmake/help/latest/guide/tutorial/index.html CMake Tutorial]
*[https://www.youtube.com/watch?v=bsXLMQ6WgIk C++Now 2017: Daniel Pfeifer "Effective CMake"] on YouTube
*[https://thatonegamedev.com/tag/cmake/ CMake Category] on That One Game Dev


[[Category:2000 software]]
[[Category:2000 software]]

Latest revision as of 13:56, 1 December 2024

CMake
Developer(s)Andy Cedilnik, Bill Hoffman, Brad King, Ken Martin, Alexander Neundorf
Initial release2000; 24 years ago (2000)
Stable release
3.31.0[1] Edit this on Wikidata / 7 November 2024
Preview release
3.31.0-rc3[2] Edit this on Wikidata / 29 October 2024
Repository
Written inC, C++[3]
Operating systemCross-platform
TypeSoftware development tools
LicenseBSD-3-Clause
Websitecmake.org Edit this on Wikidata

CMake is a free, cross-platform, software development tool for building applications via compiler-independent instructions. It also can automate testing, packaging and installation. It runs on a variety of platforms and supports many programming languages.[4]

As a meta-build tool, CMake configures native build tools which in turn build the codebase. CMake generates configuration files 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.[5]

Notable native build tools supported by CMake include: Make, Qt Creator, Ninja, Android Studio, Xcode, and Visual Studio.[5]

CMake is distributed as free and open-source software under a permissive BSD-3-Clause license.[6]

History

[edit]

Initial development began in 1999 at Kitware with funding from the United States National Library of Medicine as part of the Visible Human Project.[5] 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 platforms, and Visual Studio project files for Windows.[7][5]

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.[5] gmake supported Unix and Windows compilers, but was its design lead to hard to resolve environment issues. 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:[5]

  • 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, shared library
  • 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.[5]

Subsequent development and improvements were fueled by the incorporation of CMake into developers’ own systems, including the VXL Project,[clarification needed] the CABLE[8] features added by Brad King,[clarification needed] and GE Corporate R&D for support of DART.[clarification needed] 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.[9] It has been described as the beginning of "Modern CMake".[10] Experts now advise to avoid variables in favor of targets and properties.[11] The commands add_compile_options, include_directories, link_directories, link_libraries that were at the core of CMake 2 should now be replaced by target-specific commands.

Name

[edit]

CMake developer Brad King stated that "the 'C' in CMake stands for 'cross-platform'".[12]

Features

[edit]

Generators

[edit]

CMake generates configuration files for a particular native build tool via one of its generators. A user can select a generator via the CMake command line.[5]

The build files are configured depending on the generator used (e.g. Unix Makefiles for make) and associated toolchain files. Advanced users can also create and incorporate additional makefile generators to support their specific compiler and OS needs. Generated files are typically placed (by using cmake's flag) into a folder outside of the source's one (out of source build), e.g., build/.

Each build project in turn contains its ownCMakeCache.txt file and CMakeFiles directory in every project (sub-)directory of included by the add_subdirectory(...) command, helping to avoid or speed up regeneration when it is run repeatedly.

The generation process and the output can be fine-tuned via target properties. Previously it was done via CMAKE_...-prefixed global variables that are also used to configure CMake itself and to set up initial defaults.[11][13] The older approach is discouraged now.

Build targets

[edit]

CMake supports building executables, libraries (e.g. libxyz, xyz.dll etc.), object file 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.[14]

Separate build tree

[edit]

One of its major features is the ability to place compiler outputs (such as object files) into a build tree which is located outside of the source tree.[5] This enables multiple builds from the same source tree and cross-compilation. Separate source and build files ensure that removing a build directory will not affect source files and prevents clutter which might confuse version control systems.[5]

Dependency management

[edit]

CMake tracks changes and recompiles upstream dependencies of a given sub-module if its sources are changed.[5][a]

Flexible project structure

[edit]

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.[15]

IDE configuration support

[edit]

CMake can generate project files for several popular IDEs, such as Microsoft Visual Studio, Xcode, and Eclipse CDT. It can also produce build scripts for MSBuild or NMake on Windows; Unix Make on Unix-like platforms such as Linux, macOS, and Cygwin; and Ninja on both Windows and Unix-like platforms.

Compiler feature detection

[edit]

CMake allows specification of features that the compiler is required to support in order to get the target program or library compiled.[16]

Compiler support

[edit]

CMake supports many compilers, including: Apple Clang, Clang, GNU GCC, MSVC, Oracle Developer Studio, and Intel C++ Compiler.[17]

Packaging system

[edit]

Even though CMake is not a package manager, it provides basic modules (see CPack) functions for installing binaries and package information declared by the CMakeLists.txt script to be used by consumer CMake projects. The package may also be packed into an archive file for package manager or installer supported by a target platform. Third-party packages may also be imported via configured CMake files which are either provided by the same third-party or created manually.[18]: 132, 142 [19][20]

GUI

[edit]

Cmake may be run by using a ncurses program like ccmake that can be used to configure projects via command-line interface.

Precompiled headers

[edit]

It's possible to generate precompiled headers via CMake since version 3.6.[21]

JSON strings

[edit]

CMake supports extracting values into variables from JSON-data strings (since version 3.19).[22]

Build process

[edit]

Building via CMake has a two major stages.[5] First, native build tool configuration files are generated from CMake configuration files – written in the CMake scripting language. The command line syntax is cmake <dir> where <dir> is a directory that contains a CMakeLists.txt file. Then, the native build tools are invoked either via CMake (cmake --build) or directly via the native tools interface. The native build tools use the generated files.[15][23]

Language

[edit]

CMake includes an interpreter for a relatively simple, custom, imperative scripting language that supports variables, string manipulation, arrays, function and macro declaration, and module inclusion (importing).

The interpreter reads CMake language commands from files named CMakeLists.txt which specify source files and build preferences. CMake uses this information to generate native tool configuration files. Additionally, files with suffix .cmake can be used for storing additional script.[24]

Command syntax

[edit]

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 COMPILE_FLAGS delimits a list of source files from compiler flags.[25]

set_source_file_properties(filename ... COMPILE_FLAGS compile_flag ...)

Some commonly used commands include:[26][27][28]

  • add_executable()— declares an executable binary target and the source files to use to build it
  • add_library() — the same but for a library
  • target_link_libraries() — adds dependencies etc.

Implementation

[edit]

The CMake scripting language is implemented via Yacc and Lex generators.[b]

The executable programs CMake, CPack, and CTest are written in C++.[b]

Much of CMake's functionality is implemented in modules written in the CMake language.[29]

Since release 3.0, CMake's documentation uses reStructuredText markup. HTML pages and man pages are generated by the Sphinx documentation generator.

Additional tools

[edit]

CMake ships with numerous .cmake script files and development tools that facilitate tasks such as finding dependencies (both built-in and external, e.g. FindXYZ modules), testing the toolchain environment and executables, packaging releases (CPack), and managing dependencies on external projects (ExternalProject module). Additional development tools include:[30][31]

  • ccmake and cmake-gui — for updating configuration variables intended for a native build tool
  • CPack — for packaging software as Linux RPM, deb, and gzip packages, NSIS files (for Windows), and macOS packages [32][33]
  • CTest and CDash — for software testing and reporting

Adoption

[edit]

CMake has been very widely adopted among commercial, open source, and academic software projects. A few notable users include Android NDK, Netflix, Inria, MySQL, Boost (C++ libraries), KeePassXC, KDE, KiCAD, FreeCAD, Webkit, Blender,[34] Biicode, ReactOS, Apache Qpid, the ATLAS experiment,[35] and Second Life.[36]

Examples

[edit]

Hello world

[edit]

The following demonstrates how to configure CMake to build a hello world program written in C++.

hello.cpp:

#include <iostream>
int main() {
    std::cout << "Hello, world!" << std::endl;
    return 0;
}

CMakeLists.txt:

cmake_minimum_required(VERSION 3.5)
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 build .

Then, build the program via the native build tool as supported thru CMake:

cmake --build build

The program is then available for running. Via Bash, the command is like ./build/hello. On Windows, the output file ends with .exe.

Include

[edit]

This example demonstrates configuring the preprocessor include path.

hello.cpp:

#include "hello.h"
#include <iostream>
int main() {
    for (int i = 0; i < Times; i++) {
        std::cout << "Hello, world!" << std::endl;
    }
    return 0;
}

hello.h:

const int Times = 10;

CMakeLists.txt:

cmake_minimum_required(VERSION 3.5)
project(HelloWorld CXX)
include_directories(${PROJECT_SOURCE_DIR})
add_executable(hello hello.cpp)

See also

[edit]

Notes

[edit]
  1. ^ Upstream dependencies - dependent sources that depend upon a modified source file
  2. ^ a b As evident from the CMake source code repository.

References

[edit]
  1. ^ "CMake 3.31.0 available for download". 7 November 2024. Retrieved 14 November 2024.
  2. ^ "CMake 3.31.0-rc3 is ready for testing". 29 October 2024. Retrieved 6 November 2024.
  3. ^ "The CMake Open Source Project on OpenHub". OpenHub. Retrieved 9 April 2016.
  4. ^ "CMake".
  5. ^ a b c d e f g h i j k l "The Architecture of Open Source Applications (Volume 1)CMake". aosabook.org. Retrieved 11 June 2023.
  6. ^ "Licenses · master · CMake / CMake". GitLab. Retrieved 13 November 2020.
  7. ^ "FLOSS Weekly 111: CMake". podcast. TWiT Network. Retrieved 27 February 2011.
  8. ^ "The CABLE". Archived from the original on 19 June 2013. Retrieved 10 November 2010.
  9. ^ Maynard, Robert (10 June 2014). "[CMake] [ANNOUNCE] CMake 3.0.0 Released".
  10. ^ "Effective Modern CMake". Gist.
  11. ^ a b Binna, Manuel (22 July 2018). "Effective Modern CMake".
  12. ^ https://public.kitware.com/Bug/view.php?id=14012#c32631 [bare URL]
  13. ^ Pfeifer, Daniel (19 May 2017). Effective Cmake (PDF). CPPNow.
  14. ^ "cmake-buildsystem(7) — CMake 3.19.0-rc3 Documentation". cmake.org. Retrieved 14 November 2020.
  15. ^ a b Neundorf, Alexander (21 June 2006). "Why the KDE project switched to CMake—and how". LWN.net.
  16. ^ "CMake compiler feature detect". www.scivision.dev. 15 November 2020. Retrieved 22 January 2022.
  17. ^ "Supported Compilers". CMake Documentation. Retrieved 22 January 2022.
  18. ^ Berner, Dominik (2022). CMake Best Practices : Discover Proven Techniques for Creating and Maintaining Programming Projects with CMake. Mustafa Kemal Gilor. Birmingham: Packt Publishing, Limited. ISBN 978-1-80324-424-2. OCLC 1319218167.
  19. ^ "cmake-packages(7) — CMake 3.24.1 Documentation". cmake.org. Retrieved 11 September 2022.
  20. ^ "Exposing a module's configuration (advanced) — CMake build procedure 1.0 documentation". docs.salome-platform.org. Retrieved 11 September 2022.
  21. ^ "target_precompile_headers — CMake 3.21.20210925-gb818e3c Documentation". cmake.org. Retrieved 25 September 2021.
  22. ^ "CMake 3.19 Release Notes — CMake 3.19.7 Documentation". cmake.org. Retrieved 15 March 2021.
  23. ^ "cmake-toolchains(7) — CMake 3.19.0-rc2 Documentation". cmake.org. Retrieved 29 October 2020.
  24. ^ "cmake-language(7) — CMake 3.19.0-rc2 Documentation". cmake.org. Retrieved 29 October 2020.
  25. ^ Cedilnik, Andrej (30 October 2003). "Cross-Platform Software Development Using CMake Software". Linux Journal. Retrieved 29 January 2021.
  26. ^ "add_executable — CMake 3.19.0-rc1 Documentation". cmake.org. Retrieved 25 October 2020.
  27. ^ "add_library — CMake 3.19.0-rc1 Documentation". cmake.org. Retrieved 25 October 2020.
  28. ^ "target_link_directories — CMake 3.20.2 Documentation". cmake.org. Retrieved 10 May 2021.
  29. ^ "cmake-language(7) — CMake 3.19.0-rc1 Documentation". cmake.org. Retrieved 25 October 2020.
  30. ^ "cmake-modules(7) — CMake 3.14.7 Documentation". cmake.org. Retrieved 24 October 2020.
  31. ^ "ExternalProject — CMake 3.14.7 Documentation". cmake.org. Retrieved 24 October 2020.
  32. ^ "Packaging With CPack". CMake Community Wiki.
  33. ^ cpack(1) – Linux General Commands Manual.
  34. ^ "Building Blender - Blender Developer Wiki". wiki.blender.org. Retrieved 1 July 2021.
  35. ^ Elmsheuser, J; Krasznahorkay, A; Obreshkov, E; Undrus, A (2017). "Large Scale Software Building with CMake in ATLAS" (PDF). CERN. Archived (PDF) from the original on 28 July 2018.
  36. ^ "CMake Success". cmake.org. Kitware. 2011. Retrieved 12 March 2022.
[edit]