Cmake C 20

CMake ships with numerous.cmake modules and tools. These facilitate work such as finding dependencies ( FindXYZ modules), testing the toolchain environment and executables, packaging releases ( CPack module and cpack command), and managing dependencies on external projects ( ExternalProject module): 19 20.

  1. If you are using GitHub Actions, also see the jwlawson/actions-setup-cmake action, which can install your selection of CMake, even in a docker action run. Versions less than 3.10 are marked by a deeper color of red.
  2. The suite of CMake tools were created by Kitware in response to the need for a powerful, cross-platform build environment for open-source projects such as ITK and VTK. The CMake python wheels provide CMake 3.18.4.
  3. If you have an existing application that was created with an SDK earlier than 20.04, see Convert an existing app to use the CMake functions. The CMakeLists.txt file must call the project command before any of the azsphere functions.
  4. CMake does not currently support C20 modules like the others have stated. However, module support for Fortran is very similar, and perhaps this could be easily changed to support modules in C20.

First written on 2020-09-11.
Last updated on 2021-01-02.

In this article and several more, I will be discussing developing a very simple C++ library and application using CMake and Visual Studio Code. I will also use git and Google Test, and port the project from Windows to Linux. Most of the information is applicable to using almost any IDE, or indeed, no IDE at all.

Why Use CMake?

CMake is a powerful and robust build system. You specify what you want done, not how to do it. CMake then takes that information and generates the files needed to build the system. For example, CMake can generate solution (.sln) and project files (.vcxproj) that Visual Studio and Visual Studio Code use on Windows. Similar capabilities are available for every other popular IDE. It can even create make files if you want to do everything from the command line. Because it can be called from the command line, it integrates well with continuous integration/continuous build systems.

You can specify the build tools that you want to use; for example, you can use MSVC or LLVM on Windows, and gnu or LLVM on Unix-like systems, including Linux, OSX, and MSYS or MinGW. Aside from specifying the tools to use, no other changes are required to the CMake specification files. You will see this when I port my project from Windows using the Visual Studio build tools to Linux using the gnu build tools.

With CMake, you can download, build, and use a large number of tools. I will show one example of this when I use Google Test in this project. Just about any tool that can be downloaded from the internet, and which provides CMake files for building, can be used.

Installing the Needed Tools

I will start the project on Windows using VS Code and the Visual Studio Build Tools, but if you wish, you can start with a different IDE, or even a different operating system. In a later article, I will discuss using the gnu tools on Linux (Ubuntu).


So let’s begin.

Installing Visual Studio Code and Extensions

On Windows, the latest version of Visual Studio Code is available on its download page. Select the appropriate version; click on the Windows button for the x64 version, or one of the ARM links for ARM if that is applicable to you. The download will begin. When it is completed, run the downloaded file.

Next, we need two VSCode extensions. Start VS Code and display the extensions panel (select View → Extensions from the main menu). In the search box, enter C++. A number of C and C++ extensions are displayed. You want the one called C++. Make sure it is from Microsoft. This extension provides Intellisense, debugging, and browsing capabilities. Click on the Install button to install it.

The second extension is CMake Tools. Search for and install it.

Installing Visual Studio Build Tools

We need the build tools provided by Visual Studio. Don’t worry, we aren’t installing Visual Studio, just the build tools.

On the Visual Studio downloads page, move down into the All Downloads section. As I write this, the current version of Visual Studio is 2019, so I will be referring to it in this section. If a later version is available, use that instead. Select Tools for Visual Studio 2019. Click on the Download button for Build Tools for Visual Studio 2019. Download and save the file. When the download has completed, open the file. This starts Visual Studio Installer. Again, don’t worry, we are not installing Visual Studio, just the build tools. When the installer window opens select only the build tools. After some time (several minutes), the install will complete. Close the installer.

Open the Windows Start menu and start Developer Command Prompt for VS 2019; do not open the standard command prompt or Powershell. At the command prompt, enter:

The following should be displayed, although the version number may be different:

If the message says that it cannot find CMake, then the build tools did not install correctly.

You will almost always be starting VS Code from the command line of Developer Command Prompt, so you will probably want to add it to the Productivity section of the Start menu.

Installing Git

We will need git. If you have done any development work, you probably already have it installed. If not, Git for Windows is available here.

A Simple C++ Program With Library

We will start by creating a simple C++ program with a simple library. You can perform similar steps, with slight modifications, if you are on Linux or any other Unix-like system. To support the program, we will create a directory structure and start VS Code as follows:

Open a Developer Command Prompt. Enter:

In the Explorer list in VS Code, select the hello/include directory and create a new file called hello.h. Place the following code in that file and save it:

Again in the Explorer list, select hello/src and create a new file called hello.cpp. Place the following code in that file and save it:

That is all the code we need for our library. Now create the program to use the library. In the Explorer list, select the apps directory and create a new file called main.cpp. Place the following code in that file and save it:

