| name | cross-compilation |
| description | Cross-compile programs for ARM architecture from x86 hosts. Use when building ARM binaries, compiling kernels for ARM targets, or setting up cross-compilation toolchains. |
Cross-Compilation for ARM
Build ARM binaries on x86 systems using cross-compilation toolchains.
Toolchain Setup
Install ARM cross-compiler:
# Debian/Ubuntu
apt-get install gcc-arm-linux-gnueabihf g++-arm-linux-gnueabihf
# For bare-metal ARM
apt-get install gcc-arm-none-eabi
Basic Compilation
# Compile C program for ARM Linux
arm-linux-gnueabihf-gcc -o hello hello.c
# Compile with static linking
arm-linux-gnueabihf-gcc -static -o hello hello.c
# Bare-metal compilation
arm-none-eabi-gcc -mcpu=cortex-a9 -o program.elf program.c
Cross-Compilation Variables
export CROSS_COMPILE=arm-linux-gnueabihf-
export ARCH=arm
export CC=${CROSS_COMPILE}gcc
export CXX=${CROSS_COMPILE}g++
Kernel Cross-Compilation
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- defconfig
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- zImage -j$(nproc)
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- modules
CMake Cross-Compilation
Create toolchain file arm-toolchain.cmake:
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_FIND_ROOT_PATH /usr/arm-linux-gnueabihf)
Build with:
cmake -DCMAKE_TOOLCHAIN_FILE=arm-toolchain.cmake ..
make
Verify Binary Architecture
file hello
# Output: hello: ELF 32-bit LSB executable, ARM, EABI5...
arm-linux-gnueabihf-readelf -h hello | grep Machine
# Output: Machine: ARM