Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
CMake Best Practices

You're reading from   CMake Best Practices Discover proven techniques for creating and maintaining programming projects with CMake

Arrow left icon
Product type Paperback
Published in May 2022
Publisher Packt
ISBN-13 9781803239729
Length 406 pages
Edition 1st Edition
Tools
Arrow right icon
Authors (2):
Arrow left icon
Dominik Berner Dominik Berner
Author Profile Icon Dominik Berner
Dominik Berner
Mustafa Kemal Gilor Mustafa Kemal Gilor
Author Profile Icon Mustafa Kemal Gilor
Mustafa Kemal Gilor
Arrow right icon
View More author details
Toc

Table of Contents (22) Chapters Close

Preface 1. Part 1: The Basics
2. Chapter 1: Kickstarting CMake FREE CHAPTER 3. Chapter 2: Accessing CMake in Best Ways 4. Chapter 3: Creating a CMake Project 5. Part 2: Practical CMake – Getting Your Hands Dirty with CMake
6. Chapter 4: Packaging, Deploying, and Installing a CMake Project 7. Chapter 5: Integrating Third-Party Libraries and Dependency Management 8. Chapter 6: Automatically Generating Documentation with CMake 9. Chapter 7: Seamlessly Integrating Code Quality Tools with CMake 10. Chapter 8: Executing Custom Tasks with CMake 11. Chapter 9: Creating Reproducible Build Environments 12. Chapter 10: Handling Big Projects and Distributed Repositories in a Superbuild 13. Chapter 11: Automated Fuzzing with CMake 14. Part 3: Mastering the Details
15. Chapter 12: Cross-Platform Compiling and Custom Toolchains 16. Chapter 13: Reusing CMake Code 17. Chapter 14: Optimizing and Maintaining CMake Projects 18. Chapter 15: Migrating to CMake 19. Chapter 16: Contributing to CMake and Further Reading Material 20. Assessments 21. Other Books You May Enjoy

Understanding the CMake build process

CMake's build process works in two steps, as shown in the following diagram. First, if it's invoked without any special flags, CMake scans the system for any usable toolchains during the configuration process and then decides what its output should be. The second step, which is when cmake --build is invoked, is the actual compilation and building process:

Figure 1.3 – CMake's two-stage build process

Figure 1.3 – CMake's two-stage build process

The standard output is Unix Makefiles unless the only detected compiler is Microsoft Visual Studio, in which case a Visual Studio solution (.sln) will be created.

To change the generator, you can pass the -G option to CMake, like this:

cmake .. -G Ninja

This will generate files to be used with Ninja (https://ninja-build.org/), an alternative build generator. Many generators are available for CMake. A list of the ones that are supported natively can be found on CMake's website: https://cmake.org/cmake/help/latest/manual/cmake-generators.7.html.

There are two main types of generators – the ones where there are many Makefile flavors and Ninja generators, which are generally used from the command line, and the ones that create build files for an IDE such as Visual Studio or Xcode.

CMake addition differentiates between single-configuration generators and multi-configuration generators. For single-configuration generators, the build files have to be rewritten each time the configuration is changed; multi-configuration build systems can manage different configurations without the need to regenerate. Although the examples in this book use single-configuration generators, they would also work on multi-configuration generators. For most of the examples, the chosen generator is irrelevant; otherwise, it will be mentioned:

In addition, there are extra generators that use a normal generator but also produce project information for an editor or IDE, such as Sublime Text 2, Kate Editor, Code::Blocks, and Eclipse. For each, you can select whether the editor should use Make or Ninja to internally build the application.

After the call, CMake will create a lot of files in the build folder, with the most notable being the CMakeCache.txt file. This is where all the detected configurations are stored. Note that when you're using cmake-gui, the first step is split into configuring the project and generating the build file. However, when it's run from the command line, the steps are merged into one. Once configured, all the build commands are executed from the build folder.

Source folders and build folders

In CMake, two logical folders exist. One is the source folder, which contains a hierarchical set of projects, while the other is a build folder, which contains the build instructions, cache, and all the generated binaries and artifacts.

The root of the source folder is wherever the top CMakeLists.txt file is located. The build folder can be placed inside the source folder, but some people prefer to have it in another location. Both are fine; note that for the examples in this book, we decided to keep the build folder inside the source folder. The build folder is often called just build, but it can take any name, including prefixes and suffixes for different platforms. When using a build folder inside the source tree, it is a good idea to add it to .gitignore so that it does not get checked in accidentally.

When configuring a CMake project, the project and folder structure of the source folder is recreated inside the build folder so that all the build artifacts are in the same position. In each mapped folder, there is a subfolder called CMakeFiles that contains all the information that's generated by CMake's configuration step.

The following code shows an example structure for a CMake project:

├── chapter_1
│   ├── CMakeLists.txt
│   └── src
│       └── main.cpp
├── CMakeLists.txt

When you execute the CMake configuration, the file structure of the CMake project is mapped into the build folder. Each folder containing a CMakeLists.txt file will be mapped and a subfolder called CMakeFiles will be created, which contains the information that's used by CMake for building:

├── build
│   ├── chapter_1
│   │   └── CMakeFiles
│   └── CMakeFiles

So far, we have used existing projects to learn about the CMake build process. We learned about the configuration and the build step, as well as generators, and that we need CMakeLists.txt files to pass the necessary information to CMake. So, let's go a step further and see what the CMakeLists.txt files look like and how the CMake language works.

You have been reading a chapter from
CMake Best Practices
Published in: May 2022
Publisher: Packt
ISBN-13: 9781803239729
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime