Discovering Scripts and Modules

Let's learn about the scripts and modules available in CMake.

We'll cover the following

Work with CMake is primarily focused on projects that get built and the production of artifacts that get consumed by other systems, such as CI/CD pipelines and test platforms, or deployed to machines or artifact repositories. However, there are two other concepts of CMake that enable us to create with its language: scripts and modules.

Scripts

To configure project building, CMake offers a platform-agnostic programming language. This comes with many useful commands. We can use this tool to write scripts that come with our project or are completely independent.

Think of it as a consistent way to do cross-platform work: instead of using bash scripts on Linux and batch or PowerShell scripts on Windows, we can have one version. Sure, we could bring in external tools such as Python, Perl, or Ruby scripts, but this is yet another dependency and will increase the complexity of our C/C++ projects. Yes, sometimes, this will be the only thing that can get the job done, but more often than not, we can get away with something far simpler.

We can execute scripts using the -P option: cmake -P script.cmake. But what are the actual requirements for the script file provided? Not many: a script can be as complex as we like or an empty file. However, it is recommended that we 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. Let's use the following script. Click "Run" and enter cmake -P script.cmake at the prompt.

Get hands-on with 1200+ tech skills courses.