To build the library and program, we will use CMake. There are many examples of CMake on the internet, many of which are outdated or just plain bad. For this program and library, I am following Modern CMake by Henry Schreiner and others.

In the Explorer list, select VSCODE-CMAKE-HELLO in VS Code and create a new file. Call it CMakeLists.txt. Enter the following and save the file:

In the Explorer list, select apps and create a new file. Call it CMakeLists.txt. Enter the following and save the file:


Create a file called CMakeLists.txt in the hello directory and place the following code in it:

Using CMake With Visual Studio Code

Look at the status bar on the VS Code window. If it looks similar to this:

then terminate and restart VS Code. The status bar should now look like this:

From left to right, master* indicates that you are editing the git master branch and that changes have been made. The symbols and 0s indicate that there are currently no errors in workspace. Next is a button that will run CMake (CMake: [Debug]). No Kit Selected indicates that the build tools have not yet been selected; we will get to them in a moment. Following that is a Build button, the default target, a bug button that will run the application in debug mode, and a button that will run the application without starting the debugger. The remainder of the status bar provides other information that we are not currently concerned with.

We first have to run CMake to create the build files. Click on the CMake: [Debug] button. The first time you do so, a list of build tool kits is displayed below the main menu. Select either Unspecified, or Visual Studio Build Tools 2019 Release - amd64. The No Kit Selected text in the status bar will change to [Visual Studio Build Tools 2019 Release - amd64], and a list of CMake configurations are displayed: Debug, Release, MinSizeRel, and RelWithDebInfo.

Select Debug. This will execute CMake and generate a Visual Studio solution file (.sln) and Visual Studio project files (.vcxproj) if there are no errors. If there are errors, then something is not right with the CMakeLists.txt files or the C++ source files.

If you selected any of the other CMake actions, the executable, library, and debug related files would be placed in other subdirectories. For example, if you build release versions, they will be placed in build/Release.

The first time you run debugging by either clicking on the Bug button, or by selecting Run → Start Debugging, a list of build environments will be displayed just below the main menu. Select C++ (Windows).

To do a clean and rebuild, all we have to do is delete the build directory and all of its contents, then run CMake and Build.


After a debug build, you can debug main.exe by clicking on the bug button in the status bar. Alternatively, you can select Run → Start Debugging from the main menu. In the latter case, the first time you do this, a list of debug environments is displayed. Select C++ (Windows). If the active file in VS Code is a C++ source file, a list of configurations is then displayed. You can select either cl.exe - Build and debug active file or Default configuration. If a different type of file is active, such as CMakeLists.txt, then the configuration list is not displayed.

In either case, a file called launch.json is added to the .vscode directory. Open that file and change the program to ${workspaceFolder}/build/apps/Debug/main.exe

Now you can run debug from either the bug button or the menu item.

Summary and What’s Next

This article discussed how to create a C++ project containing a program called main and a library called hello with Visual Studio Code using CMake. How to debug the program is also discussed.

There is still much to do, but that will be discussed in the following articles:

  • Adding GoogleTest to the project.


The code in hello.cpp was updated to correct a typo. Thanks Vlad T. for pointing that out.

Developer(s)Andy Cedilnik, Bill Hoffman, Brad King, Ken Martin, Alexander Neundorf
Initial release2000; 21 years ago
Stable release
Written inC, C++[2]
Operating systemCross-platform
TypeSoftware development tools
LicenseNew BSD

In software development, CMake is cross-platformfree and open-source software for build automation, testing, packaging and installation of software by using a compiler-independent method.[3] CMake is not a build system but rather it generates another system's build files. It supports directory hierarchies and applications that depend on multiple libraries. It is used in conjunction with 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 is distributed as open-source software under permissive New BSD license.[4]


CMake development began in 1999 in response to the need for a cross-platform build environment for the Insight Segmentation and Registration Toolkit.[5] 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, which was made by Ken Martin and other developers to support the Visualization Toolkit (VTK). At Kitware, Bill Hoffman blended components of pcmaker with his own ideas, striving to mimic the functionality of Unixconfigure scripts. CMake was first implemented in 2000 and further developed in 2001.

Continued development and improvements were fueled by the incorporation of CMake into developers’ own systems, including the VXL Project,[clarification needed] the CABLE[6] 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.[7] It has been described as the beginning of 'Modern CMake'.[8] Experts now advise to avoid variables in favor of targets and properties.[9] 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.



A key feature is the ability to (optionally) place compiler outputs (such as object files) outside the source tree. This enables multiple builds from the same source tree and cross-compilation. It also keeps the source tree clean and ensures that removing a build directory will not remove the source files.

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 the 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.[10]

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

Build process[edit]

The build of a program or library with CMake is a two stage process. First, standard build files are created (generated) from configuration files (CMakeLists.txt) which are written in CMake language. Then the platform's native build tools (native toolchain) are used for actual building of programs.[10][11]

