Cross Gcc

mohitmishra786/low-level-dev-skills/skills/compilers/cross-gcc

作者 mohitmishra786bdc58472fa9f無授權條款253 個星標收錄於 2026年10月9日更新於 2026年10月9日儲存庫3 個月前更新

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.

AI 產生的概覽

指導設定與使用針對 ARM、AArch64、RISC-V 與 MIPS 目標的 GCC 交叉編譯工具鏈。

功能
此技能提供從 x86-64 主機對嵌入式與多架構目標進行 GCC 交叉編譯的說明。內容涵蓋 GNU 三元組、工具鏈安裝、sysroot、pkg-config 覆寫、CMake 工具鏈檔案、以 QEMU 為基礎的測試與 GDB 遠端除錯,並附上常見錯誤與修正對照表。它也列出建置系統與 autoconf 專案所需的環境變數,並指向隨附的 ARM 編譯旗標參考文件。
適用情境
適用於設定交叉 GCC 工具鏈或 sysroot、從 x86-64 主機為 ARM、AArch64、RISC-V 或 MIPS 建置,以及與 Yocto/Buildroot 整合。也適合排查架構不符的錯誤(例如 wrong ELF class 或 Exec format error),以及在 QEMU 下執行交叉編譯的執行檔。
執行需求
需要交叉編譯工具鏈,例如 gcc-aarch64-linux-gnu、gcc-arm-none-eabi 與 binutils 等套件,選用時還需要 CMake、pkg-config、qemu-user-static 與交叉版 GDB。此技能不含指令碼,僅為說明文件,並附帶一份 ARM GCC 編譯旗標參考。

Cross-GCC

Purpose

Guide agents through setting up and using cross-compilation GCC toolchains: triplets, sysroots, pkg-config, QEMU-based testing, and common failure modes.

Triggers

  • "How do I compile for ARM on my x86 machine?"
  • "I'm getting 'wrong ELF class' or 'cannot execute binary file'"
  • "How do I set up a sysroot for cross-compilation?"
  • "pkg-config returns host libraries in my cross build"
  • "How do I debug a cross-compiled binary with QEMU + GDB?"

Workflow

1. Understand the triplet

A GNU triplet has the form <arch>-<vendor>-<os>-<abi> (often 3 or 4 parts):

TripletTarget
aarch64-linux-gnu64-bit ARM Linux (glibc)
arm-linux-gnueabihf32-bit ARM Linux hard-float
arm-none-eabiBare-metal ARM (no OS)
riscv64-linux-gnu64-bit RISC-V Linux
x86_64-w64-mingw32Windows (MinGW) from Linux
mipsel-linux-gnuLittle-endian MIPS Linux

2. Install the toolchain

bash
# Debian/Ubuntusudo 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
# Verifyaarch64-linux-gnu-gcc --version

3. Basic cross-compilation

bash
# Caarch64-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

4. Sysroot

A sysroot is a directory containing the target's headers and libraries. Required when your code links against target-specific libraries.

bash
# Use a sysrootaarch64-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 output

Verify the sysroot is correct:

bash
aarch64-linux-gnu-gcc --sysroot=/path/to/sysroot -v -E - < /dev/null 2>&1 | grep sysroot

5. pkg-config for cross builds

pkg-config will return host library paths by default. Override:

bash
export PKG_CONFIG_SYSROOT_DIR=/path/to/sysrootexport PKG_CONFIG_LIBDIR=${PKG_CONFIG_SYSROOT_DIR}/usr/lib/aarch64-linux-gnu/pkgconfig:${PKG_CONFIG_SYSROOT_DIR}/usr/share/pkgconfigexport PKG_CONFIG_PATH=   # clear host path
pkg-config --libs libssl  # now returns target paths

6. CMake cross-compilation

Create a toolchain file aarch64.cmake:

cmake
set(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)
bash
cmake -S . -B build -DCMAKE_TOOLCHAIN_FILE=aarch64.cmakecmake --build build

7. Test with QEMU

bash
# 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 QEMUqemu-aarch64-static -g 1234 ./hello &aarch64-linux-gnu-gdb -ex "target remote :1234" ./hello

8. Common errors

ErrorCauseFix
cannot execute binary file: Exec format errorRunning target binary on host without QEMUUse qemu-<arch>-static
wrong ELF class: ELFCLASS64 (or 32)Wrong-architecture object linkedCheck triplet; ensure all objects use same toolchain
/usr/bin/ld: cannot find -lfooHost library path used for cross-linkSet --sysroot; fix PKG_CONFIG_LIBDIR
undefined reference to '__aeabi_*'Missing ARM ABI runtimeLink with -lgcc or -lclang_rt.builtins
relocation R_AARCH64_ADR_PREL_PG_HI21 out of rangeDistance too largeUse -mcmodel=large or restructure
unrecognized opcodeWrong -mcpu or -marchSet correct CPU flags for target

9. Environment variables

bash
# Tell build systems to use cross-compilerexport CC=aarch64-linux-gnu-gccexport CXX=aarch64-linux-gnu-g++export AR=aarch64-linux-gnu-arexport STRIP=aarch64-linux-gnu-stripexport OBJDUMP=aarch64-linux-gnu-objdump
# For autoconf projects./configure --host=aarch64-linux-gnu --prefix=/usr

For a reference on ARM-specific GCC flags, see references/arm-flags.md [blocked].

Related skills

  • Use skills/compilers/gcc for GCC flag details
  • Use skills/debuggers/gdb for remote debugging with gdbserver
  • Use skills/low-level-programming/assembly-arm for AArch64 assembly specifics
  • Use skills/build-systems/cmake for toolchain file setup

來源與署名

來源:mohitmishra786/low-level-dev-skills位於skills/compilers/cross-gcc提交bdc5847

授權條款: 無授權條款

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架