Hulton-Harrop | Minimal CMake | E-Book | www.sack.de
E-Book

E-Book, Englisch, 266 Seiten

Hulton-Harrop Minimal CMake

Learn the best bits of CMake to create and share your own libraries and applications
1. Auflage 2025
ISBN: 978-1-83508-065-8
Verlag: De Gruyter
Format: EPUB
Kopierschutz: 0 - No protection

Learn the best bits of CMake to create and share your own libraries and applications

E-Book, Englisch, 266 Seiten

ISBN: 978-1-83508-065-8
Verlag: De Gruyter
Format: EPUB
Kopierschutz: 0 - No protection



Minimal CMake guides you through creating a CMake project one step at a time. The book utilizes the author's unique expertise in game and engine development to craft compelling examples of how CMake can be used to build complex software. The chapters introduce concepts gradually, each one building on the last. Throughout the course of the book, you will progress from a simple console application all the way through to a full windowed app.
The book will help you build a strong foundation in CMake that will translate to future projects. You'll learn how to integrate existing software libraries to enhance your app's functionality, how to build reusable libraries to share with others, and how to manage developing for multiple platforms simultaneously, including macOS, Windows, and Linux. You'll also find out how CMake facilitates testing and how to package your application ready for distribution.
The book aims to not overwhelm you with everything there is to know about CMake. Instead, it focuses on the most relevant and important parts that will help you become productive quickly.
By the end of this book, you will be a confident CMake user and will have gained the skills and experience to build and share your own libraries and applications.

Hulton-Harrop Minimal CMake jetzt bestellen!

Autoren/Hrsg.


Weitere Infos & Material


1


Getting Started


The objective of is to walk you through the development process of taking an application from its humble beginnings (starting with a simple console application) to a complete windowed application you can demo to friends and use as a template for future projects.

We’ll see how CMake can help throughout the entire process. Perhaps the greatest benefit CMake provides is how easy it is to integrate existing open source software to improve the functionality of your app.

Before we can start using CMake to create our application, we need to ensure that we have our development environment set up and ready to go. The setup will vary depending on which platform you’ve decided to use (Windows, macOS, or Linux). We’ll cover each system here. This will provide a good starting point to build on as we introduce CMake and begin assembling the core of our application.

In this chapter, we’re going to cover the following topics:

  • Installing CMake on Windows
  • Installing CMake on macOS
  • Installing CMake on Linux (Ubuntu)
  • Installing Git
  • Visual Studio Code setup (optional)

Technical requirements


To get the most out of this book, we recommend that you run the examples locally. To do this you will need the following:

  • A Windows, Mac, or Linux machine with an up-to-date operating system (OS)
  • A working C/C++ compiler (a system default is recommended for each platform if you don’t already have this)

The code examples in this chapter can be found by following this link: https://github.com/PacktPublishing/Minimal-CMake.

CMake version

All examples in this book have been tested with CMake 3.28.1. Earlier versions are not guaranteed to work. Later versions should be safe to upgrade to, though it’s possible that there may be differences. If in doubt, use CMake 3.28.1 when running the examples from this book.

Installing CMake on Windows


In this section, we’ll cover how to install everything you’ll need to start building applications with CMake on Windows.

