Skip to main content

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:

MacroValueEffect
FMT_HEADER_ONLY1No separate library to link.
FMT_USE_IOSTREAM0Keeps <iostream> out of the image.
FMT_EXCEPTIONS0No 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

Questions about this page? Discuss in Community Forum