Leanpub Header

Skip to main content

Production CMake for C++ Engineers

Fixing Dependency Hell and Slow Builds with Modern Targets, Presets and Modules

Production CMake for C++ Engineers
This book is 100% completeLast updated on 2026-11-17

The release passed CI and crashed on the first machine that ran it. The build got slower after the team doubled its cores. A dependency nobody touched stopped configuring on Monday morning.

None of these were code bugs. They were build bugs, and most C++ teams have them without knowing it.

Production CMake for C++ Engineers is a field guide to finding and fixing them: dependency hell, slow builds, leaked flags, broken packages, and CI that can’t reproduce itself. Every fix comes with a way to prove it worked.

Minimum price

$30.00

$40.00

You pay

Author earns

$

Also available for 2 book credits with a Reader Membership

PDF
About

About

About the Book

Your build shouldn’t be the slowest, least understood part of your system.

Most C++ engineers never chose to learn CMake. They inherited a CMakeLists.txt, copied a block to add a dependency, and moved on. Then the clean build hits half an hour, a dependency upgrade breaks three teams, and the release binary behaves differently from the one that passed CI.

This book is for that moment. It isn’t a beginner’s tour of CMake commands. It’s a field manual for fixing real production builds. Every chapter opens with a real-world failure: a version endpoint reporting the wrong commit, two copies of a library in one binary, a CI pipeline that broke without a single commit. Each one is traced back to its cause, and the fix is one you can apply, verify, and keep.

What you’ll learn:

  • How CMake actually works: configure, generate, and build phases, the cache, scope rules, and the policy system
  • Target-based CMake done properly, with PUBLIC, PRIVATE, and INTERFACE usage requirements and no leaked flags
  • Presets and toolchain files that make a build depend on the repository, not on the machine running it
  • Consuming dependencies with find_package, FetchContent, vcpkg, and Conan, and using dependency providers to control all of them from one place
  • Fixing dependency hell: diamond dependencies, ODR violations, version conflicts, and old dependencies that stop configuring on CMake 4
  • Producing relocatable packages others can consume, plus Python wheels built from the same project with nanobind and scikit-build-core
  • Faster builds with Ninja, ccache, precompiled headers, and unity builds, and diagnosing slow builds with -ftime-trace, build timelines, and faster linkers
  • C++20 modules and import std in CMake, including a migration path for existing codebases
  • Cross-compiling for ARM, embedded, Android, iOS, WebAssembly, and Apple universal binaries, plus CUDA as a first-class language
  • Testing and code quality: CTest, sanitizers, coverage, clang-tidy, libFuzzer targets, and SARIF annotations on pull requests
  • Production concerns: CI caching, reproducible builds, CPack, SBOMs, license tracking, and code signing
  • CMake at scale: monorepos, superbuilds, debugging CMake itself, and migrating from legacy builds

Every chapter ends with a Build Health Ledger, a measured record of what changed in configure time, clean build time, incremental rebuild time, and which problems were removed, along with how each fix was verified. Appendix D gathers every check into one build health checklist you can run against any codebase.

Who it’s for: C++ engineers who already use CMake and want to use it well, and anyone who has just been handed a team’s build and told to make it faster and stop it breaking.

Requirements: CMake 3.28 or later. Features from newer releases, including CMake 4.x, are marked by version.

Author

About the Author

Weston Ashgrove

Weston Ashgrove is a C++ systems engineer who builds software where being wrong or slow has a cost. He came up through quantitative trading, building order books, matching logic and the low-latency infrastructure underneath them and now brings the same discipline to performance engineering, concurrency, storage, networking, GPUs, and AI inference.

His rule for performance-critical code is simple: nothing is true until it has been profiled, fuzzed, and replayed. Every technique in his books comes with the measurement that proves it, the trade-off it costs, and the failure it prevents. He writes for working engineers who ship and debug production C++, and he leaves out anything that only works on a slide.

Contents

Table of Contents

Table of Contents

Front Matter
  • Preface
  • Who This Book Is For
  • What You Need
  • How This Book Is Organized
  • Conventions
  • The Build Health Ledger
Part I: The Foundations

Chapter 1: How CMake Actually Works

  • 1.1 Configure, generate, and build: the three phases and what happens in each
  • 1.2 Generators: Ninja, Make, Visual Studio, and Xcode
  • 1.3 The cache, variables, and scope rules
  • 1.4 The CMake language: lists, functions, macros, and their traps