To begin with, you will need a C/C++ compiler. If you don’t already have a compiler installed, a good choice to go with is Visual Studio (Visual Studio 2022 Community Edition can be downloaded from https://visualstudio.microsoft.com/vs/community/).

Visual Studio is an integrated development environment that comes with Microsoft’s C++ compiler for Windows (cl.exe). We won’t be covering Visual Studio directly, though you are more than welcome to use it if you prefer (see for a brief summary). We’ll cover how to generate Visual Studio solution files and invoke MSBuild to build our project. To keep things as consistent as possible, we’ll use Visual Studio Code for most of the examples. This is more for convenience than anything else, and if you’re more comfortable with another tool, by all means use it. As CMake has grown in popularity, Visual Studio has expanded support for CMake considerably, which is worth investigating if you’re predominantly going to be developing on Windows.

Visual Studio versus Visual Studio Code

Although they sound similar, Visual Studio and Visual Studio Code are two hugely different applications. Visual Studio is an integrated development environment from Microsoft that runs predominantly on Windows (there is also, confusingly, a version of Visual Studio for macOS which is quite different to the Windows application). Visual Studio is used for building Windows applications in C++ or .NET (C#, F#, and Visual Basic). Visual Studio Code on the other hand is a cross-platform code editor that runs on Windows, macOS, and Linux. It has an extensive extension library that allows it to be used with many different programming languages. It’s popular for web development, having excellent support for TypeScript and JavaScript, though it also has robust support for C++ through the C/C++ extension from Microsoft. Visual Studio Code is what we’ll use throughout this book.

Open the Visual Studio installer and select Visual Studio Community 2022 (if there’s a later version available when you’re reading this, feel free to grab that instead).

Figure 1.1: Visual Studio Installer version selector

After selecting Visual Studio Community 2022, a new panel will appear. The Workloads tab lets you select an option to include a set of sensible defaults. Scroll down and select Desktop development with C++.

Figure 1.2: Visual Studio Installer workload selector

There are several optional components selected by default on the right. It won’t hurt to leave them selected. You can remove certain features such as Image and 3D model editors and Test Adapter for Boost/Google.Test if you prefer.

After confirming your selection, click on Install in the bottom-right corner of the window.

Once the installation is complete, go to the Windows Start menu and follow these steps:

Figure 1.3: Windows 11 Taskbar Search Box

  1. Search for Terminal.

Figure 1.4: Windows 11 application search results

  1. Open the Terminal app. Then, from the top bar, click the dropdown and select Developer Command Prompt for VS 2022.

Figure 1.5: Microsoft Terminal new tab selector

Customizing Command Prompt

It is possible to modify the default Command Prompt in Windows Terminal to run VsDevCmd.bat when it’s launched with the host and target architectures specified. To do this, go to Settings from the Windows Terminal drop-down menu and then select Open JSON file in the lower-left corner of the screen. In the profiles section, find the Command Prompt entry under list and change the commandLine property to include the path to VsDevCmd.bat and the required architectures (e.g., "commandline": "%SystemRoot%\\System32\\cmd.exe /k \"C:\\Program Files\\Microsoft Visual Studio\\2022\\Community\\Common7\\Tools\\VsDevCmd.bat\" -arch=x64 -host_arch=x64"). It’s also possible to call VsDevCmd.bat when opening Git Bash from Windows Terminal (see the section if you don’t already have it installed). To do this, find Git Bash in the list of profiles and add the following: "commandLine": "\"C:\\Program Files\\Microsoft Visual Studio\\2022\\Community\\Common7\\Tools\\VsDevCmd.bat\" -arch=x64 -host_arch=x64 && \"%PROGRAMFILES%/Git/bin/bash.exe\" -i -l"

  1. To verify that the Microsoft compiler is working as expected, run cl.exe. You should then see the following printed (the architecture will vary depending on the machine you’re using):

Figure 1.6: Running cl.exe from Developer Command Prompt

CMake and Visual Studio

Visual Studio comes bundled with its own version of CMake, which you can rely on and skip the following two steps. It is located in C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin. Running cmake --version will show cmake version , which indicates that the version is distinct from normal CMake.

  1. If you do not currently have CMake installed on your system (or have a fairly old version of CMake installed), head to https://cmake.org/download/ to get the latest release (at the time of writing, this is 3.28.1).

    The simplest option is to grab the Windows x64 installer (cmake-3.28.1-windows-x86_64.msi) and follow the standard installation instructions.

Figure 1.7: CMake Windows installer

  1. Ensure that you select Add CMake to the system PATH for the current user.

Figure 1.8: CMake installer PATH options

  1. Follow the remaining installation instructions and wait for CMake to install. Once this is completed, close and open Terminal and return to Developer Command...


Hulton-Harrop Tom  :

Tom Hulton-Harrop is a software developer with over 10 years of experience in game and engine development. Tom started his career in 2011 at Electronic Arts working on the Need for Speed series of racing games. In 2014, he joined Fireproof Games where he worked on The Room Three and a little-known VR game called Omega Agent. After a stint at a small start-up called Glowmade, Tom joined Amazon as part of the Lumberyard Game Engine team (later becoming Open 3D Engine). During this time, Tom became interested in open source development and started sharing multiple hobby projects on GitHub. It was this experience that led Tom to CMake, where he slowly came to appreciate the power and simplicity of the tool. Tom now works at a start-up called Amutri, helping deliver 3D experiences to engineering, manufacturing, and lighting customers.



Ihre Fragen, Wünsche oder Anmerkungen
Vorname*
Nachname*
Ihre E-Mail-Adresse*
Kundennr.
Ihre Nachricht*
Lediglich mit * gekennzeichnete Felder sind Pflichtfelder.
Wenn Sie die im Kontaktformular eingegebenen Daten durch Klick auf den nachfolgenden Button übersenden, erklären Sie sich damit einverstanden, dass wir Ihr Angaben für die Beantwortung Ihrer Anfrage verwenden. Selbstverständlich werden Ihre Daten vertraulich behandelt und nicht an Dritte weitergegeben. Sie können der Verwendung Ihrer Daten jederzeit widersprechen. Das Datenhandling bei Sack Fachmedien erklären wir Ihnen in unserer Datenschutzerklärung.