C Mastery / Cross Compilation and Toolchains
Part 5 — Compilation, Linking, and Building

Cross Compilation and Toolchains

This chapter explains cross compilation — building code on one machine to run on another — and the toolchains that make it possible.

Why This Matters

Embedded firmware, mobile, and many systems targets are compiled on a host different from the target. Cross compilation is how firmware gets built, and understanding toolchains is essential for embedded and low-level work.

Prerequisites

Core Concept

A toolchain is the set of tools (compiler, assembler, linker, libc) that produce code for a specific target. A cross compiler runs on a host (e.g., x86-64 Linux) but generates code for a different target (e.g., ARM Cortex-M).

arm-none-eabi-gcc -mcpu=cortex-m4 -mthumb -c main.c -o main.o

The compiler triple (arm-none-eabi) identifies the target.

Examples

Building firmware for ARM (illustrative)

arm-none-eabi-gcc -mcpu=cortex-m4 -mthumb -ffreestanding -nostdlib \
    -T linker.ld startup.c main.c -o firmware.elf

Cross compiling with CMake

cmake -S . -B build \
    -DCMAKE_TOOLCHAIN_FILE=toolchain.cmake
cmake --build build

How It Works

A cross compiler knows the target's instruction set, ABI, and system interfaces. The linker combines objects into a target-format executable (ELF for embedded, often converted to a binary/hex firmware image later).

Variations

Bare-metal vs. Linux cross

Sysroot

A sysroot provides the target's headers and libraries so the cross compiler can compile against them.

Common Mistakes

Undefined Behavior

toolchain error, not ISO C UB.

Portability

Under the Hood

The compiler's back end (c.compiler.3) targets a specific architecture. The assembler and linker understand that target's object format and relocations. c.cpu.5 covers the architectures.

Practical Usage

Exercises

1. If a cross compiler is available, compile a tiny freestanding program for ARM and inspect the object with readelf. 2. Write a CMake toolchain file for a cross target. 3. Explain the difference between bare-metal and Linux cross toolchains.

Deep Challenge

Set up (or describe) a bare-metal ARM cross build with a custom linker script and startup code, from .c source to a .bin firmware image. Explain each step and flag.

References

Verification

verified.`

Progress

Concept checkboxes

Mastery levels

ConceptCurrent level (0–8)Target level
c.build.cross06
c.build.toolchain06