EMBEDDED TOOLKIT

SYSTEMS R&D // DEV_TOOLS SUITE

Tools (65)

gcc attributes compiler flags

SYSTEM ONLINE
Key GCC Compiler Extensions for Embedded
__attribute__((packed))
Instructs the compiler NOT to insert any empty alignment bytes (padding) inside a struct. Often causes Unaligned Access.
Use Case: Mapping network headers (TCP/IP) or raw binary UART frames.
__attribute__((aligned(X)))
Forces alignment of a variable/struct to a boundary of X bytes in memory.
Use Case: Required e.g. by hardware DMA controller, which can only read aligned addresses in SRAM.
__attribute__((section(".name")))
Places the compiled function code or variable in a specific section defined by the programmer in the linker script (.ld).
Use Case: Placing critical ISR functions in fast RAM (ITCM) instead of Flash, or saving certificates in Flash.
__attribute__((weak))
Marks the function as 'weak'. If you write a function with the same name anywhere else in the code, this weak one will be seamlessly overridden.
Use Case: Default interrupt handlers (Default_Handler) or callbacks generated in CubeMX (HAL_UART_RxCpltCallback).
__attribute__((always_inline))
Forces the compiler to absolutely copy the function's code into the call site, even if the -O0 flag is enabled.
Use Case: Ultra-fast, low-latency code in interrupts, or macros replacing bitwise functions.
__attribute__((naked))
The compiler generates no prologue or epilogue for this function. It compiles C directly as assembly inside.
Use Case: Writing custom RTOS procedures (Context Switch), where you manually manage SP, LR, PC registers.
__attribute__((used))
Tells the linker (LTO) to absolutely not discard this code, even if it seems uncalled anywhere in the program.
Use Case: Interrupt vector tables (Vector Table) or functions called only from assembly.

📖 Compiler and Linker Magic

Writing clean C code itself is often not enough on the smallest and most hardware-sensitive devices.

  • GCC attributes inform the linking and Assembly generation process on how to optimize data in memory (bypassing C99/C11 standard rules).
  • Even the most optimized code with -O0 flag may turn out sluggish, and with -O3 grow so much in size (e.g. through FOR loop unrolling) that it won't fit in Flash. Always balance using -Os and flags -flto.