The Complete Guide to Cross-Compiling Userland for Embedded Linux
Cross-compile C, Autotools, CMake, Meson, Go and Rust programs for an embedded Linux board with a toolchain and sysroot, and fix the errors you will hit.

A binary that runs on the machine that built it tells you nothing about whether it will run on the board. Cross-compiling userland is the discipline of producing programs for a processor and a C library you are not sitting at, and almost every failure in it has one of three causes: the compiler targets the wrong architecture, the build links against the host's libraries instead of the target's, or the program runs fine but needs a library or a loader that is not on the board. The cure for all three is the same two ideas, a toolchain you can name and a sysroot you point at. This guide builds up from a single C file to Autotools, CMake and Meson projects, then covers Go and Rust, static linking with musl, finding missing runtime libraries, testing without the board, and debugging remotely. The examples target a 32-bit Arm Cortex-A board running a glibc-based Linux 6.12 system and use GCC 13, with the 64-bit variants shown where they differ. The Yocto SDK and Buildroot appear where each makes a step easier.
The words that cause most of the confusion
Three machines appear in every cross build, and the build tools name them. The build machine is where compilation runs: your laptop. The host machine is where the output will run. The target is only meaningful when you are building a compiler, because it is the machine the compiler's own output will run on. For ordinary programs, build is your workstation and host is the board. That is why Autotools asks for --host= and not --target= when you cross-compile an application, a naming that confuses nearly everyone once.
A target triple names the host side of this: a few fields that say which processor, which vendor and which operating system and ABI. arm-linux-gnueabihf reads as 32-bit Arm, Linux, GNU C library, EABI, hard float. aarch64-linux-gnu is 64-bit Arm with glibc. The triple is also the prefix of every tool in the toolchain, which is how you find them: arm-linux-gnueabihf-gcc, arm-linux-gnueabihf-ld, arm-linux-gnueabihf-strip.
| Triple | Processor | C library | Notes |
|---|---|---|---|
arm-linux-gnueabihf | 32-bit Arm, hard float | glibc | the common Armv7 userland |
arm-linux-gnueabi | 32-bit Arm, soft float | glibc | older boards without an FPU |
aarch64-linux-gnu | 64-bit Arm | glibc | most current Linux boards |
arm-linux-musleabihf | 32-bit Arm, hard float | musl | small static-friendly systems |
riscv64-linux-gnu | 64-bit RISC-V | glibc | RISC-V Linux boards |
A sysroot is a directory that looks like the target's root filesystem for the purposes of building: it holds the C library, its headers, the libraries your program will link against and their headers. When you pass --sysroot=/path, the compiler and linker treat /path as / for header and library searches. A cross toolchain without a sysroot compiles programs that link against the host's libraries, which is the bug this guide is mostly about avoiding.
Get a toolchain you can name
There are five realistic sources, and they differ in how well they match the board's C library.
A distribution package, such as gcc-arm-linux-gnueabihf on Debian and Ubuntu, is the fastest way to start. It targets the distribution's own Arm userland, so it matches a Debian-based board and no one else. Check the libc version before you trust it.
A vendor toolchain, or the Arm GNU Toolchain downloaded as a tarball, is a known good compiler with its own sysroot. It is easy to unpack and pin.
The toolchain Buildroot builds is generated to match the system you build with it: Buildroot compiles the toolchain, then the root filesystem against it, so the sysroot and the board are consistent by construction.
The Yocto SDK, built with bitbake -c populate_sdk, is an installer for a toolchain plus a sysroot generated from the image, which means the headers and libraries you link against are the ones on the board. For application developers on a Yocto product, this is the most reliable option.
crosstool-NG builds a toolchain to your specification, down to the kernel headers and the C library version. Use it when none of the others matches, and expect the build to take a while.
Whichever you choose, record it. Write the compiler version, the triple and the sysroot path into the build documentation, because a toolchain that changes between machines is a program that behaves differently between machines. Ask the compiler:
arm-linux-gnueabihf-gcc -dumpmachine
arm-linux-gnueabihf-gcc --version | head -1
arm-linux-gnueabihf-gcc -print-sysrootarm-linux-gnueabihf
arm-linux-gnueabihf-gcc (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0
The empty line at the end is not a mistake: the Debian and Ubuntu cross compiler is built without a sysroot of its own and prints nothing. A toolchain built with a sysroot, such as one from Buildroot or the Arm GNU Toolchain, prints a path. For the Debian toolchain, the headers and libraries live in the system's /usr/arm-linux-gnueabihf directory, which the compiler finds on its own for the C library.
Your first cross-compiled program
Start with the smallest program that proves the toolchain. It prints a message and the size of a pointer, which is the quickest check of whether the build produced 32-bit code:
#include <stdio.h>
int main(void)
{
printf("hello from an embedded target, pointers are %zu bytes\n", sizeof(void *));
return 0;
}arm-linux-gnueabihf-gcc -Wall -O2 -o hello hello.c
file hellohello: ELF 32-bit LSB pie executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-armhf.so.3, BuildID[sha1]=9d3f..., for GNU/Linux 3.2.0, not strippedRead the output of file as a checklist. ELF 32-bit and ARM confirm the architecture. EABI5 is the ABI, and the interpreter line names the dynamic loader the program will ask for when it starts. If any of those is not what the board has, the program will not run, and file tells you before you copy it anywhere.
Run it on the board:
scp hello [email protected]:/tmp/
ssh [email protected] /tmp/hellohello from an embedded target, pointers are 4 bytesThe pointer size of 4 confirms a 32-bit binary. Build the same file with aarch64-linux-gnu-gcc and the answer is 8.
The error that looks like a missing file
The first failure most people see on a new board is this, and it has nothing to do with a missing file:
-sh: /tmp/hello: not foundThe program exists and the shell says it does not. What is missing is the interpreter named in the ELF header, the dynamic loader /lib/ld-linux-armhf.so.3. If the board's root filesystem uses another loader, for example because it is built with musl, or is a soft-float system with a different loader name, the kernel cannot start the program and the shell reports the problem as the program being "not found". Read the interpreter the binary asks for and the one the board has:
arm-linux-gnueabihf-readelf -l hello | grep interpreter
ssh [email protected] ls /lib/ld-* [Requesting program interpreter: /lib/ld-linux-armhf.so.3]
/lib/ld-linux.so.3Here the board has ld-linux.so.3, which is the soft-float loader, and the binary wants ld-linux-armhf.so.3. The binary and the board disagree about the floating-point ABI, and rebuilding with the right toolchain is the fix. The post on hard and soft float explains what that choice means.
Static or dynamic
A dynamically linked program needs the C library and any other shared libraries on the board, at the right versions. A statically linked program carries them inside the binary and needs only the kernel. For a first test, static linking removes the whole class of missing-library problems:
arm-linux-gnueabihf-gcc -static -o hello-static hello.c
file hello-statichello-static: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), statically linked, BuildID[sha1]=4be1..., for GNU/Linux 3.2.0, not strippedStatic linking against glibc has limits: name service lookups and some locale features load shared objects at run time even in a static binary, and the result is large. It also has a licensing consequence for LGPL libraries, since you distribute the library inside your binary. For a small system, musl is a better static libc than glibc. The post on static binaries with musl covers it. For now, use static as a diagnostic: if the static build runs and the dynamic one does not, the board is missing a library or a loader.
The sysroot, in practice
A real program uses more than the C library, so the compiler must find the headers and libraries of the target, not the host. The sysroot is how. Suppose you have the target's root filesystem unpacked, or an SDK sysroot, at $SYSROOT. Pass it on the command line, and compile a program that uses zlib:
#include <stdio.h>
#include <string.h>
#include <zlib.h>
int main(void)
{
const char *text = "embedded linux embedded linux embedded linux";
unsigned char out[128];
uLongf out_len = sizeof(out);
if (compress(out, &out_len, (const Bytef *)text, strlen(text)) != Z_OK) {
fputs("compress failed\n", stderr);
return 1;
}
printf("%zu bytes in, %lu bytes out\n", strlen(text), (unsigned long)out_len);
return 0;
}export SYSROOT=$HOME/sdk/sysroots/cortexa7t2hf-neon-poky-linux-gnueabi
arm-poky-linux-gnueabi-gcc --sysroot=$SYSROOT -O2 -o compress compress.c -lzThe Yocto SDK's cross compiler is named with the vendor part poky. Without --sysroot, the compiler would look for zlib.h in the host's /usr/include, which would silently pick the x86 headers or fail. With it, both the header and libz come from the target sysroot. Confirm what the linker used:
arm-poky-linux-gnueabi-readelf -d compress | grep NEEDED 0x00000001 (NEEDED) Shared library: [libz.so.1]
0x00000001 (NEEDED) Shared library: [libc.so.6]Those NEEDED entries are the runtime contract. The board must have a libz.so.1 and a libc.so.6 that are compatible with what you linked against, which is the subject of the section on missing libraries below.
You can also ask the compiler where it found things, which settles arguments about which header was used:
arm-poky-linux-gnueabi-gcc --sysroot=$SYSROOT -E -x c - -M <<< '#include <zlib.h>' | head -3-: /home/dev/sdk/sysroots/cortexa7t2hf-neon-poky-linux-gnueabi/usr/include/zlib.h \
/home/dev/sdk/sysroots/cortexa7t2hf-neon-poky-linux-gnueabi/usr/include/zconf.h \The path in the output starts with the sysroot, which is what you want. A path starting /usr/include means the host header was used and the binary is wrong.
The environment script
The Yocto SDK installs a script that sets all of this for you, which is why it is the pleasant route. Source it and the environment variables point at the right compiler and sysroot:
. ~/sdk/environment-setup-cortexa7t2hf-neon-poky-linux-gnueabi
echo $CC
echo $SDKTARGETSYSROOTarm-poky-linux-gnueabi-gcc -mthumb -mfpu=neon -mfloat-abi=hard -mcpu=cortex-a7 --sysroot=/home/dev/sdk/sysroots/cortexa7t2hf-neon-poky-linux-gnueabi
/home/dev/sdk/sysroots/cortexa7t2hf-neon-poky-linux-gnueabiCC is not just a compiler name: it carries the architecture flags and the sysroot, so a Makefile that uses $(CC) is cross-compiled correctly with no further changes. This is why setting CC in the environment is the first thing to do for any build system that respects it.
Make and plain Makefiles
A hand-written Makefile cross-compiles when it uses variables instead of hard-coded gcc. The convention is CC, CFLAGS and LDFLAGS, and a CROSS_COMPILE prefix for the others:
CROSS_COMPILE ?=
CC := $(CROSS_COMPILE)gcc
STRIP := $(CROSS_COMPILE)strip
CFLAGS ?= -O2 -Wall
app: main.o util.o
$(CC) $(LDFLAGS) -o $@ $^
%.o: %.c
$(CC) $(CFLAGS) -c -o $@ $<
install: app
$(STRIP) --strip-unneeded app
install -D -m 0755 app $(DESTDIR)/usr/bin/app
clean:
rm -f app *.omake CROSS_COMPILE=arm-linux-gnueabihf- CFLAGS="-O2 -Wall --sysroot=$SYSROOT" LDFLAGS="--sysroot=$SYSROOT"
make CROSS_COMPILE=arm-linux-gnueabihf- DESTDIR=$PWD/stage installThe DESTDIR variable is a standard: make install puts files under that prefix instead of the host's root, so you can collect the result in a staging directory and copy it to the board, rather than running the Makefile's install against your own /usr/bin. A Makefile whose install target ignores DESTDIR is a bug waiting to overwrite something on the build machine.
Autotools projects
Many C libraries still build with ./configure. Cross-compiling one needs three flags. --host is the triple of the machine that will run the result, --build is the machine you are building on, and --prefix is where the files will live on the target. Pass the sysroot through the usual compiler variables:
tar xf zlib-ng-example.tar.gz && cd zlib-ng-example
./configure --host=arm-linux-gnueabihf --build=$(./config.guess) --prefix=/usr \
CC="arm-linux-gnueabihf-gcc" CFLAGS="-O2 --sysroot=$SYSROOT" LDFLAGS="--sysroot=$SYSROOT"
make -j"$(nproc)"
make DESTDIR=$PWD/stage installchecking build system type... x86_64-pc-linux-gnu
checking host system type... arm-unknown-linux-gnueabihf
checking for arm-linux-gnueabihf-gcc... arm-linux-gnueabihf-gcc
checking whether we are cross compiling... yesThe lines to look for are the last two. checking whether we are cross compiling... yes confirms that configure understood the situation. If it says no, it is about to run test programs on the build machine and conclude things about the wrong architecture.
The classic cross-compile failure with Autotools is a test that has to run a program. A configure check such as "does malloc(0) return non-NULL" compiles and runs a test, which cannot work when the program is for another processor. In a cross build, configure refuses or guesses, and the escape hatch is a cache variable that you set from knowledge of the target:
./configure --host=arm-linux-gnueabihf ac_cv_func_malloc_0_nonnull=yes ac_cv_func_realloc_0_nonnull=yeschecking for GNU libc compatible malloc... (cached) yesThe ac_cv_ prefix marks Autoconf's cache variables, and giving one on the command line skips the check. The right answer depends on the target's C library, so look it up and do not guess. A project that is used often in cross builds can put its answers in a config.site file and point to it with CONFIG_SITE=/path/to/config.site, which makes the answers part of the build environment and not of each command.
The other thing Autotools adds is libtool, which has its own idea of library paths. A project built with libtool may link against .la files that contain the paths of the original build. Pass --with-sysroot to configure for projects that support it, or let your build system rewrite the paths, and check the result with readelf -d for any RPATH that points into your build directory. A leaked RPATH is harmless on the board and embarrassing in a security review.
pkg-config without the host leaking in
Projects find libraries through pkg-config, and by default it reads the host's .pc files. A cross build must point it at the target's. Three variables control this:
| Variable | Purpose |
|---|---|
PKG_CONFIG_LIBDIR | The only directories searched for .pc files, replacing the default |
PKG_CONFIG_PATH | Extra directories searched in addition to the default |
PKG_CONFIG_SYSROOT_DIR | Prefix added to every path the output contains |
For a cross build, use the first and the third, and leave PKG_CONFIG_PATH empty so the host's directories are never searched:
export PKG_CONFIG_LIBDIR=$SYSROOT/usr/lib/pkgconfig:$SYSROOT/usr/share/pkgconfig
export PKG_CONFIG_SYSROOT_DIR=$SYSROOT
pkg-config --cflags --libs zlib-I/home/dev/sdk/sysroots/cortexa7t2hf-neon-poky-linux-gnueabi/usr/include -lzWithout PKG_CONFIG_SYSROOT_DIR, the same command prints -I/usr/include, which would make the compiler read the host's headers. The sysroot-prefixed path is the sign that it works.
CMake projects
CMake takes its cross-compilation setup from a toolchain file, a small CMake script that says what the target is and where to look. Write it once, and every project uses it:
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR arm)
set(CMAKE_C_COMPILER arm-linux-gnueabihf-gcc)
set(CMAKE_CXX_COMPILER arm-linux-gnueabihf-g++)
set(CMAKE_SYSROOT $ENV{SYSROOT})
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)The last four lines are the important ones. They control where find_program, find_library, find_path and find_package search. Programs, such as python3 or a code generator, must come from the host, because they run during the build, so NEVER restricts the search away from the sysroot. Libraries, headers and packages must come only from the sysroot, so those use ONLY. Getting this backwards is how a build ends up linking a host library into an Arm binary.
cmake -S . -B build -DCMAKE_TOOLCHAIN_FILE=$PWD/arm-toolchain.cmake -DCMAKE_BUILD_TYPE=Release
cmake --build build -j"$(nproc)"
file build/myapp-- The C compiler identification is GNU 13.3.0
-- Detecting C compiler ABI info - done
build/myapp: ELF 32-bit LSB pie executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-armhf.so.3, ...CMake can also run test programs through an emulator when you set CMAKE_CROSSCOMPILING_EMULATOR in the toolchain file, for instance to qemu-arm;-L;${CMAKE_SYSROOT}. Then ctest runs the target binaries under user-mode emulation on your workstation, which is a cheap way to run a unit test suite for a cross build. The Yocto SDK ships a ready toolchain file, and the post on using it covers that case.
Meson projects
Meson describes the target in a cross file, an INI-style file with sections for binaries, properties and the host machine:
[binaries]
c = 'arm-linux-gnueabihf-gcc'
cpp = 'arm-linux-gnueabihf-g++'
ar = 'arm-linux-gnueabihf-ar'
strip = 'arm-linux-gnueabihf-strip'
pkg-config = 'pkg-config'
[properties]
sys_root = '/home/dev/sdk/sysroots/cortexa7t2hf-neon-poky-linux-gnueabi'
[host_machine]
system = 'linux'
cpu_family = 'arm'
cpu = 'cortex-a7'
endian = 'little'meson setup build-arm --cross-file cross-arm.ini
meson compile -C build-armThe Meson build system
Build type: cross build
Host machine cpu family: arm
Host machine cpu: cortex-a7Build type: cross build is the line that tells you Meson understood. The sys_root property lets Meson pass the sysroot to the compiler and to pkg-config. The post on Meson cross files gives the details, including the extra fields for passing flags.
Go and Rust
Go cross-compiles with two environment variables and no toolchain, because the Go compiler is its own cross compiler. For a pure Go program:
GOOS=linux GOARCH=arm GOARM=7 CGO_ENABLED=0 go build -o myservice-arm .
GOOS=linux GOARCH=arm64 CGO_ENABLED=0 go build -o myservice-arm64 .
file myservice-armmyservice-arm: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), statically linked, Go BuildID=..., not strippedGOARM=7 selects the Armv7 instruction set, and CGO_ENABLED=0 makes the binary pure Go and statically linked, which is why it runs on a board with no C library at all. If your program uses cgo, you need a C cross compiler and the sysroot after all, and the post on cgo cross builds covers it.
Rust needs the target's standard library installed and a linker that knows the target. Add the target once, then name the linker in the Cargo configuration:
rustup target add armv7-unknown-linux-gnueabihf[target.armv7-unknown-linux-gnueabihf]
linker = "arm-linux-gnueabihf-gcc"cargo build --release --target armv7-unknown-linux-gnueabihf
file target/armv7-unknown-linux-gnueabihf/release/myapptarget/armv7-unknown-linux-gnueabihf/release/myapp: ELF 32-bit LSB pie executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-armhf.so.3, ...Without the linker line, Cargo calls the host's cc, which links Arm object code with the x86 linker and fails with errors about file format. The C cross compiler is used as the linker driver, so the Rust build uses the same sysroot and libc as the rest of your system.
When the program runs, but a library is missing
A binary can build and copy and still fail on the board with a message about a shared library:
./compress: error while loading shared libraries: libz.so.1: cannot open shared object file: No such file or directoryThe remedy starts on the build machine. The NEEDED entries of the binary list what it requires, and the same for each library it pulls in:
arm-linux-gnueabihf-readelf -d compress | grep -E 'NEEDED|RPATH|RUNPATH' 0x00000001 (NEEDED) Shared library: [libz.so.1]
0x00000001 (NEEDED) Shared library: [libc.so.6]Compare against what the board holds. Run ls /lib /usr/lib on the board, or check its package list. ldd does not work on a foreign architecture binary on your workstation, because it works by running the dynamic loader, so use readelf for the first level. For the full tree, the lddtree script from the pax-utils package reads the ELF headers without running anything.
The second library failure is a version one:
./app: /lib/libc.so.6: version `GLIBC_2.38' not found (required by ./app)The program was linked against a newer glibc than the one on the board. Symbols have versions, and the binary records the newest version it uses. The fix is to build against a sysroot that matches the board, which is what the SDK and a Buildroot toolchain give you, not a distribution cross compiler that is newer than the board's libc. You can list the versions a binary requires:
arm-linux-gnueabihf-objdump -T app | grep -o 'GLIBC_[0-9.]*' | sort -uV | tail -3GLIBC_2.4
GLIBC_2.34
GLIBC_2.38The highest line is the minimum glibc the board must have. If the board has glibc 2.35 and the binary needs 2.38, the toolchain is too new.
Test without the board
User-mode QEMU runs a foreign-architecture Linux binary on your workstation by translating its instructions and forwarding its system calls to your kernel. It is not a full emulator and needs no board, only the target's libraries to load. Give it the sysroot with -L:
sudo apt install qemu-user
qemu-arm -L $SYSROOT ./compress44 bytes in, 17 bytes outThe byte counts are what the example's input compresses to with zlib's default level. The same program prints that on the board and under emulation, which is the point: a unit test suite can run on a build server with no hardware. The limits are real. User-mode emulation does not model your peripherals, interrupts or timing, so it proves the code is correct and says nothing about the performance or the hardware access. The post on qemu-user and binfmt_misc shows how to run foreign binaries transparently without the explicit qemu-arm command.
Shrink and package what you ship
A fresh build includes debug symbols that the board never needs. Strip before you deploy, and keep the unstripped binary for debugging:
cp compress compress.debug
arm-linux-gnueabihf-strip --strip-unneeded compress
ls -l compress.debug compress | awk '{print $5, $9}'17984 compress.debug
6008 compressThe sizes are an example. Keep compress.debug next to the release build, because it is the file a debugger or a crash analysis tool needs. A stripped binary and its matching debug file are paired by the build ID, and a mismatched pair gives you wrong line numbers.
Collect the files for the board with DESTDIR into a staging tree that mirrors the target's filesystem, then copy that tree. For a product, turn the staging tree into a package or an image layer, so a deployment is a versioned artifact and not a set of scp commands.
Debug on the board from your workstation
When a cross-compiled program crashes on the board, debug it from the workstation. gdbserver runs on the board next to the program, and a GDB on your workstation attaches over the network. The workstation GDB must understand the target architecture, which is why the cross toolchains ship one named arm-linux-gnueabihf-gdb, or you can use gdb-multiarch:
## on the board
gdbserver :2345 /tmp/compressProcess /tmp/compress created; pid = 612
Listening on port 2345## on the workstation
gdb-multiarch compress.debug(gdb) set sysroot /home/dev/sdk/sysroots/cortexa7t2hf-neon-poky-linux-gnueabi
(gdb) target remote 192.168.1.50:2345
(gdb) break main
(gdb) continueTwo settings make this work. set sysroot tells GDB where to read the target's shared libraries from, so it can show symbols for libc and libz, which are stripped on the board. And loading compress.debug, the unstripped file, gives GDB the symbols of your own program while the board runs the stripped one. A backtrace with ?? frames means one of those two is missing.
Getting the matching toolchain, three ways
The advice to use a toolchain that matches the board is easier to follow when you see what the commands are. Each route below produces a compiler plus a sysroot that agree with the system on the board.
From Yocto, build the SDK for the image you ship. The installer is a self-extracting shell script that unpacks a toolchain and a sysroot generated from the image's package list, so the sysroot contains exactly the libraries the image has:
MACHINE=myboard bitbake core-image-minimal -c populate_sdk
ls tmp/deploy/sdk/poky-glibc-x86_64-core-image-minimal-cortexa7t2hf-neon-myboard-toolchain-5.0.8.sh
poky-glibc-x86_64-core-image-minimal-cortexa7t2hf-neon-myboard-toolchain-5.0.8.host.manifest
poky-glibc-x86_64-core-image-minimal-cortexa7t2hf-neon-myboard-toolchain-5.0.8.target.manifest
poky-glibc-x86_64-core-image-minimal-cortexa7t2hf-neon-myboard-toolchain-5.0.8.testdata.jsonThe .target.manifest file lists every package in the sysroot, which is the record of what you can link against. A library missing from that list is not in the SDK, and the fix is to add it to the image's TOOLCHAIN_TARGET_TASK or the image itself, then rebuild the SDK. Install it with the script:
./poky-glibc-x86_64-core-image-minimal-cortexa7t2hf-neon-myboard-toolchain-5.0.8.sh -d ~/sdk -y
. ~/sdk/environment-setup-cortexa7t2hf-neon-poky-linux-gnueabiFrom Buildroot, the toolchain is built as part of the system, and an SDK tarball can be produced from it. The result is relocatable, so it can be unpacked anywhere:
make sdk
ls output/images/*sdk*output/images/arm-buildroot-linux-gnueabihf_sdk-buildroot.tar.gzUnpack it and run the relocate-sdk.sh script that it includes once, so the paths inside are fixed up for the new location. The compiler in that tarball uses the same C library as the root filesystem Buildroot built, which is the match you want.
From crosstool-NG, you describe the toolchain in a configuration and build it. For an Armv7 hard-float glibc toolchain the sample is named after the triple:
ct-ng arm-cortexa9_neon-linux-gnueabihf
ct-ng menuconfig
ct-ng buildThe sample name shown is one of the samples listed by ct-ng list-samples; pick the nearest one and adjust the kernel headers version and the glibc version to your board. The resulting toolchain lands under ~/x-tools by default. Set the C library and the kernel header versions no newer than what the board runs, since the kernel headers decide which system calls the library will try.
A project with a library and an application
Single-file examples hide the interactions that cause the trouble. This CMake project has a library, an application that links it, and a dependency on a system library found through pkg-config. It is small enough to read and shaped like real embedded userland.
sensor/
CMakeLists.txt
include/sensor.h
src/sensor.c
app/main.ccmake_minimum_required(VERSION 3.20)
project(sensor C)
find_package(PkgConfig REQUIRED)
pkg_check_modules(ZLIB REQUIRED zlib)
add_library(sensor src/sensor.c)
target_include_directories(sensor PUBLIC include)
target_link_libraries(sensor PUBLIC ${ZLIB_LIBRARIES})
target_include_directories(sensor PRIVATE ${ZLIB_INCLUDE_DIRS})
add_executable(sensord app/main.c)
target_link_libraries(sensord PRIVATE sensor)
install(TARGETS sensord sensor RUNTIME DESTINATION bin LIBRARY DESTINATION lib ARCHIVE DESTINATION lib)Configure it with the toolchain file from earlier and the pkg-config variables set, then build and install into a staging directory:
export PKG_CONFIG_LIBDIR=$SYSROOT/usr/lib/pkgconfig
export PKG_CONFIG_SYSROOT_DIR=$SYSROOT
cmake -S sensor -B build -DCMAKE_TOOLCHAIN_FILE=$PWD/arm-toolchain.cmake \
-DCMAKE_INSTALL_PREFIX=/usr
cmake --build build
DESTDIR=$PWD/stage cmake --install build-- Checking for module 'zlib'
-- Found zlib, version 1.3.1
-- Install configuration: ""
-- Installing: /home/dev/stage/usr/bin/sensord
-- Installing: /home/dev/stage/usr/lib/libsensor.aThe staging tree now holds usr/bin/sensord and the library, laid out as they will be on the board. The Found zlib, version 1.3.1 line is the version in the sysroot, not on your workstation, which is the proof that pkg-config is reading the right place. Check one more thing: the installed binary must not reference your build directory.
arm-linux-gnueabihf-readelf -d stage/usr/bin/sensord | grep -E 'RPATH|RUNPATH' || echo "no rpath"no rpathCMake sets a build-tree RPATH for executables while it builds and strips it at install time, so the staged binary is clean. A RUNPATH that names /home/dev/... after install means something overrode that, and it should be fixed before the binary ships.
Flags that matter on Arm
A cross compiler's defaults come from how it was configured, and they may not be your board's. Check what the compiler assumes and what your CPU needs, because a mismatch gives either slow code or an illegal instruction crash.
arm-linux-gnueabihf-gcc -Q --help=target -O2 | grep -E 'march|mcpu|mfpu|mfloat-abi' | head -march= armv7-a+fp
-mcpu=
-mfloat-abi= hard
-mfpu= vfpv3-d16The default here is armv7-a with a basic floating-point unit. A Cortex-A7 board with NEON can use more, so the build adds -mcpu=cortex-a7 -mfpu=neon-vfpv4. Do not turn on instruction sets the CPU lacks. If a binary dies with Illegal instruction, the first check is whether the flags assume a newer CPU than the board has, and grep -m1 Features /proc/cpuinfo on the board lists what it supports.
| Flag | Effect | Risk |
|---|---|---|
-mcpu=cortex-a7 | Tune and select the instruction set for this core | none if it matches the board |
-mfpu=neon-vfpv4 | Allow NEON and VFPv4 instructions | Illegal instruction on a core without them |
-mfloat-abi=hard | Pass floating-point values in FPU registers | link errors against soft-float libraries |
-mthumb | Generate Thumb-2 code, which is denser | none on Armv7 |
-fstack-protector-strong | Stack canaries on risky functions | small size and speed cost |
-D_FORTIFY_SOURCE=2 | Checked versions of common libc calls | needs -O1 or higher |
Mixing a hard-float object with a soft-float library fails at link time with uses VFP register arguments, ... does not and that message is the floating-point ABI mismatch. The cure is always to use one ABI throughout, which a single toolchain and sysroot guarantee.
For hardening a shipped binary, ask the linker for the same protections your distribution uses: -Wl,-z,relro,-z,now for full RELRO, and -pie with -fPIE for position independence. A check on the result prints the properties:
arm-linux-gnueabihf-readelf -lW sensord | grep -E 'GNU_RELRO|GNU_STACK' GNU_STACK 0x000000 0x00000000 0x00000000 0x00000 0x00000 RW 0x10
GNU_RELRO 0x000ee8 0x00010ee8 0x00010ee8 0x00118 0x00118 R 0x1GNU_STACK with flags RW and no E means the stack is not executable, and the presence of GNU_RELRO means part of the data is made read-only after relocation. Those are the two lines a security review will ask about.
Build a missing dependency into a staging sysroot
Sooner or later your program needs a library that the SDK does not have. The tempting fix is to install it into the SDK's sysroot, which changes a directory that the next SDK install silently overwrites. A better habit keeps your own libraries in a separate staging prefix that you add to the search paths, so the SDK stays pristine and your additions are a build artifact you can recreate.
The pattern for an Autotools library is the same as for an application, with two differences. The --prefix is the path the library will have on the board, and DESTDIR is the staging directory, so you install into the staging tree and later copy it to both the build sysroot and the board:
export STAGE=$HOME/stage
cd libexample-2.4
./configure --host=arm-linux-gnueabihf --build=$(./config.guess) --prefix=/usr \
CC="arm-linux-gnueabihf-gcc --sysroot=$SYSROOT" --disable-static
make -j"$(nproc)"
make DESTDIR=$STAGE install
find $STAGE -name '*.pc' -o -name '*.so*' | sort/home/dev/stage/usr/lib/libexample.so
/home/dev/stage/usr/lib/libexample.so.2
/home/dev/stage/usr/lib/libexample.so.2.4.0
/home/dev/stage/usr/lib/pkgconfig/example.pcThe library is installed with its .pc file, which is how applications find it. Extend the pkg-config search path to include the staging tree, and set the sysroot variable to the same place your applications will compile against, using a merged directory of the SDK sysroot and the staging tree if you want one prefix:
export PKG_CONFIG_LIBDIR=$STAGE/usr/lib/pkgconfig:$SYSROOT/usr/lib/pkgconfig
export PKG_CONFIG_SYSROOT_DIR=$STAGE
pkg-config --cflags --libs example-I/home/dev/stage/usr/include/example -L/home/dev/stage/usr/lib -lexampleNotice the limit of PKG_CONFIG_SYSROOT_DIR: it accepts a single prefix, so a library in the SDK and another in the staging tree cannot both be rewritten correctly by the variable alone. The reliable solution is to make the staging tree a complete overlay, by copying the SDK sysroot into a working directory and installing your libraries into it. The overlay then has one prefix, one pkg-config setting and one --sysroot value. Another is to give your libraries a .pc file that already contains absolute paths, but that breaks when the directory moves.
When the library is a shared object, the board needs it too. Copy libexample.so.2 and its symlinks to /usr/lib on the target, and run ldconfig if the target has it, or place the files in a directory the loader already searches. A shared library on the build machine that is not on the board is the commonest reason a program runs under the SDK's emulator and not on the hardware.
Use a container for the toolchain
A cross toolchain pinned in a container image gives every developer and the CI server the same compiler, sysroot and tools. The Dockerfile is short and holds the decisions that otherwise live in someone's shell history:
FROM ubuntu:24.04
RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential cmake ninja-build meson pkg-config \
gcc-arm-linux-gnueabihf g++-arm-linux-gnueabihf \
qemu-user gdb-multiarch ca-certificates \
&& rm -rf /var/lib/apt/lists/*
COPY sysroot /opt/sysroot
ENV SYSROOT=/opt/sysroot \
CROSS_COMPILE=arm-linux-gnueabihf- \
PKG_CONFIG_LIBDIR=/opt/sysroot/usr/lib/pkgconfig \
PKG_CONFIG_SYSROOT_DIR=/opt/sysroot
WORKDIR /srcThe sysroot directory in the build context is a copy of the target's root filesystem or the SDK's sysroot, committed or fetched at a pinned version. Builds then run as docker run --rm -v "$PWD":/src image make, and nothing about the host matters. Tag the image with the SDK version so a build can name exactly which toolchain produced a binary. A container image is also the cleanest way to hand the toolchain to a contractor, since the instructions fit on one line.
Two cautions apply. A container shares the host kernel, so qemu-user inside it needs the host's binfmt_misc registration to run foreign binaries transparently, and without it you call qemu-arm explicitly. And file ownership: files the container creates belong to the user it runs as, so run it with --user "$(id -u):$(id -g)" to avoid root-owned build output.
A pre-deployment check you can script
Every inspection above takes seconds, which makes them worth automating. A short script that runs on the build machine before a binary is copied to the board catches wrong architectures, wrong interpreters and leaked build paths, and it fails the CI job with a message a person can act on. It needs only readelf from the cross toolchain:
#!/bin/sh
set -eu
BIN="$1"
RE="${CROSS_COMPILE:-arm-linux-gnueabihf-}readelf"
WANT_MACHINE="ARM"
WANT_INTERP="/lib/ld-linux-armhf.so.3"
machine=$($RE -h "$BIN" | awk -F: '/Machine:/ {gsub(/^ +/, "", $2); print $2}')
interp=$($RE -l "$BIN" | sed -n 's/.*Requesting program interpreter: \(.*\)\]/\1/p')
[ "$machine" = "$WANT_MACHINE" ] || { echo "FAIL machine is $machine"; exit 1; }
[ -z "$interp" ] || [ "$interp" = "$WANT_INTERP" ] || { echo "FAIL interpreter is $interp"; exit 1; }
if $RE -d "$BIN" | grep -E 'RPATH|RUNPATH' | grep -q '/home/'; then
echo "FAIL build path leaked into RPATH"; exit 1
fi
echo "ok $BIN machine=$machine interpreter=${interp:-static}"sh check-binary.sh stage/usr/bin/sensordok stage/usr/bin/sensord machine=ARM interpreter=/lib/ld-linux-armhf.so.3The script accepts a static binary, which has no interpreter, and refuses one built for another processor. Extend it for your product: add a check that the highest GLIBC_ version in objdump -T is no newer than the board's, and that the file is stripped. A script like this turns a recurring review comment into a build failure, which is a cheaper place to find it.
Make the build repeatable
A cross build depends on more state than a native one, and any of it can drift. Keep the following under version control or in a pinned container: the toolchain version and its download checksum, the sysroot contents (or the SDK installer's checksum), the exact flags in the toolchain or cross file, and the versions of every third-party library you compile. A CI job that installs the SDK from a checksummed installer and builds from a clean directory is a few lines and removes the "works on my machine" conversation.
For Yocto products, the SDK is a build artifact like any other, produced from the same recipes as the image, so a new SDK follows every image change. For Buildroot, make sdk produces a relocatable toolchain and sysroot tarball for the same purpose. The principle is the same in both: the application developers use the toolchain and sysroot the system build produced, so the libraries match the board.
Failures and their first checks
The recurring errors are few, and each has a first check:
| Symptom | First check |
|---|---|
file says x86-64 | Is CC set, or CROSS_COMPILE exported? |
not found on the board for an existing file | readelf -l for the interpreter, compare with ls /lib/ld-* |
cannot open shared object file | readelf -d NEEDED against the board's libraries |
version GLIBC_x not found | Toolchain libc newer than the board's, use the matching SDK |
Header from /usr/include in the dependency list | Missing --sysroot or wrong PKG_CONFIG_* |
| configure tries to run a test binary | --host missing, or an ac_cv_ answer needed |
Rust file format not recognized while linking | Missing linker for the target in .cargo/config.toml |
| Core dump on the board, no symbols | Keep the unstripped binary, use set sysroot |
None of these needs the board to diagnose. Almost every one is answered by file, readelf or the dependency output on the workstation, which is why the first rule of cross-compiling is to inspect the binary before you copy it.
When this guide does not fit
Some readers need a different approach.
You develop on the board itself. A board with enough memory and storage can compile natively, and for a small tool that is simpler than any of this. Native builds on a slow Arm core are slow, and they do not scale to a CI pipeline, but they remove every cross-compilation problem at once.
You build entire images, not applications. If you are assembling a root filesystem, a build system such as Yocto or Buildroot already cross-compiles every package for you with the right sysroot. Use their recipes and packages, and use the SDK for your own application on top.
You target a microcontroller with no operating system. The bare-metal toolchains use triples like arm-none-eabi and have no sysroot or C library of the kind described here. The ideas about triples and ABIs carry over, but the build steps do not.
You work on a distribution-based board. For a Debian or Ubuntu system on the board, the distribution's own multiarch tooling or an emulated native build in a container may be less work than assembling a sysroot by hand.
In each case the habit of inspecting the output with file and readelf still saves time. The toolchain details differ, and the failure modes are close relatives.
The primary sources for this post are the GCC directory options and Yocto SDK usage guide.


