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

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.

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