Modern CMake for C++

Book description

Write comprehensive, professional-standard CMake projects and ensure the quality and simplicity of your solutions

Purchase of the print or Kindle book includes a free eBook in the PDF format

Key Features

  • Understand and automate compilation and linking with CMake
  • Manage internal and external dependencies easily
  • Add quality checks and tests as an inherent step for your builds

Book Description

Creating top-notch software is an extremely difficult undertaking. Developers researching the subject have difficulty determining which advice is up to date and which approaches have already been replaced by easier, better practices. At the same time, most online resources offer limited explanation, while also lacking the proper context and structure.

This book offers a simpler, more comprehensive, experience as it treats the subject of building C++ solutions holistically. Modern CMake for C++ is an end-to-end guide to the automatization of complex tasks, including building, testing, and packaging. You'll not only learn how to use the CMake language in CMake projects, but also discover what makes them maintainable, elegant, and clean. The book also focuses on the structure of source directories, building targets, and packages. As you progress, you'll learn how to compile and link executables and libraries, how those processes work, and how to optimize builds in CMake for the best results. You'll understand how to use external dependencies in your project – third-party libraries, testing frameworks, program analysis tools, and documentation generators. Finally, you'll get to grips with exporting, installing, and packaging for internal and external purposes.

By the end of this book, you'll be able to use CMake confidently on a professional level.

What you will learn

  • Understand best practices for building C++ code
  • Gain practical knowledge of the CMake language by focusing on the most useful aspects
  • Use cutting-edge tooling to guarantee code quality with the help of tests and static and dynamic analysis
  • Discover how to manage, discover, download, and link dependencies with CMake
  • Build solutions that can be reused and maintained in the long term
  • Understand how to optimize build artifacts and the build process itself

Who this book is for

The book is for build engineers and software developers with knowledge of C/C++ programming who are looking to learn CMake to automate the process of building small and large software solutions. If you are someone who's just getting started with CMake, a long-time GNU Make user, or simply looking to brush up on the latest best practices, this book is for you.

