CMake (which includes the executable tools cmake, ctest and cpack with each CMake installation) continues to evolve and improve fairly rapidly from release to release.Kitware (the organization responsible for CMake development) makes three major releases each year (or about every four months).Best practices for using CMake evolve as new versions are released.For example, at the time of this writing, CMake support for Generator Expressions is rapidly evolving and can have a dramatic impact on the best way to write CMakeList.txt files.)The bssw.io editorial team has created this article informed by 13+ years of CMake usage and hundreds of hours of searching for and reading CMake documentation, especially in the years 2021-2022.
As a developer contributing to a project using CMake (i.e. editing the project's CMakeLists.txt files), when consulting the official reference documentation, it is advisable to view documentation for only the minimum version of CMake supported by the project (i.e. for the version X.Y listed in the project's top-level CMakeLists.txt file calling cmake_minimum_required(VERSION X.Y)).For example, if minimum version is 3.17, then one would select that version in the drop-down box (taking you to ). This helps to avoid accidentally using CMake features available only in newer versions.
I bought the ebook Professional CMake. If you buy the book once you also get updates for new versions of CMake. I read the first few chapters (the entire book is 538 pages), but ultimately I figured it will be easier to just copy/steal from other Fortran projects using CMake available on GitHub and adapt them to my needs.
The first part of the book covers the essential steps for getting up and running quickly with a basic project. It assumes no prior experience with CMake. It introduces the reader to the most pivotal CMake concepts (especially targets) and each of the main tools: cmake, ctest and cpack. It also points out a few common things for new users to avoid. By the end of this section, the reader will have a basic working project they can use to explore and experiment with as they build their knowledge through the rest of the book.
The source code for the recipes in this book is available on GitHub, at -cafe/cmake-cookbook. The code is licensed under the standard open source MIT license: this is a permissive software license, and you can reuse and remix the code in whatever way you see fit, as long as the original copyright and license notice are included in any copies of the software/source. The full text of the license is available at
This will create a folder named cmake-cookbook. The book and the repository are organized in chapters and recipes. The numbering of chapters and the order of recipes in the repository reflect the order in the text. Each recipe is further organized into example folders. Some of the recipes have more than one example, usually when similar CMake concepts are illustrated in different programming languages.
Most GNU/Linux distributions have CMake available in their package managers. However, on some distributions, the packaged version can be rather old, so downloading the binary maintained by Kitware is still the preferred option. The following commands will download and install CMake 3.5.2 under $HOME/Deps/cmake (adjust this path to your preference), from the version packaged by CMake:
The recipes are tested on state-of-the-art continuous integration (CI) services: Travis ( -ci.org) for GNU/Linux and macOS, Appveyor ( ) for Windows, and CircleCI ( ) for additional GNU/Linux testing with commercial compilers. The configuration files for the CI services can be found in the repository ( -cafe/cmake-cookbook/): .travis.yml for Travis, .appveyor.yml for Appveyor, and .circleci/config.yml for CircleCI. Additional installation scripts for Travis and Appveyor can be found in the folder testing/dependencies.
Do you remember our hello.cpp application from the Understanding the basics section? CMake makes it really easy for you to build it. All we need is the following CMakeLists.txt file next to our source and two simple commands, cmake -B buildtree and cmake --build buildtree, as follows:
As the use of this particular mode is fairly limited, we won't cover it in depth. However, if you're interested in the details, I recommend calling cmake -E to list all the available commands. To simply get a glimpse of what's on offer, CMake 3.20 supports the following commands:
CMake for Windows comes with a GUI version to configure the building process of previously prepared projects. For Unix-like platforms, there is a version built with QT libraries. Ubuntu offers it in the cmake-qt-gui package.
These files are generated in the build tree by the cmake executable in the generation stage. As such, they shouldn't be edited manually. CMake uses them as a configuration for the cmake install action, CTest, and CPack. If you're implementing an in-source build (not recommended), it's probably a good idea to add them to the VCS ignore file.
We have already learned from the Mastering the command line section that we can execute scripts using the -P option: cmake -P script.cmake. But what are the actual requirements for the script file provided? Not that many: a script can be as complex as you like or an empty file. However, it is recommended that you call the cmake_minimum_required() command at the beginning of the script. This command tells CMake which policies should be applied to subsequent commands in this project (more details in Chapter 3, Setting Up Your First CMake Project).
The CMake distribution comes packed with almost 90 different utility modules. If that's not enough, you can download more from the internet by browsing curated lists, such as the one found at -cmake, or write a module from scratch.
aa06259810