Hello everyone,
I’ve been super busy at work hence the delay in this blog post that summarizes our 52nd San Diego C++ Meetup.
This time, we hosted a Qualcommer co-worker – Alex Reinking.
Alex is very knowledgeable in many fields and in specific, he likes build systems. We had an offline chat before he submitted the talk for the July session. We talked about CMake, FetchContent, Package managers and it was very clear to me that Alex would be great speaker in our meetup.
Here is the recording:
Alex started by taking us through the general idea of CMake as a build generator and also mentioned its popularity in the community.
Few important bullet points and take away:
- Under the hood, Package managers do more that you realize. Like patching code and similar.
- As a library, we would like to provide the users the best experience when using our library with CMake.
- Alex has a very nice demo posted here: alexreinking/sdcppmu-diffuse. Building on Linux and MSVC with Emscripten targeting WebAssembly(Linux)/SDL(Windows).
- Always use the latest CMake. This is something that I like in CLion which is usually bundled with the latest, or almost latest CMake.
- CMake’s find_package() is the one true way to locate dependencies.
- FetchContent is nice but it will clutter your CMake “env”, unless upstream project is written well enough.
- Halide (see Alex’s Bio) has CMake helper functions like “add_halide_generator“, “add_halide_library“) – a generator that must run on the host system, even when cross compilation is involved.
- Alex walks us through the path of getting your library “FetchContent” friendly for consumers.
- Interesting discussion on Why we should not create 2 targets (shared and static for example).
- Why not GLOB for sources.
- Discussion on what to place and NOT to place in your CMakeLists.txt. For example – compile options – probably should go into Presets.
- Other CMake related constructs that were new to me: ALIAS for add-Library, BASE_DIRS + target_sources, GenerateExportHeader, CMakeDependentOption, CMakePackageConfigHelpers, configure_package_config_file, write_basic_package_version_file.
More information can be found in the recording.
Thank you!
Kobi
Abstract
In this talk, we will discuss the best practices as of the latest CMake version for writing C and C++ libraries that are easy for everyone to consume. We will build a toy application that demonstrates switching between static and shared library types, dependency management, creating accurate CMake config packages, and cross-compilation involving code generators. We will see how writing CMake lists that are minimal and declarative leads to more correct and maintainable builds.
Bio
Alex is a research scientist at Qualcomm Compiler Labs. He got his Ph.D. in Programming Languages from UC Berkeley in 2022. Since 2020, Alex has served as an open-source maintainer for Halide, a popular DSL for optimizing image and tensor processing; in this capacity, he has overhauled the build system, CI infrastructure, and the release process. Alex has contributed build system improvements to prominent open-source projects, including WABT, tinyxml2, and Herb Sutter’s cppfront, and has found and fixed large CMake bugs. His blog page: https://alexreinking.com