Try Embedded

Toolchains and cross-compilation

Target triplets, sysroots, float ABI and the flags that must agree.

Cross-compiling

A native compiler builds programs for the machine it runs on. Firmware is built on a laptop (x86-64 or Apple silicon) for a different CPU (an Arm Cortex-M, a RISC-V core), so it needs a cross compiler: one that runs on the host and emits code for the target.

What a toolchain contains

ComponentExamplesJob
Compiler driverarm-none-eabi-gcc, clang --target=...Runs the stages with the right options
Compiler propercc1, cc1plusC/C++ to assembly
Binutilsas, ld, objcopy, objdump, nm, size, readelf, addr2lineAssemble, link, inspect
C librarynewlib, newlib-nano, picolibc (bare metal); glibc, musl (Linux)printf, memcpy, malloc...
Compiler runtimelibgcc, compiler-rtHelpers the CPU lacks, such as 64-bit division
Debuggergdb-multiarch, arm-none-eabi-gdbTalks to the target through a debug probe

Arm publishes the "Arm GNU Toolchain" for arm-none-eabi; vendor IDEs bundle the same compiler.

Target triplets

The prefix of every tool is a triplet: arch-vendor-os-abi (vendor is often omitted).

TripletMeans
arm-none-eabi32-bit Arm, no OS (bare metal), Embedded ABI. Microcontrollers.
arm-linux-gnueabihf32-bit Arm running Linux with glibc, hard-float ABI. Raspberry Pi OS 32-bit.
aarch64-linux-gnu64-bit Arm Linux.
riscv32-unknown-elf / riscv64-unknown-elfRISC-V bare metal.
xtensa-esp32-elfEspressif's Xtensa cores.

none versus linux is the important part: a bare-metal toolchain has no system calls, so things like printf need you to provide _write (to a UART or semihosting), and there is no dynamic loader.

CPU and FPU flags must agree everywhere

For Cortex-M the triplet does not say which core. Flags do:

-mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard
  • -mcpu selects the instruction set and tunes scheduling (cortex-m0plus, cortex-m33, cortex-m7...).
  • -mthumb: Cortex-M only runs Thumb code.
  • -mfpu names the floating-point unit, if there is one.
  • -mfloat-abi decides how floating point is done and passed:
-mfloat-abiFPU instructions usedFloat arguments passed inLinks with
softNo (software emulation)Integer registerssoft or softfp objects
softfpYesInteger registerssoft or softfp objects
hardYesFPU registershard objects only

Every object file and library in the link must use a compatible ABI. Mixing them gives the error uses VFP register arguments, x.o does not. Precompiled vendor libraries (DSP, BLE stacks) are shipped in several variants for this reason; pick the one matching your flags.

Pitfall: Enabling -mfloat-abi=hard is not enough on its own. The FPU is off after reset, and the startup code must enable it (set CP10 and CP11 full access in SCB->CPACR) before any float instruction runs, or the first one causes a UsageFault.

Multilib: the right library for the flags

The toolchain ships many builds of newlib and libgcc, one per combination of core and float ABI. The driver picks the matching one from your flags. Ask which it will use:

arm-none-eabi-gcc -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16 -print-multi-directory
thumb/v7e-m+fp/hard

This is why the CPU flags must also be passed at link time, not only when compiling.

Sysroot

For an embedded Linux target, the compiler also needs the target's headers and libraries (glibc, OpenSSL, and so on) to compile and link against. They live in a directory tree mirroring the target's root filesystem, the sysroot:

aarch64-linux-gnu-gcc --sysroot=/opt/sdk/sysroots/cortexa53-poky-linux app.c -lssl -o app

Yocto and Buildroot generate an SDK containing a matching cross compiler and sysroot, so applications are built against exactly the libraries on the device.

Reproducible toolchains

The same source built with two compiler versions can produce different binaries and occasionally different behaviour. Pin the version:

  • record it in the repository (arm-none-eabi-gcc --version in CI logs)
  • build in a container image with the toolchain installed
  • treat a toolchain upgrade as a change that needs testing, like any other

You can check what target a compiler assumes at any time; the snippet below shows the C equivalent of asking the preprocessor what it defines for the current target:

#include <stdio.h>

int main(void) {
  printf("sizeof(int) = %u, sizeof(long) = %u, sizeof(void *) = %u\n",
         (unsigned)sizeof(int), (unsigned)sizeof(long), (unsigned)sizeof(void *));
  printf("sizeof(long long) = %u, sizeof(double) = %u\n",
         (unsigned)sizeof(long long), (unsigned)sizeof(double));
  return 0;
}

On a real toolchain, arm-none-eabi-gcc -mcpu=cortex-m4 -dM -E - < /dev/null lists every predefined macro, including __ARM_ARCH, __ARM_FP and __ARM_PCS_VFP (set when the hard-float ABI is in use).

Interview question: You link a vendor's precompiled library and get "uses VFP register arguments, main.o does not". What is wrong?

Answer: The float ABIs disagree: the library was built with -mfloat-abi=hard (floats passed in FPU registers) and your objects with soft or softfp. Every object in a link must share a compatible calling convention. Either compile your code with the same -mcpu, -mfpu and -mfloat-abi=hard, or use the library variant built for softfp. Also pass the same CPU flags to the linker so it selects the matching multilib.

Interview question: What is a sysroot and when do you need one?

Answer: A directory containing the target's headers and libraries laid out like its root filesystem. When cross-compiling for embedded Linux, the compiler and linker must use the target's glibc, kernel headers and libraries rather than the host's, so you pass --sysroot (or use the SDK generated by Yocto or Buildroot, which sets it). Bare-metal toolchains include their own C library and usually do not need a separate sysroot.

Going deeper

Interview question: How would you move a product from one MCU vendor's compiler to GCC without changing behaviour?

Answer: Build with both and compare warnings, sizes and the map file, then run the full test suite on the target with each. Look for reliance on implementation-defined or undefined behaviour, compiler-specific pragmas and attributes, startup and linker script differences, and library functions with different behaviour. Pin the new toolchain version.

Interview question: What is semihosting and why is it slow?

Answer: Semihosting lets code on the target use the host's I/O (printf to the debugger console, file access) by executing a breakpoint instruction that the debugger traps, services and then resumes. Every call halts the CPU and round-trips through the debug probe, so it is very slow and breaks real-time behaviour, and a build with semihosting hangs if no debugger is attached. RTT or a UART is used instead for logging.

Interview question: Why might newlib-nano's printf not print floating-point numbers?

Answer: To save space, newlib-nano's printf leaves out floating-point formatting unless it is requested with the linker flag -u _printf_float. Without it, %f prints nothing or garbage. Adding it costs several kilobytes of flash, so many projects print scaled integers instead.

Explain it: In your own words, explain what the parts of the triplet arm-none-eabi tell you about a toolchain.

Answer: arm says it generates code for 32-bit Arm processors. none says there is no operating system: the code runs on bare metal, so there are no system calls or dynamic loader, and functions like printf need you to provide low-level output routines. eabi says it follows the Arm Embedded Application Binary Interface, which defines the calling convention, data type sizes and object file format, so code from different compilers and libraries can be linked together. Which exact core it targets is chosen separately with flags such as -mcpu.