Book description
Learn CMake through a series of task-based recipes that provide you with practical, simple, and ready-to-use CMake solutions for your code
Key Features
- Learn to configure, build, test, and package software written in C, C++, and Fortran
- Progress from simple to advanced tasks with examples tested on Linux, macOS, and Windows
- Manage code complexity and library dependencies with reusable CMake building blocks
Book Description
CMake is cross-platform, open-source software for managing the build process in a portable fashion. This book features a collection of recipes and building blocks with tips and techniques for working with CMake, CTest, CPack, and CDash.
CMake Cookbook includes real-world examples in the form of recipes that cover different ways to structure, configure, build, and test small- to large-scale code projects. You will learn to use CMake's command-line tools and master modern CMake practices for configuring, building, and testing binaries and libraries. With this book, you will be able to work with external libraries and structure your own projects in a modular and reusable way. You will be well-equipped to generate native build scripts for Linux, MacOS, and Windows, simplify and refactor projects using CMake, and port projects to CMake.
What you will learn
- Configure, build, test, and install code projects using CMake
- Detect operating systems, processors, libraries, files, and programs for conditional compilation
- Increase the portability of your code
- Refactor a large codebase into modules with the help of CMake
- Build multi-language projects
- Know where and how to tweak CMake configuration files written by somebody else
- Package projects for distribution
- Port projects to CMake
Who this book is for
If you are a software developer keen to manage build systems using CMake or would like to understand and modify CMake code written by others, this book is for you. A basic knowledge of C++, C, or Fortran is required to understand the topics covered in this book.
Table of contents
- Title Page
- Copyright and Credits
- Packt Upsell
- Contributors
- Preface
- Setting up Your System
-
From a Simple Executable to Libraries
- Introduction
- Compiling a single source file into an executable
- Switching generators
- Building and linking static and shared libraries
- Controlling compilation with conditionals
- Presenting options to the user
- Specifying the compiler
- Switching the build type
- Controlling compiler flags
- Setting the standard for the language
- Using control flow constructs
- Detecting the Environment
-
Detecting External Libraries and Programs
- Introduction
- Detecting the Python interpreter
- Detecting the Python library
- Detecting Python modules and packages
- Detecting the BLAS and LAPACK math libraries
- Detecting the OpenMP parallel environment
- Detecting the MPI parallel environment
- Detecting the Eigen library
- Detecting the Boost libraries
- Detecting external libraries: I. Using pkg-config
- Detecting external libraries: II. Writing a find-module
-
Creating and Running Tests
- Introduction
- Creating a simple unit test
- Defining a unit test using the Catch2 library
- Defining a unit test and linking against Google Test
- Defining a unit test and linking against Boost test
- Using dynamic analysis to detect memory defects
- Testing expected failures
- Using timeouts for long tests
- Running tests in parallel
- Running a subset of tests
- Using test fixtures
-
Configure-time and Build-time Operations
- Introduction
- Using platform-independent file operations
- Running a custom command at configure time
- Running a custom command at build time: I. Using add_custom_command
- Running a custom command at build time: II. Using add_custom_target
- Running custom commands for specific targets at build time
- Probing compilation and linking
- Probing compiler flags
- Probing execution
- Fine-tuning configuration and compilation with generator expressions
-
Generating Source Code
- Introduction
- Generating sources at configure time
- Generating source code at configure time using Python
- Generating source code at build time using Python
- Recording the project version information for reproducibility
- Recording the project version from a file
- Recording the Git hash at configure time
- Recording the Git hash at build time
-
Structuring Projects
- Introduction
- Code reuse with functions and macros
- Splitting CMake sources into modules
- Writing a function to test and set compiler flags
- Defining a function or macro with named arguments
- Redefining functions and macros
- Deprecating functions, macros, and variables
- Limiting scope with add_subdirectory
- Avoiding global variables using target_sources
- Organizing Fortran projects
- The Superbuild Pattern
-
Mixed-language Projects
- Introduction
- Building Fortran projects that use C/C++ libraries
- Building C/C++ projects that use Fortran libraries
- Building C++ and Python projects using Cython
- Building C++ and Python projects using Boost.Python
- Building C++ and Python projects using pybind11
- Mixing C, C++, Fortran, and Python using Python CFFI
- Writing an Installer
- Packaging Projects
- Building Documentation
- Alternative Generators and Cross-compilation
- Testing Dashboards
-
Porting a Project to CMake
-
Where to start
- Reproducing the porting example
- Creating a top-level CMakeLists.txt
- How to allow both conventional configuration and configuration with CMake at the same time
- Capturing a record of what the traditional build does
- Debugging the migration
- Implementing options
- Start with the executable and very few targets, later localize scope
- Generating files and writing platform checks
- Detecting required dependencies and linking
- Reproducing compiler flags
- Porting tests
- Porting install targets
- Further steps
- Summary and common pitfalls when converting projects to CMake
-
Where to start
- Other Books You May Enjoy
Product information
- Title: CMake Cookbook
- Author(s):
- Release date: September 2018
- Publisher(s): Packt Publishing
- ISBN: 9781788470711
You might also like
book
Modern CMake for C++
Write comprehensive, professional-standard CMake projects and ensure the quality and simplicity of your solutions Purchase of …
book
CMake Best Practices
Explore a compendium of tips, tricks, and techniques for leveraging CMake and empowering your software development …
book
Efficient Linux at the Command Line
Take your Linux skills to the next level! Whether you're a system administrator, software developer, site …
video
Complete Modern C++ (C++11/14/17)
This course provides an extensive exploration of C++. The journey starts with mastering the syntax and …