Cross-Compiling
Cross-compiling means building on one machine (the host, here x86-64 Linux) for another (the target, say a 64-bit ARM board). You need three things: a compiler that emits target code, the target's headers and libraries (a sysroot), and a toolchain file that tells CMake about both.
The image these lessons run in has no ARM sysroot, but Clang is a cross compiler out of the box: one binary can emit code for every architecture LLVM supports, selected with --target. That is enough to build an ARM static library, which needs only the compiler and an archiver, and to see exactly where a full cross build needs a sysroot.
Attempt 1: no sysroot
mkdir -p xbuild && cd xbuild
cat > aarch64-clang.cmake <<'CM'
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR aarch64)
set(CMAKE_C_COMPILER clang)
set(CMAKE_C_COMPILER_TARGET aarch64-linux-gnu) # becomes --target=aarch64-linux-gnu
CM
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(Checksum LANGUAGES C)
add_library(checksum STATIC checksum.c)
CM
echo 'unsigned checksum(const unsigned char *p, unsigned n) { unsigned s = 0; while (n--) s = s * 31 + *p++; return s; }' > checksum.c
cmake -S . -B build -G Ninja --toolchain aarch64-clang.cmake 2>&1 \
| grep -E 'compiler identification|is not able to compile|unrecognised emulation|incomplete'-- The C compiler identification is Clang 18.1.3
is not able to compile a simple test program.
/usr/bin/ld: unrecognised emulation mode: aarch64linux
-- Configuring incomplete, errors occurred!
CMake's first step is to check that the compiler can build and link a test program. Compiling worked (Clang knows aarch64), but linking needs an aarch64 linker and the target's C library, and the host's ld only speaks x86. A real setup provides both (a cross binutils + sysroot); a static library needs neither.
Attempt 2: build a static library
CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY tells CMake to test the compiler by building a static library instead of an executable. It is meant for bare-metal targets (which need a linker script to link anything) and is exactly what we need here:
mkdir -p xbuild && cd xbuild
cat > aarch64-clang.cmake <<'CM'
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR aarch64)
set(CMAKE_C_COMPILER clang)
set(CMAKE_C_COMPILER_TARGET aarch64-linux-gnu)
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY) # don't try to link test executables
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
CM
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(Checksum LANGUAGES C)
message("host ${CMAKE_HOST_SYSTEM_PROCESSOR} -> target ${CMAKE_SYSTEM_PROCESSOR}, CMAKE_CROSSCOMPILING=${CMAKE_CROSSCOMPILING}")
add_library(checksum STATIC checksum.c)
find_library(M_LIB m)
message("find_library(m) -> ${M_LIB}")
CM
echo 'unsigned checksum(const unsigned char *p, unsigned n) { unsigned s = 0; while (n--) s = s * 31 + *p++; return s; }' > checksum.c
cmake -S . -B build -G Ninja --toolchain aarch64-clang.cmake --log-level=NOTICE
cmake --build build -v | grep -o 'clang --target=aarch64-linux-gnu .* -c'
cd build && ar x libchecksum.a && file checksum.c.o
echo "--- the same source built natively"
cd .. && cc -c checksum.c -o native.o && file native.ohost x86_64 -> target aarch64, CMAKE_CROSSCOMPILING=TRUE
find_library(m) -> M_LIB-NOTFOUND
-- Configuring done (0.2s)
-- Generating done (0.0s)
-- Build files have been written to: /home/learner/xbuild/build
clang --target=aarch64-linux-gnu -fno-color-diagnostics -MD -MT CMakeFiles/checksum.dir/checksum.c.o -MF CMakeFiles/checksum.dir/checksum.c.o.d -o CMakeFiles/checksum.dir/checksum.c.o -c
checksum.c.o: ELF 64-bit LSB relocatable, ARM aarch64, version 1 (SYSV), not stripped
--- the same source built natively
native.o: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped
The object file is ARM aarch64 code, built on an x86-64 machine. Notice also find_library(m): with the ONLY find-root mode and no sysroot, it found nothing instead of returning the host's x86-64 libm.so. That is the point of the find-root settings: a cross build that silently links host libraries fails much later, at the linker or on the device.
What changes in a cross build
| Topic | What to do |
|---|---|
if(LINUX), CMAKE_SYSTEM_NAME |
describe the target; CMAKE_HOST_* describes the build machine |
try_run / check_* that run programs |
can't run target binaries; provide the answers in the toolchain or cache, or set CMAKE_CROSSCOMPILING_EMULATOR (e.g. qemu-aarch64) |
| code generators built in the project | a generator compiled for ARM can't run on the build machine. Build host tools in a separate native build (or with ExternalProject_Add), then import them with add_executable(gen IMPORTED) or find_program |
| tests | add_test uses CMAKE_CROSSCOMPILING_EMULATOR automatically when it is set; otherwise run them on the device |
| dependencies | must be built for the target too: a package manager with a matching profile or triplet (Conan --profile:host, vcpkg --triplet arm64-linux), or a sysroot that contains them |
Where cross toolchains come from
- Distribution packages:
gcc-aarch64-linux-gnu,g++-arm-linux-gnueabihfpluslibc6-dev-arm64-crosson Debian/Ubuntu. - Vendor SDKs: Android NDK, Yocto/Poky SDKs (they ship an environment script and a toolchain file), Raspberry Pi toolchains, ARM GNU Toolchain for bare metal.
- Clang + sysroot: one Clang, any number of sysroots copied from target images.
- Container-based: build inside a target-architecture container under QEMU user emulation (slow, but no toolchain file needed).
Test yourself
Why did the first configure fail while compiling worked?
- Clang can't target aarch64
- CMake's compiler check also links a test program, which needs an aarch64 linker and C library (a sysroot)
- Ninja doesn't support cross-compiling
- The toolchain file was ignored
What does CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY change?
- Makes every library static
- CMake's compiler checks build a static library instead of linking an executable
- Disables try_compile entirely
- Links statically against libc
A project builds a code generator and runs it during the build. What breaks when cross-compiling?
- Nothing
- The generator is compiled for the target and can't run on the build machine; it must be built natively and imported
- Ninja refuses to run it
- add_custom_command is disabled