Loading...
Loading...
Cross-compilation with GCC skill for embedded and multi-architecture targets. Use when setting up cross-gcc toolchains, configuring sysroots, building for ARM/AArch64/RISC-V/MIPS from an x86-64 host, troubleshooting wrong-architecture errors, or running cross-compiled binaries under QEMU. Activates on queries about cross-compilation triplets, sysroot, pkg-config for cross builds, embedded toolchains, or Yocto/Buildroot integration.
npx skill4agent add mohitmishra786/low-level-dev-skills cross-gcc<arch>-<vendor>-<os>-<abi>| Triplet | Target |
|---|---|
| 64-bit ARM Linux (glibc) |
| 32-bit ARM Linux hard-float |
| Bare-metal ARM (no OS) |
| 64-bit RISC-V Linux |
| Windows (MinGW) from Linux |
| Little-endian MIPS Linux |
# Debian/Ubuntu
sudo apt install gcc-aarch64-linux-gnu g++-aarch64-linux-gnu binutils-aarch64-linux-gnu
# For bare-metal ARM (Cortex-M)
sudo apt install gcc-arm-none-eabi binutils-arm-none-eabi
# Verify
aarch64-linux-gnu-gcc --version# C
aarch64-linux-gnu-gcc -O2 -o hello hello.c
# C++
aarch64-linux-gnu-g++ -O2 -std=c++17 -o hello hello.cpp
# Bare-metal (no stdlib, no OS)
arm-none-eabi-gcc -mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16 \
-ffreestanding -nostdlib -T linker.ld -o firmware.elf startup.s main.c# Use a sysroot
aarch64-linux-gnu-gcc --sysroot=/path/to/aarch64-sysroot -O2 -o prog main.c
# Common sysroot sources:
# - Raspberry Pi: download from raspbian/raspios
# - Debian multiarch: debootstrap --arch arm64 bullseye /tmp/sysroot
# - Yocto/Buildroot: generated automatically in build outputaarch64-linux-gnu-gcc --sysroot=/path/to/sysroot -v -E - < /dev/null 2>&1 | grep sysrootpkg-configexport PKG_CONFIG_SYSROOT_DIR=/path/to/sysroot
export PKG_CONFIG_LIBDIR=${PKG_CONFIG_SYSROOT_DIR}/usr/lib/aarch64-linux-gnu/pkgconfig:${PKG_CONFIG_SYSROOT_DIR}/usr/share/pkgconfig
export PKG_CONFIG_PATH= # clear host path
pkg-config --libs libssl # now returns target pathsaarch64.cmakeset(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR aarch64)
set(CMAKE_C_COMPILER aarch64-linux-gnu-gcc)
set(CMAKE_CXX_COMPILER aarch64-linux-gnu-g++)
set(CMAKE_SYSROOT /path/to/aarch64-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)cmake -S . -B build -DCMAKE_TOOLCHAIN_FILE=aarch64.cmake
cmake --build build# User-mode emulation (Linux binaries, no full OS)
sudo apt install qemu-user-static
qemu-aarch64-static ./hello
# Or set binfmt_misc for transparent execution:
# Then just: ./hello
# GDB remote debug via QEMU
qemu-aarch64-static -g 1234 ./hello &
aarch64-linux-gnu-gdb -ex "target remote :1234" ./hello| Error | Cause | Fix |
|---|---|---|
| Running target binary on host without QEMU | Use |
| Wrong-architecture object linked | Check triplet; ensure all objects use same toolchain |
| Host library path used for cross-link | Set |
| Missing ARM ABI runtime | Link with |
| Distance too large | Use |
| Wrong | Set correct CPU flags for target |
# Tell build systems to use cross-compiler
export CC=aarch64-linux-gnu-gcc
export CXX=aarch64-linux-gnu-g++
export AR=aarch64-linux-gnu-ar
export STRIP=aarch64-linux-gnu-strip
export OBJDUMP=aarch64-linux-gnu-objdump
# For autoconf projects
./configure --host=aarch64-linux-gnu --prefix=/usrskills/compilers/gccskills/debuggers/gdbgdbserverskills/low-level-programming/assembly-armskills/build-systems/cmake