Cmake Online Tutorials

In software development, CMake is cross-platform free and open-source software for build automation, testing, packaging and installation of software by using a compiler-independent method. CMake is not a build system itself; it generates another system's build files. It supports directory hierarchies and applications that depend on multiple libraries. It can invoke native build environments such as Make, Qt Creator, Ninja, Android Studio, Apple's Xcode, and Microsoft Visual Studio. It has minimal dependencies, requiring only a C++ compiler on its own build system.

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

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

History

edit

CMake development began in 1999, in response to the need for a cross-platform build environment for the Insight Segmentation and Registration Toolkit (ITK). The project is funded by the United States National Library of Medicine as part of the Visible Human Project. It was partially inspired by pcmaker, a predecessor to CMake, which was made by Ken Martin and other developers to support building of the Visualization Toolkit (VTK). pcmaker was a C program that converted Make files into MS Windows' NMake counterparts. At Kitware, Bill Hoffman blended components of pcmaker with his own ideas, striving to mimic the functionality of Unix configure scripts. CMake was first implemented in 2000 and further developed in 2001.

Historically CMake was conceived with the following major features in mind:

  • depending only on system C++ compiler, meaning no third-party libraries
  • to be able to generate Visual Studio IDE input files
  • capable of producing 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 Autotools): what system could and could not do
  • automatically scan C/C++ dependencies
  • cross-platform

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.

Continued 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 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.

Developer Brad King has stated that "the 'C' in CMake stands for 'cross-platform'".

Features

edit

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. 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.

Dependency management

edit

CMake keeps track and recompiles all the upstream dependencies of a given sub-module if its sources are changed.

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.

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.

Compilers

edit

CMake supports an extensive list of compilers, including: Apple Clang, Clang, GNU GCC, MSVC, Oracle Developer Studio, and Intel C++ Compiler.

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.: 132, 142 [17][18]

GUI

edit

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

Build process

edit

The build of a program or library with CMake is a two-stage process.[5] First, build files (usually scripts) are created (generated) from configuration files (CMakeLists.txt scripts) written in CMake language. Then the platform's native build tools that can read these build files (native toolchain) are invoked either manually externally or via cmake --build for actual building of programs (build targets).[13][19] The generator specified by the user on the commandline determines which build tool chain to use.[5]

Generators

edit

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][20] The older approach is discouraged now.

Types of build targets

edit

Depending on CMakeLists.txt configuration the build files may be either executables, libraries (e.g. libxyz, 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.[21]

Build dependencies may be determined automatically.

Precompiled headers

edit

It's possible to generate precompiled headers by using CMake since version 3.6.[22]

Language

edit

CMakeLists.txt

edit

CMake has a relatively simple 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 cmake from a file named 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, .cmake-suffixed files can contain scripts used by CMake.[23]

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

Command syntax

edit

The arguments of the commands are whitespace-separated and can include keywords to separate groups of arguments. Commands can take keywords. For instance, in the command SET_SOURCE_FILE_PROPERTIES(source_file ... COMPILE_FLAGS compiler_option ...) the keyword is COMPILE_FLAGS. It serves as a delimiter between the list of source files and some other options.[24]

Examples of commands that CMake offers to specify targets and their dependencies and which serve as the starting point of the CMakeLists.txt:[25][26][27]

  • add_executable(...)— declares an executable binary target with sources (depend on language chosen) to be built
  • add_library(...) — the same but for a library
  • target_link_libraries(...) — adds dependencies etc.

JSON strings

edit

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

Internals

edit

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

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

Much of CMake's functionality is implemented in modules that are 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.

Modules and tools

edit

CMake ships with numerous .cmake modules and tools. These facilitate work such as finding dependencies (both built-in and external, e.g. FindXYZ modules), testing the toolchain environment and executables, packaging releases (CPack module and cpack command), and managing dependencies on external projects (ExternalProject module):[30][31]

  • 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

CPack

edit

CPack is a packaging system for software distributions. It is tightly integrated with CMake but can function without it. [32][33]

It can be used to generate:

  • Linux RPM, deb, and gzip packages (for both binaries and source code).
  • NSIS files (for Microsoft Windows).
  • macOS packages.

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 source code files demonstrate how to build a simple hello world program written in C++ by using CMake.

Example 1

edit
// 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)

Example 2

edit
// hello.cpp
#include "hello_heading.h"
#include <iostream>

int main()
{
    for (int i = 0; i < Times; i++)
    {
        std::cout << "Hello, world!" << std::endl;
    }

    return 0;
}
// hello_heading.h
#ifndef HELLO_HEADING_H_
#define HELLO_HEADING_H_
const int Times = 10;
#endif
# CMakeLists.txt
cmake_minimum_required(VERSION 3.5)
project(HelloWorld CXX)
include_directories(${PROJECT_SOURCE_DIR})
add_executable(hello hello.cpp)

Shell commands to run CMake on a Linux system (to be entered in the directory that contains the two files above):

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!")


See also

edit
  • List of build automation software § Build script generation
  • configure script
  • Make
  • Monorepo
  • Qmake

Notes

edit

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 m "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. ^ a b Neundorf, Alexander (21 June 2006). "Why the KDE project switched to CMake—and how". LWN.net.
  14. ^ "CMake compiler feature detect". www.scivision.dev. 15 November 2020. Retrieved 22 January 2022.
  15. ^ "Supported Compilers". CMake Documentation. Retrieved 22 January 2022.
  16. ^ 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.
  17. ^ "cmake-packages(7) — CMake 3.24.1 Documentation". cmake.org. Retrieved 11 September 2022.
  18. ^ rel="nofollow" class="external text" href="https://docs.salome-platform.org/lates
    Cmake Tutorials: CMake is a cross-platform, open-source build system. It generates native makefiles and project files that can be used from the command line or integrated development environment of your choice

    Latest online Cmake Tutorials with example so this page for both freshers and experienced candidate who want to get job in Cmake company

    Latest online Cmake Tutorials for both freshers and experienced

    advertisements

    View Tutorials on Cmake View all questions

    Ask your interview questions on Cmake

    Write Your comment or Questions if you want the answers on Cmake from Cmake Experts
    Name* :
    Email Id* :
    Mob no* :
    Question
    Or
    Comment* :
     





Disclimer: PCDS.CO.IN not responsible for any content, information, data or any feature of website. If you are using this website then its your own responsibility to understand the content of the website

--------- Tutorials ---