Lesson 1 of 9 · 24m
Cross-Compilation Without Cargo-Cult Flags
Understand build, host, and target machines; how sysroots and toolchains fit together; and why most embedded build failures are context failures.
The first embedded build I inherited failed only on Thursdays, because one engineer's shell exported CC from a desktop toolchain and another's did not. Nothing was wrong with the compiler. The system had no agreed answer to the basic cross-compilation question: which machine is this binary supposed to run on?
Build, host, and target are different roles
Autotools terminology is old but still the clearest model:
build the machine compiling the software
host the machine where the compiled software will run
target the machine a compiler or debugger generates code forFor a normal application cross-compile, build is your x86-64 workstation, host is the embedded ARM board, and target is irrelevant unless you are building compiler tooling. People conflate host and target; when configure output says one thing and intuition says another, read the terminology literally.
A minimal cross-compile looks like this:
arm-linux-gnueabihf-gcc -O2 -Wall --sysroot=/opt/sysroots/imx6ull -o sensorctl sensorctl.c -lgpiodThe compiler prefix is not decorative. It encodes architecture, operating system, libc flavor, and floating-point ABI. The wrong one produces binaries that fail before main() ever runs.
The sysroot is the target's worldview
A sysroot is a partial copy of the target filesystem containing headers and libraries: usr/include/gpiod.h, usr/lib/libgpiod.so.2, usr/lib/libc.so.6, and the dynamic loader. If a build links against /usr/lib/x86_64-linux-gnu/libssl.so, you are not cross-compiling that dependency; you are mixing worlds.
Inspect the result before blaming the board:
file sensorctl
readelf -l sensorctl | grep interpreter
arm-linux-gnueabihf-readelf -d sensorctl | grep NEEDEDThe classic symptom is No such file or directory even though the binary visibly exists. Often the file exists but its ELF interpreter, such as /lib/ld-linux-armhf.so.3, does not.
Kernel builds are cross-builds too
Linux uses ARCH and CROSS_COMPILE rather than a compiler command baked into the Makefile:
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- imx_v7_defconfig
make -j8 ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- zImage dtbs modulesCROSS_COMPILE ends with a hyphen because it is a prefix prepended to gcc, ld, and objcopy. A missing trailing hyphen makes the build look for a command literally named arm-linux-gnueabihfgcc.
Field notes
- Most failures are context failures: host tools leaking into target builds, stale objects from another architecture, or hard-coded
-Iand-Lpaths bypassing the sysroot. - Never fix a missing target library by copying your workstation
.soonto the board. - Use verbose build output (
make V=1,ninja -v) to see the actual compiler and library paths. - Rebuild clean after changing toolchains; dependency caches and object files do not know the world changed.
Was this lesson helpful?