fmt-formatting
:::warning [UNTESTED] on real silicon
Verified on native_sim only. fmt is header-only and portable; on-target the same #includes and API calls build and run unchanged — only the C++ toolchain knobs in prj.conf differ.
:::
Type-safe string formatting into a fixed stack buffer — no allocation, no <iostream> — via the vendored {fmt} 11.0.2.
Source: examples/peripheral-io/fmt-formatting/.
What it does
Formats an int, a float ({:.2f}), and a const char * into a char[64] stack buffer with fmt::format_to(), null-terminates it, and prints it.
Why it matters on an MCU
fmt gives you type-safe formatting without dragging in <iostream> or an allocator. The SDK's profile pins it to:
| Macro | Value | Effect |
|---|---|---|
FMT_HEADER_ONLY | 1 | No separate library to link. |
FMT_USE_IOSTREAM | 0 | Keeps <iostream> out of the image. |
FMT_EXCEPTIONS | 0 | No exceptions on M-class. |
:::caution format_to does not bound-check
Writing past the end of the target buffer is undefined behaviour. Use fmt::format_to_n() for anything driven by untrusted input.
:::
Header-only, so there is no Kconfig knob — libraries: [fmt] puts the vendored headers and the SDK's fmt_config.h on the include path.
board.yaml
libraries:
- name: fmt
cores: [m55_hp]
som:
sku: E1M-AEN801
preset: e1m-evk
supported_boards:
- e1m-evk
- e1m-x-evk
cores:
m55_hp:
app: ./src
peripherals: []
diagnostics:
log_level: info
Expected output
[fmt-formatting] buffer: "count=7 ratio=3.14 label=sensor-a"
[fmt-formatting] done
See also
etl-fixed-containers— the other header-only C++ library example- Examples overview