The build files are configured depending on used generator (e.g. Unix Makefiles for make). 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 flag) into a different from sources folder, e.g., build/.

Each build project in turn contains a CMakeCache.txt file and CMakeFiles directory in every (sub-)directory of the projects (happened to be included by add_subdirectory(...) command earlier) helping to avoid or speed up regeneration stage once it's run over again.

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) one instead. This enables flexibility in configuration of various optimizations.[12]


CMake has a relatively simple interpreted, functional, scripting language. It supports variables, string manipulation, arrays, function/macro declarations, and module inclusion (import). 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.[13]

To generate a project's build files, one invokes cmake and specifies the directory which contains CMakeLists.txt. This file contains one or more commands in the form COMMAND(argument ...). The arguments are whitespace-separated.

The language includes commands to specify dependencies. For example, commands such as add_executable(...) and add_library(...) introduce the target and dependencies for executables and libraries, respectively.[14][15] Some build dependencies can be determined automatically.

The arguments can include keywords to separate groups of arguments. For example, in the command SET_SOURCE_FILE_PROPERTIES(source_file...COMPILE_FLAGScompiler_option...). Here, the keyword COMPILE_FLAGS terminates the list of source files and begins the list of compiler options.[16]

Once the Makefile (or alternative) has been generated, build behavior can be fine-tuned via target properties (since version 3.1) or via CMAKE_...-prefixed global variables.The latter is discouraged for target-only configurations because variables are also used to configure CMake itself and to set up initial defaults.[9]

JSON strings[edit]

Cmake supports extracting values into variables from the JSON-data strings (since version 3.19).[17]

Cmake C 20 Review


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

Cmake c fortran

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

C 20 License California

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

Modules & Tools[edit]

CMake ships with numerous .cmake modules and tools. These facilitate work such as finding dependencies (FindXYZ modules), testing the toolchain environment and executables, packaging releases (CPack module and cpack command), and managing dependencies on external projects (ExternalProject module):[19][20]

  • 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 is a packaging system for software distributions. It is tightly integrated with CMake but can function without it.[21][22]

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.


Hello World[edit]

The following source code files demonstrate how to build a simple hello world program written in C++ by using CMake. The source files are placed in a src/ directory.

bash script to run CMake on a Linux system. This example assumes that the script will be kept next to the src/ folder:

See also[edit]

  • GYP (Generate Your Projects) - Google-made project building tool


Cmake Clr

  1. ^'CMake 3.20.2 available for download'.
  2. ^'The CMake Open Source Project on OpenHub'. OpenHub. Retrieved 2016-04-09.
  3. ^'CMake'.
  4. ^'Licenses · master · CMake / CMake'. GitLab. Retrieved 2020-11-13.
  5. ^'FLOSS Weekly 111: CMake'. podcast. TWiT Network. Retrieved 27 February 2011.
  6. ^'The CABLE'. Archived from the original on 2013-06-19. Retrieved 2010-11-10.
  7. ^Maynard, Robert (June 10, 2014). '[CMake] [ANNOUNCE] CMake 3.0.0 Released'.
  8. ^'Effective Modern CMake'. Gist.
  9. ^ ab,
  10. ^ abNeundorf, Alexander (2006-06-21). 'Why the KDE project switched to CMake—and how'.
  11. ^'cmake-toolchains(7) — CMake 3.19.0-rc2 Documentation'. Retrieved 2020-10-29.
  12. ^'cmake-buildsystem(7) — CMake 3.19.0-rc3 Documentation'. Retrieved 2020-11-14.
  13. ^'cmake-language(7) — CMake 3.19.0-rc2 Documentation'. Retrieved 2020-10-29.
  14. ^'add_executable — CMake 3.19.0-rc1 Documentation'. Retrieved 2020-10-25.
  15. ^'add_library — CMake 3.19.0-rc1 Documentation'. Retrieved 2020-10-25.
  16. ^Andrej Cedilnik (2003-10-30). 'Cross-Platform Software Development Using CMake Software'. Linux Journal. Retrieved 2021-01-29.
  17. ^'CMake 3.19 Release Notes — CMake 3.19.7 Documentation'. Retrieved 2021-03-15.
  18. ^'cmake-language(7) — CMake 3.19.0-rc1 Documentation'. Retrieved 2020-10-25.
  19. ^'cmake-modules(7) — CMake 3.14.7 Documentation'. Retrieved 2020-10-24.
  20. ^'ExternalProject — CMake 3.14.7 Documentation'. Retrieved 2020-10-24.
  21. ^'Packaging With CPack'. CMake Community Wiki.
  22. ^cpack(1) – Linux General Commands Manual

External links[edit]

Cmake C 2020

  • Official website
  • CMake on GitHub
  • C++Now 2017: Daniel Pfeifer “Effective CMake' on YouTube
Retrieved from ''