Chapter 2: Modern Target-Based CMake

  • 2.1 Targets as the unit of everything
  • 2.2 Usage requirements: PUBLIC, PRIVATE, and INTERFACE
  • 2.3 Generator expressions without the confusion
  • 2.4 Legacy anti-patterns and how to remove them
Part II: Project Structure

Chapter 3: Structuring Real Projects

  • 3.1 Directory layout and add_subdirectory
  • 3.2 Static, shared, object, and interface libraries
  • 3.3 Symbol visibility and export headers
  • 3.4 Options, feature toggles, and configuration headers
  • 3.5 Mixed-language targets: CUDA as a first-class language

Chapter 4: Presets and Toolchains

  • 4.1 CMakePresets.json and CMakeUserPresets.json
  • 4.2 Toolchain files
  • 4.3 Managing compilers and flags without global pollution
  • 4.4 Build types and multi-config generators
Part III: Dependencies

Chapter 5: Consuming Dependencies

  • 5.1 find_package: config mode vs. module mode
  • 5.2 FetchContent
  • 5.3 vcpkg integration
  • 5.4 Conan integration
  • 5.5 Dependency providers: one place to decide where dependencies come from

Chapter 6: Producing Packages Others Can Consume

  • 6.1 install() and export sets
  • 6.2 Package config files with CMakePackageConfigHelpers
  • 6.3 Versioning and compatibility rules
  • 6.4 Relocatable packages
  • 6.5 Shipping Python extension modules with nanobind and scikit-build-core

Chapter 7: Fixing Dependency Hell

  • 7.1 Diamond dependencies and ODR violations
  • 7.2 Version conflicts across the dependency graph
  • 7.3 Transitive dependency leakage
  • 7.4 Debugging find_package with --debug-find
  • 7.5 CMake 4 and the policy floor: when old dependencies stop configuring
Part IV: Build Speed

Chapter 8: Fast Builds

  • 8.1 Ninja and build parallelism
  • 8.2 Compiler caching with ccache and sccache
  • 8.3 Precompiled headers
  • 8.4 Unity builds and their trade-offs

Chapter 9: Diagnosing Slow Builds

  • 9.1 -ftime-trace and ClangBuildAnalyzer
  • 9.2 Visualizing build timelines with ninjatracing
  • 9.3 Header dependency hygiene
  • 9.4 Link time: lld, mold, and split DWARF

Chapter 10: C++ Modules in CMake

  • 10.1 FILE_SET CXX_MODULES and module targets
  • 10.2 Dependency scanning and build ordering
  • 10.3 import std: standard library modules in C++23
  • 10.4 Migrating an existing codebase to modules
Part V: Platforms and Tooling

Chapter 11: Cross-Compilation

  • 11.1 Toolchain files for ARM and embedded targets
  • 11.2 Sysroots and finding target dependencies
  • 11.3 Android and iOS builds
  • 11.4 Windows and MSVC specifics
  • 11.5 WebAssembly with Emscripten, and Apple universal binaries

Chapter 12: Testing and Code Quality

  • 12.1 CTest and test organization
  • 12.2 Integrating sanitizers and coverage
  • 12.3 Static analysis with CMAKE_CXX_CLANG_TIDY
  • 12.4 compile_commands.json and tooling integration
  • 12.5 Fuzzing targets with libFuzzer
  • 12.6 Machine-readable diagnostics: SARIF for CI annotations

Chapter 13: Code Generation and Custom Commands

  • 13.1 add_custom_command and add_custom_target
  • 13.2 Generating sources at build time
  • 13.3 Protobuf and gRPC integration
  • 13.4 Correct dependencies for generated files
Part VI: Production

Chapter 14: CI and Reproducible Builds

  • 14.1 CMake in CI pipelines
  • 14.2 Build caching in CI
  • 14.3 Reproducible builds: deterministic outputs and path remapping
  • 14.4 Packaging and distribution with CPack
  • 14.5 SBOMs, license tracking, and code signing

Chapter 15: CMake at Scale

  • 15.1 Monorepos and large target graphs
  • 15.2 Superbuilds and ExternalProject
  • 15.3 Debugging CMake itself with --trace and the CMake debugger
  • 15.4 Migrating from legacy CMake and other build systems
Appendices
  • Appendix A: CMake Command and Property Reference
  • Appendix B: Generator Expression Reference
  • Appendix C: Toolchain and Preset Templates
  • Appendix D: Build Health Checklist

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub