Installing CMake on different platforms
CMake is a cross-platform, open-source software written in C++. That means you can, of course, compile it yourself; however, the most likely scenario is that you won't have to. This is because precompiled binaries are available for you to download from the official web page at https://cmake.org/download/.
Unix-based systems provide ready-to-install packages directly from the command line.
Note
Remember that CMake doesn't come with compilers. If your system doesn't have them installed yet, you'll need to provide them before using CMake. Make sure to add the paths to their executables to the PATH
environment variable so that CMake can find them.
To avoid solving tooling and dependency problems while learning from this book, I recommend choosing the first installation method: Docker.
Let's go through different environments on which CMake can be used.
Docker
Docker (https://www.docker.com/) is a cross-platform tool that provides OS-level virtualization, allowing applications to be shipped in complete packages, called containers. These are self-sufficient bundles that contain a piece of software with all of its libraries, dependencies, and tools required to run it. Docker executes its containers in lightweight environments that are isolated one from another.
This concept makes it extremely convenient to share whole toolchains, which are necessary for a given process, configured and ready to go. I can't stress enough how easy things become when you don't need to worry about minuscule environmental differences.
The Docker platform has a public repository of container images, https://registry.hub.docker.com/, that provides millions of ready-to-use images.
For your convenience, I have published two Docker repositories:
swidzinski/cmake:toolchain
: This contains the curated tools and dependencies that are necessary to build with CMake.swidzinski/cmake:examples
: This contains the preceding toolchain and all of the projects and examples from this book.
The first option is for readers who simply want a clean-slate image ready to build their own projects, and the second option is for hands-on practice with examples as we go through the chapters.
You can install Docker by following the instructions from its official documentation (please refer to docs.docker.com/get-docker). Then, execute the following commands in your Terminal to download the image and start the container:
$ docker pull swidzinski/cmake:examples $ docker run -it swidzinski/cmake:examples root@b55e271a85b2:root@b55e271a85b2:#
Note that all of the examples are available in the directories matching this format:/root/examples/
examples/chapter-<N>/<M>-<title>
.
Windows
Installing in Windows is straightforward – simply download the version for 32 or 64 bits. You can pick a portable ZIP or MSI package for Windows Installer.
With the ZIP package, you will have to add the CMake bin directory to the PATH
environment variable so that you can use it in any directory without any such errors:
'cmake' is not recognized as an internal or external command, operable program or batch file.
If you prefer convenience, simply use the MSI installer:
As I mentioned earlier, this is open-source software, so it is possible to build CMake yourself. However, first, you will have to get a binary copy of CMake on your system. So, why use other build tools if you have your own, right? This scenario is used by CMake contributors to generate newer versions.
On Windows, we also require a build tool that can finalize the build process started by CMake. A popular choice here is Visual Studio, for which the Community Edition is available for free from Microsoft's website: https://visualstudio.microsoft.com/downloads/.
Linux
Getting CMake on Linux is the same as getting any other popular package. Simply use your package manager from the command line. Packages are usually kept up to date with fairly recent versions. However, if you are after the latest version, you can download the installation script from the website:
The script for Linux x86_64
$ wget -O - https://github.com/Kitware/CMake/releases/download/v3.20.0/cmake-3.20.0-linux-x86_64.sh | bash
The script for Linux aarch64
$ wget -O - https://github.com/Kitware/CMake/releases/download/v3.20.0/cmake-3.20.0-Linux-aarch64.sh | bash
The package for Debian/Ubuntu
$ sudo apt-get install cmake
The package for Red Hat
$ yum install cmake
macOS
This platform is also strongly supported by CMake developers. The most popular choice of installation is through MacPorts:
$ sudo port install cmake
Alternatively, you can use Homebrew:
$ brew install cmake
Building from the source
If all else fails – or if you're on a special platform – download the source from the official website and compile it yourself:
$ wget https://github.com/Kitware/CMake/releases/download/v3.20.0/cmake-3.20.0.tar.gz $ tar xzf cmake-3.20.0.tar.gz $ cd cmake-3.20.0 $ ./bootstrap $ make $ make install
Building from source is relatively slow and requires more steps. However, by doing it this way, you're guaranteed to use the latest version of CMake. This is especially apparent when compared to packages that are available for Linux: the older the version of the system, the fewer updates it gets.
Now that we have our CMake readily installed, let's learn how to use it!