Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
CMake Best Practices
CMake Best Practices

CMake Best Practices: Discover proven techniques for creating and maintaining programming projects with CMake

Arrow left icon
Profile Icon Dominik Berner Profile Icon Mustafa Kemal Gilor
Arrow right icon
AU$45.99 AU$51.99
Full star icon Full star icon Full star icon Full star icon Half star icon 4.8 (4 Ratings)
eBook May 2022 406 pages 1st Edition
eBook
AU$45.99 AU$51.99
Paperback
AU$64.99
Subscription
Free Trial
Renews at AU$24.99p/m
Arrow left icon
Profile Icon Dominik Berner Profile Icon Mustafa Kemal Gilor
Arrow right icon
AU$45.99 AU$51.99
Full star icon Full star icon Full star icon Full star icon Half star icon 4.8 (4 Ratings)
eBook May 2022 406 pages 1st Edition
eBook
AU$45.99 AU$51.99
Paperback
AU$64.99
Subscription
Free Trial
Renews at AU$24.99p/m
eBook
AU$45.99 AU$51.99
Paperback
AU$64.99
Subscription
Free Trial
Renews at AU$24.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Product feature icon AI Assistant (beta) to help accelerate your learning
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

CMake Best Practices

Chapter 1: Kickstarting CMake

If you're developing software using C++ or C, you have probably heard about CMake before. Over the last 20 years, CMake has evolved into something that's an industry standard when it comes to building C++ applications. But CMake is more than just a build system – it is a build system generator, which means it produces instructions for other build systems such as Makefile, Ninja, Visual Studio, Qt Creator, Android Studio, and Xcode. And it does not stop at building software – CMake also includes features that support installing, packaging, and testing software.

As a de facto industry standard, CMake is a must-know technology for any C++ programmer.

In this chapter, you will get a high-level overview of what CMake is and learn about the necessary basics to build your first program. We will have a look at CMake's build process and provide an overview of how to use the CMake language to configure build processes.

In this chapter, we will cover the following topics:

  • CMake in a nutshell
  • Installing CMake
  • The CMake build process
  • Writing CMake files
  • Different toolchains and build configurations

Let's begin!

Technical requirements

To run the examples in this chapter, you will need a recent C++ compiler that understands C++17. Although the examples are not complex enough to require the functionality of the new standard, the examples have been set up accordingly.

We recommend using any of the compilers listed here to run the examples:

  • Linux: GCC 9 or newer, Clang 10 or newer
  • Windows: MSVC 19 or newer or MinGW 9.0.0 or newer
  • macOS: Apple Clang 10 or newer

    Note

    To try out any examples in this book, we have provided a ready-made Docker container that contains all the requirements.

    You can find it at https://github.com/PacktPublishing/CMake-Best-Practices.

CMake in a nutshell

CMake is open source and available on many platforms. It is also compiler-independent, making it a very strong tool when it comes to building and distributing cross-platform software. All these features make it a valuable tool for building software in a modern way – that is, by relying heavily on build automation and built-in quality gates.

CMake consists of three command-line tools:

  • cmake: CMake itself, which is used to generate build instructions
  • ctest: CMake's test utility, which is used to detect and run tests
  • cpack: CMake's packaging tool, which is used to pack software into convenient installers, such as deb, RPM, and self-extracting installers

There are also two interactive tools:

  • cmake-gui: A GUI frontend to help with configuring projects
  • ccmake: An interactive terminal UI for configuring CMake

cmake-gui can be used to conveniently configure a CMake build and select the compiler to be used:

Figure 1.1 – cmake-gui after configuring a project

Figure 1.1 – cmake-gui after configuring a project

If you're working on the console but still want to have an interactive configuration of CMake, then ccmake is the right tool. While not as convenient as cmake-gui, it offers the same functionality. This is especially useful when you must configure CMake remotely over an ssh shell or similar:

Figure 1.2 – Configuring a project using ccmake

Figure 1.2 – Configuring a project using ccmake

The advantage of CMake over a regular build system is manyfold. First, there is the cross-platform aspect. With CMake, it is much easier to create build instructions for a variety of compilers and platforms without the need to know the specifics of the respective build system in depth.

Then, there is CMake's ability to discover system libraries and dependencies, which lessens the pain of locating the correct libraries for building a piece of software considerably. An additional bonus is that CMake integrates nicely with package managers such as Conan and vcpkg.

It is not just the ability to build software for multiple platforms, but also its native support for testing, installing, and packaging software that makes CMake a much better candidate for building software than just a single build system. Being able to define everything from building and over-testing to packaging at a single point helps tremendously with maintaining projects in the long run.

The fact that CMake itself has very few dependencies on the system and can run on the command line without user interaction makes it very suitable for build system automatization in CI/CD pipelines.

Now that we've covered briefly what CMake can do, let's learn how to install CMake.

Installing CMake

CMake is freely available to download from https://cmake.org/download/. It is available as either a precompiled binary or as source code. For most use cases, the precompiled binary is fully sufficient, but since CMake itself has very few dependencies, building a version is also possible.

Any major Linux distribution offers CMake over its package repositories. Although the pre-packaged versions of CMake are not usually the latest releases, these installations are often sufficient to use if the system is regularly updated.

Note

The minimum version of CMake to use with the examples in this book is 3.21. We recommend that you download the appropriate version of CMake manually to ensure that you get the correct version.

Building CMake from source

CMake is written in C++ and uses Make to build itself. Building CMake from scratch is possible, but for most use cases, using the binary downloads will do just fine.

After downloading the source package from https://cmake.org/download/, extract it to a folder and run the following command:

./configure make

If you want to build cmake-gui as well, configure it with the --qt-gui option. This requires Qt to be installed. Configuring will take a while, but once it's succeeded, CMake can be installed using the following command:

make install

To test whether the installation was successful, you can execute the following command:

cmake --version

This will print out the version of CMake, like this:

cmake version 3.21.2
CMake suite maintained and supported by Kitware (kitware.com/
cmake).
Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Understand what CMake is, how it works, and how to interact with it
  • Discover how to properly create and maintain well-structured CMake projects
  • Explore tools and techniques to get the most out of your CMake project

Description

CMake is a powerful tool used to perform a wide variety of tasks, so finding a good starting point for learning CMake is difficult. This book cuts to the core and covers the most common tasks that can be accomplished with CMake without taking an academic approach. While the CMake documentation is comprehensive, it is often hard to find good examples of how things fit together, especially since there are lots of dirty hacks and obsolete solutions available on the internet. This book focuses on helping you to tie things together and create clean and maintainable projects with CMake. You'll not only get to grips with the basics but also work through real-world examples of structuring large and complex maintainable projects and creating builds that run in any programming environment. You'll understand the steps to integrate and automate various tools for improving the overall software quality, such as testing frameworks, fuzzers, and automatic generation of documentation. And since writing code is only half of the work, the book also guides you in creating installers and packaging and distributing your software. All this is tailored to modern development workflows that make heavy use of CI/CD infrastructure. By the end of this CMake book, you'll be able to set up and maintain complex software projects using CMake in the best way possible.

Who is this book for?

This book is for software engineers and build system maintainers working with C or C++ on a regular basis and trying to use CMake to better effect for their everyday tasks. Basic C++ and general programming knowledge will help you to better understand the examples covered in the book.

What you will learn

  • Get to grips with architecting a well-structured CMake project
  • Modularize and reuse CMake code across projects
  • Integrate various tools for static analysis, linting, formatting, and documentation into a CMake project
  • Get hands-on with performing cross-platform builds
  • Discover how you can easily use different toolchains with CMake
  • Get started with crafting a well-defined and portable build environment for your project

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : May 27, 2022
Length: 406 pages
Edition : 1st
Language : English
ISBN-13 : 9781803244242
Category :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Product feature icon AI Assistant (beta) to help accelerate your learning
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : May 27, 2022
Length: 406 pages
Edition : 1st
Language : English
ISBN-13 : 9781803244242
Category :

Packt Subscriptions

See our plans and pricing
Modal Close icon
AU$24.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
AU$249.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just AU$5 each
Feature tick icon Exclusive print discounts
AU$349.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just AU$5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total AU$ 213.97
Modern CMake for C++
AU$64.99
CMake Best Practices
AU$64.99
CMake Cookbook
AU$83.99
Total AU$ 213.97 Stars icon

Table of Contents

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

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.8
(4 Ratings)
5 star 75%
4 star 25%
3 star 0%
2 star 0%
1 star 0%
Salim Pamukcu Jan 19, 2024
Full star icon Full star icon Full star icon Full star icon Full star icon 5
detailed, clean, updated information with examples
Feefo Verified review Feefo
POE Jun 03, 2022
Full star icon Full star icon Full star icon Full star icon Full star icon 5
While Visual Studio is seemingly the industry standard for building, testing, and packaging C++ projects, CMake is an important and stable alternative. This book provides great insights, instructions, and samples. After reading the book, it was clear that the target readers are software engineers with build system experience. If you are not a C++ developer, this book might not be for you.The authors begin the book with a quick overview of CMake, how to install it, and then building a project. True to the book’s title, best practices with regards to using CMake are highlighted throughout the book. There are not frivolous chapters; all content is applicable. Some of the most intriguing content covered include working with Docker, automation, reproducible build environments, and fuzzing tools.A nice resource to have if you need a new C++ build system or want to further your knowledge of CMake.
Amazon Verified review Amazon
Anonymous Jun 22, 2022
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Disclosure:I was a technical reviewer for this book. I was not paid by the publisher, but did get a copy of the print book and was given an online subscription at Packt.com. My name appears in the print copy as I spent time proving the Github projects were working as well as giving feedback on each chapter as the drafts came out.Review:The authors' approach is to glean from their own experiences the most useful information about CMake to present to other engineers. It is more a thorough tutorial than a reference book. This book brought new uses and approaches to my attention that I had not seen before, despite having used CMake for about 5 years.This book covers a great deal of syntax, but also relies heavily on examples to help the reader understand the concepts. In addition to what the book contains, a Github project is maintained with examples for each chapter. I highly recommend pulling the sources for those examples and trying them out yourself.I was particularly impressed with how much of the book covered interacting with other tools, and I don't mean just the compiler and linker. There is coverage of using sanitizers and fuzzing tools and auto-generating documentation. There is coverage of how to work with package managers like Conan and vcpkg, and even how to structure your CMake to act like like a poor man's package manager if you don't have one. One example of this pulls in all of Qt and builds it from source with just a few lines of CMake code. From there it can be used as a dependency as if it were a library you created yourself.I most often use CMake for projects where I'm cross-compiling and there is ample coverage of toolchain files and other concerns for embedded systems. As an example of something new to me with this book is using an emulator like qemu to run tests on a host instead of always on a target.To reinforce the learning process with each chapter, questions are provided at the end with answers in the back of the book.For those who have large existing projects and are considering the non-trivial task of migrating your project to CMake, this book gives you some strategies to consider. The authors have significant experience doing this and present a few approaches with discussion of which approach is best for the situation.A minor annoyance I have is that some of the illustrations in the book are screen captures and the appearance of white text on a dark background, while pleasing on a computer screen, is less pleasing in book form. There is at least one page where cmake-gui is presented and the size of the image is small enough that it's very hard to make out the text on it. By and large, the examples are very clear and what I am saying here about screen captures doesn't detract significantly from the book.I like that there is an index, but I've already found at least one thing not indexed that I was searching for (i.e. 'qemu'). I'm being picky here I realize, and I was still able to find the reference by scanning through the chapter dealing with cross-compilation.Overall I'm very happy with the book. Congrats to Dominik and Mustafa and thank you for putting all the work in to create it. I hope other readers enjoy it as much as I did.
Amazon Verified review Amazon
Rohan Jul 31, 2022
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
The book's introduction by the authors includes an explanation of CMake, instructions for installing it, and instructions for building a project. In keeping with the title of the book, recommended practises for using CMake are emphasised frequently. There are no pointless chapters; all of the information is useful. Working with Docker, automation, reproducible build environments, and fuzzing tools are some of the more fascinating topics covered.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.