CMake is a cross-platform open-source meta-build system which can build, test and package software. It can be used to support multiple native build environments including make, Apple’s xcode and Microsoft Visual Studio.
This repository includes some example modern CMake configurations which I have picked up when exploring it’s usage for various projects. The examples are laid out in a tutorial like format. The first examples are very basic and slowly increase in complexity drawing on previous examples to show more complex use cases.
These examples have been tested on Ubuntu 22.04 but should work under any Linux system that supports CMake v3.10+.
The basic requirements for most examples are:
-
CMake v3.10+
-
A c++ compiler (defaults to gcc)
-
make
The easiest way to install the above on Ubuntu is as follows
$ sudo apt-get install build-essential
$ sudo apt-get install cmake
Some specific examples may require other tools including:
In-place builds generate all temporary build files in the same directory structure as the source code. This means that all Makefiles and object files are interspersed with your normal code. To create an in-place build target run the cmake command in your root directory. For example:
$ cmake .
Out-of-source builds allow you to create a single build folder that can be anywhere on your file system. All temporary build and object files are located in this directory keeping your source tree clean. To create an out-of-source build run the cmake command in the build folder and point it to the directory with your root CMakeLists.txt file. Using out-of-source builds if you want to recreate your cmake environment from scratch, you only need to delete your build directory and then rerun cmake.
For example:
$ mkdir build
$ cd build
$ cmake ..
Below is sample output from building this example.
$ mkdir build
$ cd build
$ cmake ..
$ make