WIP
Cheatsheet for working with C++ in an embedded bare-metal environment.
- Intro
- CPUs
- Hosted vs. Freestanding
- Compilers
- Assembler
- Linker
- Tools
- Build System
- Libraries
- IDE
- Debugging
- Examples
- No MMU (most of the time)
- 32 Bit
- Cortex-M Series
Numbers for Frequency, Flash & RAM where taken from the STM product lineup. Others manufacturers may vary, but will probably have a similar offering.
Feature | M0/M0+ | M3 | M4 | M7 |
---|---|---|---|---|
Microarchitecture | ARMv6-M | ARMv7-M | ARMv7E-M | ARMv7E-M |
Interrupts | 1-32 | 1-240 | 1-240 | 1-240 |
Frequency | 32-64 Mhz | 24-120 Mhz | 48-180 Mhz | 200-600 Mhz |
Flash | 8-256 kBytes | 16-1024 kBytes | 16-2048 kBytes | 64-2048 kBytes |
RAM | 2-36 kBytes | 4-128 kBytes | 16-640 kBytes | 128-1024 kBytes |
Floating Point (single) | β | β | βοΈ (optional) | βοΈ (optional) |
Floating Point (double) | β | β | β | βοΈ (optional) |
STM Series | L0, G0 | F1, F2, L1 | F3, F4, L4 | F7, H7 |
For the complete list see: GCC manual
Flag | Description | gcc | clang |
---|---|---|---|
-mcpu= |
This specifies the name of the target ARM processor. | βοΈ | βοΈ |
-mthumb |
Use thumb instructions. | βοΈ | βοΈ |
-mfloat-abi |
Specifies which floating-point ABI to use. | βοΈ | βοΈ |
- 8 Bit
- Arduino
Flag | Description | gcc | clang |
---|---|---|---|
-mmcu= |
Select the MCU chip. See GCC manual | βοΈ | βοΈ |
-mint8 |
Assume int to be 8-bit integer. Not standard! See GCC manual | βοΈ | β |
- 32 Bit
- Open Source ISA
- Headers
- Exceptions
- RTTI
Flag | Description | gcc | clang |
---|---|---|---|
-std=c++17 |
Set the language standard for C++. Possible values: 98, 11, 14, 17, 20 | βοΈ | βοΈ |
-std=gnu++17 |
Same as above, but with GNU extensions enabled. | βοΈ | βοΈ |
-ffreestanding |
Enables freestanding C/C++. Default is hosted. | βοΈ | βοΈ |
-fno-exceptions |
See fno-exceptions. | βοΈ | βοΈ |
-fno-rtti |
See fno-rtti. | βοΈ | βοΈ |
-nostdinc++ |
Do not search for header files in the standard directories specific to C++. | βοΈ | βοΈ |
Disable exception handling. See GCC manual for more details.
Disable generation of information about every class with virtual functions for use by the C++ run-time type identification features dynamic_cast
and typeid
. Can save space if not needed. Mixing code compiled with -frtti
with that compiled with -fno-rtti
may not work.
Flag | Description | gcc | clang |
---|---|---|---|
-O |
Sets optimization level. Possible values: 0,1,2,3,s,g & z on clang. |
βοΈ | βοΈ |
-flto |
Enables link time optimization. | βοΈ | βοΈ |
-fno-unroll-loops |
Disable loop unrolling. Can save space. | βοΈ | βοΈ |
Flag | Description | gcc | clang |
---|---|---|---|
-Og |
Enable optimization level which is tuned for best debuggability while still performing OK. | βοΈ | βοΈ |
-g |
Include debug symbols. | βοΈ | βοΈ |
-ggdb |
βοΈ | βοΈ |
Flag | Description | gcc | clang |
---|---|---|---|
-Wall |
Basic warnings. Should always be used. | βοΈ | βοΈ |
-Wextra |
Everything from -Wall plus more. |
βοΈ | βοΈ |
-Wpedantic |
Issue all the warnings demanded by strict ISO C and ISO C++. | βοΈ | βοΈ |
-Werror |
Make all warnings into errors. | βοΈ | βοΈ |
Flag | Description | gcc | clang |
---|---|---|---|
-x assembler-with-cpp |
Enables the C preprocessor for assembly files. | βοΈ | βοΈ |
When using g++ or clang++ as the linker front end prefix all flags with -Wl. Example: -Wl,--no-undefined
Flag | Description | gcc | clang |
---|---|---|---|
-Tpath/to/linkerscript |
Path to the location of the linker script | βοΈ | βοΈ |
-fstack-usage |
βοΈ | β | |
--print-memory-usage |
βοΈ | β | |
-static |
βοΈ | βοΈ |
In this section will take a detailed look at a couple of build system, that can be used for embedded bare-metal development. The two most common right now are Makefile or CMake projects, so let's start with those.
First a quick comparison:
Makefile | CMake |
---|---|
Easy initial setup | The first CMake toolchain file can be a little tricky. |
Portable across Unix-like development hosts | Portable across all development hosts (Linux,macOS,Unix,Windows) |
Clear output showing the set flags & options | Default output does not show direct compiler invocation |
Easy management of multiple executables & libraries (shared code) | |
Combine host tests with device code in same project | |
Some IDE support | Supported by many IDEs and editors |
Generate compile-commands.json via the bear cli utility |
Generate compile-commands.json directly with CMake |
- STL
- STL-like
- Boost
- Terminal + Editor
- VS Code
- CLion
# Launches QEMU as an Arduino Uno and printing USART to stdio.
qemu-system-avr -machine uno -bios firmware.elf -serial mon:stdio -nographic
The examples folder contains some basic Makefile & CMake based projects.