Understanding the Basics
Let's learn about what CMake is and how its works.
The compilation of C++ source code appears to be a fairly straightforward process. Let's take a small program, such as a classic hello.cpp
application, as follows:
#include <iostream>int main() {std::cout << "Hello World!" << std::endl;return 0;}
Now, all we need to do to get an executable is to run a single command. We call the compiler with the filename as an argument:
$ g++ hello.cpp -o a.out
Our code is correct, so the compiler will silently produce an executable binary file that our machine can understand. We can run it by calling its name:
$ ./a.outHello World!$
However, as our projects grow, we’ll realize that keeping everything in a single file is simply not possible. Clean code practices recommend that files should be kept small and in well-organized structures. The manual compilation of every file can be a tiresome and fragile process. There must be a better way.
What is CMake?
Let's say we automate building by writing a script that goes through our project tree and compiles everything. To avoid any unnecessary compilations, our script will detect whether the source has been modified since the last time we ran it (the script). Now, we'd like a convenient way to manage arguments that are passed to the compiler for each file—preferably, we'd like to do that based on configurable criteria. Additionally, our script should know how to link all of the compiled files in a binary or, even better, build whole solutions that can be reused ...