CMake introduction and hands-on workshop

CMake is a language-agnostic, cross-platform build tool and is nowadays the de facto standard, with many large projects using it to reliably build, test, and deploy their codebases.

Learning outcomes

You will learn how to:

  • Write a CMake build system for C, C++, and Fortran projects producing libraries and/or executables.

  • Run tests for your code with CTest.

  • Ensure your build system will work on different platforms.

  • Detect and use external dependencies in your project.

  • Recognize modular patterns but also recognize and know how to avoid problematic and later unmaintainable “spaghetti” patterns.

Topics not covered

  • Probing compilation, linking, and execution

  • Composing CMake projects from non-CMake projects

  • Superbuilds

  • Cross-compilation

  • GPUs

  • Mixing C/C++/Fortran with Python

  • pkg-config for finding dependencies

Prerequisites

Software requirements

Before attending this workshop, please make sure that you have access to a computer with a compiler for your favorite language and a recent version of CMake. Instructions on how to set up these dependencies can be found on the Setting up your system page.

Who is the course for?

This course is for students, researchers, engineers, and programmers that have heard of CMake and want to learn how to use it effectively with projects they are working on. This course assumes no previous experience with CMake. You will have to be familiar with the tools commonly used to build software in your compiled language of choice: C++, C, Fortran. Specifically, this lesson assumes that participants have some prior experience with or knowledge of the following topics (but no expertise is required):

  • Compiling and linking executables and libraries.

  • Differences between shared and static libraries.

  • Automated testing.

Note

You can find all examples, exercises, and solutions at https://github.com/coderefinery/cmake-workshop. In there navigate to content/examples/.

Schedule

Timing still being adjusted. It’s still too much material:

15 min

Motivation and overview

15 min

Brief tour to demonstrate concepts/capabilities

Lunch break

15 min

Type-along: building an executable and setup help

15 min

Exercise: building and linking a library

10 min

Compile flags, definitions, and debugging

20 min

Target-based build systems with CMake

10 min

Break

15 min

Building a larger project with multiple folders

20 min

Detecting your environment

20 min

Finding and using dependencies

10 min

Break

10 min

Creating and running tests with CTest

15 min

Exercises: testing with CTest

20 min

Automated dependency handling with FetchContent

15 min

Growing projects: pitfalls and recommendations