Skip to content
systemcoding.dev
Toolchains

Enable float printf in newlib-nano with -u _printf_float

Make printf with %f print numbers under newlib-nano on Arm GNU Toolchain 13 by forcing the float formatter into the link with -Wl,-u,_printf_float.

1 min read
Summarize in AI

With --specs=nano.specs the Arm GNU Toolchain links newlib-nano, whose printf leaves out floating-point formatting to save several kilobytes of flash. A %f then produces nothing, with no warning and no error, which looks like a broken UART.

main.cc
#include <stdio.h>

int main(void) {
    printf("pi=%.2f\n", 3.14159);
    return 0;
}
Shell
arm-none-eabi-gcc -mcpu=cortex-m4 -mthumb --specs=nano.specs --specs=nosys.specs \
    -Wl,-u,_printf_float main.c -o main.elf
Text
pi=3.14

Without the -u flag the same program prints pi= and the number is dropped. The reason is how the C library is built. The float code lives in a separate object that only gets linked when something references the symbol _printf_float, and nothing does, because printf reaches the formatter through a table the linker cannot trace. -Wl,-u,_printf_float marks the symbol as undefined on the command line, which forces the linker to pull the object in.

scanf has its twin, -Wl,-u,_scanf_float. Each costs real flash, since the formatter drags in soft-float double conversion on cores without a double-precision FPU. The usual alternative is to print an integer scaled by 100 and skip both.

Also inEmbedded
Share

More snippets

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.