Cross-Compile a C Program for ARM64 and Run It with QEMU User-Mode
Build a C program for 64-bit ARM (aarch64) on an x86 Debian host with a GCC cross toolchain, then check the binary by running it under qemu-aarch64.
You can build a C program for a 64-bit ARM (arm64, also called aarch64) machine on an ordinary x86 Debian host and run it without any ARM hardware. A GCC cross-compiler produces the binary, and QEMU’s user-mode emulation executes it on your own operating system.
This is the 64-bit counterpart of the 32-bit ARM guide.
Install the toolchain and emulator
Debian describes gcc-aarch64-linux-gnu as a dependency package providing the default GNU C cross-compiler for arm64. It recommends libc6-dev-arm64-cross, the C library headers and libraries for cross-compiling, so install that explicitly. The qemu-user package is described by Debian as user-mode emulation with static binaries.
apt-get update
apt-get install -y gcc-aarch64-linux-gnu libc6-dev-arm64-cross qemu-user fileWrite and cross-compile an ARM64 program
This program prints a line and the size of a pointer, which differs between 32-bit and 64-bit targets:
cat > /tmp/hello64.c <<'EOF'
#include <stdio.h>
int main(void)
{
printf("Hello from ARM64, pointer size: %zu bytes\n", sizeof(void *));
return 0;
}
EOFThe cross-compiler uses the target prefix aarch64-linux-gnu- instead of plain gcc. Linking statically means the binary needs no ARM64 shared libraries at run time:
aarch64-linux-gnu-gcc -static -o /tmp/hello64 /tmp/hello64.c
file /tmp/hello64The file command reports the type of the binary. An x86 CPU cannot run an ARM64 binary directly, so the next step uses an emulator.
Run it with qemu-aarch64
QEMU’s Linux user-mode emulators are named qemu-<arch>. The user-mode documentation page I used lists qemu-arm but does not list an aarch64 entry, so I did not rely on it for the name. The 64-bit ARM emulator in Debian’s qemu-user package is qemu-aarch64, and the test run for this article confirmed it. Usage is the emulator followed by the program:
qemu-aarch64 /tmp/hello64Hello from ARM64, pointer size: 8 bytesThe pointer size of 8 bytes confirms a 64-bit build.
Dynamically linked binaries
A normal dynamically linked binary asks for an ARM64 dynamic loader, which an x86 root file system does not have. QEMU’s -L path option sets an ELF interpreter prefix (the documentation describes it for x86 as the “x86 elf interpreter prefix”). On Debian, the commands below work when -L points at /usr/aarch64-linux-gnu, as the test run for this article confirmed:
aarch64-linux-gnu-gcc -o /tmp/hello64-dyn /tmp/hello64.c
qemu-aarch64 -L /usr/aarch64-linux-gnu /tmp/hello64-dynHello from ARM64, pointer size: 8 bytesLimits to keep in mind
QEMU’s documentation describes user-mode emulation as launching programs built for one CPU architecture on the same OS, so it is not a way to test drivers, boot code or device trees. For those, use full-system emulation, as in the Buildroot QEMU guide or the initramfs QEMU guide, or real hardware.
Sources
This article was drafted with AI assistance, fact-checked against the sources listed above and reviewed by a human editor before publication. Spotted an error? See our corrections policy.