Table of contents

  1. Modern CMake for C++
  2. Contributors
  3. About the author
  4. About the reviewers
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Download the color images
    6. Conventions used
    7. Get in touch
    8. Share Your Thoughts
    9. Download a Free PDF copy of this book
  6. Section 1: Introducing CMake
  7. Chapter 1: First Steps with CMake
    1. Technical requirements
    2. Understanding the basics
      1. What is CMake?
      2. How does it work?
    3. Installing CMake on different platforms
      1. Docker
      2. Windows
      3. Linux
      4. macOS
      5. Building from the source
    4. Mastering the command line
      1. CMake
      2. CTest
      3. CPack
      4. The CMake GUI
      5. CCMake
    5. Navigating the project files
      1. The source tree
      2. The build tree
      3. Listfiles
      4. CMakeLists.txt
      5. CMakeCache.txt
      6. The Config-files for packages
      7. The cmake_install.cmake, CTestTestfile.cmake, and CPackConfig.cmake files
      8. CMakePresets.json and CMakeUserPresets.json
      9. Ignoring files in Git
    6. Discovering scripts and modules
      1. Scripts
      2. Utility modules
      3. Find-modules
    7. Summary
    8. Further reading
  8. Chapter 2: The CMake Language
    1. Technical requirements
    2. The basics of the CMake Language syntax
      1. Comments
      2. Command invocations
      3. Command arguments
    3. Working with variables
      1. Variable references
      2. Using the environment variables
      3. Using the cache variables
      4. How to correctly use the variable scope in CMake
    4. Using lists
    5. Understanding control structures in CMake
      1. Conditional blocks
      2. Loops
      3. Command definitions
    6. Useful commands
      1. The message() command
      2. The include() command
      3. The include_guard() command
      4. The file() command
      5. The execute_process() command
    7. Summary
    8. Further reading
  9. Chapter 3: Setting Up Your First CMake Project
    1. Technical requirements
    2. Basic directives and commands
      1. Specifying the minimum CMake version – cmake_minimum_required()
      2. Defining languages and metadata – project()
    3. Partitioning your project
      1. Scoped subdirectories
      2. Nested projects
      3. External projects
    4. Thinking about the project structure
    5. Scoping the environment
      1. Discovering the operating system
      2. Cross-compilation – what are host and target systems?
      3. Abbreviated variables
      4. Host system information
      5. Does the platform have 32-bit or 64-bit architecture?
      6. What is the endianness of the system?
    6. Configuring the toolchain
      1. Setting the C++ standard
      2. Insisting on standard support
      3. Vendor-specific extensions
      4. Interprocedural optimization
      5. Checking for supported compiler features
      6. Compiling a test file
    7. Disabling in-source builds
    8. Summary
    9. Further reading
  10. Section 2: Building With CMake
  11. Chapter 4: Working with Targets
    1. Technical requirements
    2. The concept of a target
      1. Dependency graph
      2. Visualizing dependencies
      3. Target properties
      4. What are transitive usage requirements?
      5. Dealing with conflicting propagated properties
      6. Meet the pseudo targets
      7. Build targets
    3. Writing custom commands
      1. Using a custom command as a generator
      2. Using a custom command as a target hook
    4. Understanding generator expressions
      1. General syntax
      2. Types of evaluation
      3. Examples to try out
    5. Summary
      1. Further reading
  12. Chapter 5: Compiling C++ Sources with CMake
    1. Technical requirements
    2. The basics of compilation
      1. How compilation works
      2. Initial configuration
      3. Managing sources for targets
    3. Preprocessor configuration
      1. Providing paths to included files
      2. Preprocessor definitions
      3. Configuring the headers
    4. Configuring the optimizer
      1. General level
      2. Function inlining
      3. Loop unrolling
      4. Loop vectorization
    5. Managing the process of compilation
      1. Reducing compilation time
      2. Finding mistakes
    6. Summary
      1. Further reading
  13. Chapter 6: Linking with CMake
    1. Technical requirements
    2. Getting the basics of linking right
    3. Building different library types
      1. Static libraries
      2. Shared libraries
      3. Shared modules
      4. Position-independent code
    4. Solving problems with the One Definition Rule
      1. Dynamically linked duplicated symbols
      2. Use namespaces – don't count on a linker
    5. The order of linking and unresolved symbols
    6. Separating main() for testing
    7. Summary
    8. Further reading
  14. Chapter 7: Managing Dependencies with CMake
    1. Technical requirements
    2. How to find installed packages
    3. Discovering legacy packages with FindPkgConfig
    4. Writing your own find-modules
    5. Working with Git repositories
      1. Providing external libraries through Git submodules
      2. Git-cloning dependencies for projects that don't use Git
    6. Using ExternalProject and FetchContent modules
      1. ExternalProject
      2. FetchContent
    7. Summary
    8. Further reading
  15. Section 3: Automating With CMake
  16. Chapter 8: Testing Frameworks
    1. Technical requirements
    2. Why are automated tests worth the trouble?
    3. Using CTest to standardize testing in CMake
      1. Build-and-test mode
      2. Test mode
    4. Creating the most basic unit test for CTest
      1. Structuring our projects for testing
    5. Unit-testing frameworks
      1. Catch2
      2. GTest
      3. GMock
    6. Generating test coverage reports
      1. Avoiding the SEGFAULT gotcha
    7. Summary
    8. Further reading
  17. Chapter 9: Program Analysis Tools
    1. Technical requirements
    2. Enforcing the formatting
    3. Using static checkers
      1. Clang-Tidy
      2. Cpplint
      3. Cppcheck
      4. include-what-you-use
      5. Link what you use
    4. Dynamic analysis with Valgrind
      1. Memcheck
      2. Memcheck-Cover
    5. Summary
    6. Further reading
  18. Chapter 10: Generating Documentation
    1. Technical requirements
    2. Adding Doxygen to your project
    3. Generating documentation with a modern look
    4. Summary
    5. Further reading
      1. Other documentation generation utilities
  19. Chapter 11: Installing and Packaging
    1. Technical requirements
    2. Exporting without installation
    3. Installing projects on the system
      1. Installing logical targets
      2. Low-level installation
      3. Invoking scripts during installation
    4. Creating reusable packages
      1. Understanding the issues with relocatable targets
      2. Installing target export files
      3. Writing basic config-files
      4. Creating advanced config-files
      5. Generating package version files
    5. Defining components
      1. How to use components in find_package()
      2. How to use components in the install() command
    6. Packaging with CPack
    7. Summary
    8. Further reading
  20. Chapter 12: Creating Your Professional Project
    1. Technical requirements
    2. Planning our work
    3. Project layout
      1. Object libraries
      2. Shared libraries versus static libraries
      3. Project file structure
    4. Building and managing dependencies
      1. Building the Calc library
      2. Building the Calc Console executable
    5. Testing and program analysis
      1. Preparing the coverage module
      2. Preparing the Memcheck module
      3. Applying testing scenarios
      4. Adding static analysis tools
    6. Installing and packaging
      1. Installation of the library
      2. Installation of the executable
      3. Packaging with CPack
    7. Providing the documentation
      1. Automatic documentation generation
      2. Not-as-technical documents of professional project
    8. Summary
    9. Further reading
  21. Appendix: Miscellaneous Commands
    1. The string() command
      1. Search and replace
      2. Manipulation
      3. Comparison
      4. Hashing
      5. Generation
      6. JSON
    2. The list() command
      1. Reading
      2. Searching
      3. Modification
      4. Ordering
    3. The file() command
      1. Reading
      2. Writing
      3. Filesystem
      4. Path conversion
      5. Transfer
      6. Locking
      7. Archiving
    4. The math() command
    5. Why subscribe?
  22. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Share Your Thoughts
    3. Download a Free PDF copy of this book

Product information

  • Title: Modern CMake for C++
  • Author(s): Rafał Świdziński
  • Release date: February 2022
  • Publisher(s): Packt Publishing
  • ISBN: 9781801070058