Posts for: #Cmake

CMake on STM32 | Episode 8: building with Docker

CMake on STM32 | Episode 8: building with Docker

In this episode, we will use Docker to set up and manage our build environment.

The embedded world doesn’t always embrace the latest software development techniques. I acknowledged this in the previous episode about unit tests. Four years ago, when I started this series, I was totally unaware of Docker for instance. Since then, I have used it for non-embedded projects and I think it could have solved some issues I encountered in the past. I would like to share my experience and demonstrate that it can perfectly fit in an MCU project.

CMake on STM32 | Episode 7: unit tests

CMake on STM32 | Episode 7: unit tests

It’s been almost four years, and it’s about time to revive this series on CMake and STM32 (or MCUs in general, STM32 just serves as a tangible example).

Funny enough, I’m not working with microcontrollers anymore… This is a very recent change in my professional career, even if I think I will probably work again with MCUs sooner or later. However, I still use CMake to build C++ projects! The real reason to resume this series is my desire to write an article about Docker for builds. In 2020, I had never used Docker at all. Today, I use it very often, and I’m convinced it could have solved some build and environment issues back then.

CPM.cmake to make CMake’s FetchContent easier

Scrolling through my list of starred projects on GitHub, I saw a tool I had forgotten, CPM.cmake, and decided that it was time to try it!

CPM.cmake (or just CPM) describes itself as “a cross-platform CMake script that adds dependency management capabilities to CMake”. It’s in fact a wrapper around CMake’s FetchContent module that makes it much easier to download packages.

To demonstrate its usage, I will build a project that depends on 3 libraries from GitHub: the famous Catch2 and fmt, and the probably not-as-famous-as-it-deserves scnlib.

The SYSTEM property from CMake 3.25

CMake 3.25 introduced a new property called SYSTEM. It will help us handle warnings from 3rd party libraries. Let’s see how!

The issue (with a minimal example project)

Here is a minimal example project to reproduce the issue. It simply prints something with the great fmt library:

#include <fmt/core.h>

int main()
{
    fmt::print("The answer is {}", 42);
}

The library is fetched from GitHub thanks to CMake’s FetchContent module:

cmake_minimum_required(VERSION 3.25)
project(CMake_SYSTEM)

# Set C++ standard
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

# Fetch fmt from GitHub
include(FetchContent)

FetchContent_Declare(
        fmt
        GIT_REPOSITORY https://github.com/fmtlib/fmt.git
        GIT_TAG 9.1.0
)

FetchContent_MakeAvailable(fmt)

# Create executable
add_executable(${PROJECT_NAME} main.cpp)

target_compile_options(${PROJECT_NAME} PRIVATE -Wall -Wextra -pedantic -Wswitch-enum)

target_link_libraries(${PROJECT_NAME} PRIVATE fmt)

The project can be built with:

CMake on STM32 | Episode 6: add C++ specific compiler options

CMake on STM32 | Episode 6: add C++ specific compiler options

In episode 4, we started to use C++ but we didn’t (*) add C++ specific compiler options. In this episode we will see how to do this. Because guess what: C++ and C are different languages and compiling C code with C++ specific options may not be a good idea 😄

Generator expression

(*) This isn’t completely true: to fix a linker error I suggested to add -fno-exceptions and this is clearly a C++ specific option. I didn’t show any CMake code but if you tried on your computer, I suppose you simply added it like this and compilation went fine: