Cross-Compile a Hello World for ARM and Run It with QEMU User-Mode

Build a small C program for 32-bit ARM on an x86 Debian or Ubuntu machine, then run it with QEMU user-mode emulation instead of a full ARM system.

Embedded Published 3 min read

You can cross-compile a program for an ARM processor on an ordinary x86 PC and run it right away, without an ARM board or a full virtual machine. A cross-compiler produces the ARM binary, and QEMU’s user-mode emulation executes it, passing the program’s system calls to your own kernel.

This guide uses 32-bit ARM with hard-float (armhf) on Debian or Ubuntu.

Install the tools

Debian describes gcc-arm-linux-gnueabihf as a dependency package providing the default GNU C cross-compiler for armhf. It recommends libc6-dev-armhf-cross for the target C library headers on non-ARM systems, so install that explicitly. The qemu-user package provides the user-mode emulators; Debian describes it as static binaries.

✓ tested · debian,ubuntu
apt-get update
apt-get install -y gcc-arm-linux-gnueabihf libc6-dev-armhf-cross qemu-user file

Write and cross-compile the program

Create a minimal C file:

✓ tested · debian,ubuntu
cat > /tmp/hello.c <<'EOF'
#include <stdio.h>

int main(void)
{
    puts("Hello from ARM");
    return 0;
}
EOF

The compiler is invoked with a target prefix, arm-linux-gnueabihf-gcc, instead of plain gcc:

✓ tested · debian,ubuntu
arm-linux-gnueabihf-gcc -static -o /tmp/hello-arm /tmp/hello.c
file /tmp/hello-arm

The file output shows what kind of binary was produced. The x86 CPU cannot execute ARM instructions, which is why the next step uses an emulator.

Run it with qemu-arm

QEMU’s user-mode documentation lists qemu-arm as the emulator for Arm binaries. The usage is the emulator name followed by the program and its arguments:

✓ tested · debian,ubuntu
qemu-arm /tmp/hello-arm
Hello from ARM

Because the binary is static, it needs no ARM shared libraries at run time, so nothing else has to be set up.

Dynamically linked binaries

Static linking is the simplest route, but you can also build a normal dynamically linked binary. Such a binary asks for an ARM dynamic loader, which does not exist on an x86 root file system. The -L path option of QEMU’s Linux user-mode emulators sets the prefix used to look up the target’s ELF interpreter. On Debian and Ubuntu, the ARM libraries installed by the cross packages live under /usr/arm-linux-gnueabihf; the check below confirms that before you rely on it.

✓ tested · debian,ubuntu
ls /usr/arm-linux-gnueabihf/lib/ld-linux-armhf.so.3
arm-linux-gnueabihf-gcc -o /tmp/hello-arm-dyn /tmp/hello.c
qemu-arm -L /usr/arm-linux-gnueabihf /tmp/hello-arm-dyn
Hello from ARM

Limits to keep in mind

User-mode emulation runs one program, not an operating system. It does not model a board, a kernel, or hardware peripherals, so it is unsuitable for testing drivers, boot code, or device trees. For those you need full-system emulation or real hardware. It is, however, a quick way to check that code compiles and behaves correctly for another architecture.

For other targets, QEMU’s documentation lists emulators such as qemu-mips, qemu-ppc, and qemu-m68k; you would pair each with the matching cross-compiler.

Sources

  1. QEMU documentation: User Mode Emulation
  2. Debian package gcc-arm-linux-gnueabihf (trixie)
  3. Debian package qemu-user (trixie)

#Arm #Cross-Compilation #Qemu #Gcc #Embedded

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.