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