Glossary · 5 · Hardware SDKs and devices
Cross-compilation
Also known as: Cross-compiling, Cross compilation
In software engineering, cross-compilation is the compilation of source code on one computer system, the host or build machine, into executable code for a different system, the target, typically one with another processor architecture or operating system, using a toolchain configured for that target.
- Hardware SDK
- Toolchains
In one sentence
Cross-compilation builds code on one system (the host) for a different target architecture or operating system with a target-specific toolchain.
Example
A developer builds firmware for an Arm Cortex-M microcontroller on an x86-64 laptop with a cross-compiler for an Arm bare-metal target and flashes the result through a debug probe.
How it applies
- Hardware SDKs: Cross-compilation is the normal build mode of hardware kits, because microcontrollers and many embedded processors cannot run a compiler themselves. The kit ships a cross-toolchain (compiler, assembler, linker, libraries) and the headers for the target, for example for a microcontroller (MCU).
- Build, host and target: The GCC documentation distinguishes three systems: the machine the build runs on (build), the machine the built program runs on (host) and, for compilers, the machine the compiler produces code for (target). In everyday SDK use, "host" usually means the development computer and "target" the device.
- Target triples: Clang identifies the target by a triple of the general form arch-vendor-system-environment, for example x86_64-linux-gnu. If no target is given, Clang assumes the host triple and produces code for the host platform, which breaks later when assembling or linking.
- Sysroot: A sysroot is the directory tree with the target's headers and libraries; the --sysroot option changes the logical root for headers and libraries. Pointing the compiler at the right sysroot keeps host libraries out of a target build.
- Platform and developer SDKs: Mobile platform kits such as the Android NDK cross-compile native code for each supported application binary interface (ABI); cloud SDKs cross-compile when they build container images or agents for another processor architecture.
- Documentation: State the supported host systems, the target triples, the toolchain version and where the sysroot comes from. Binaries built with a different toolchain may not be compatible.
Cross-compilation vs. emulation
Cross-compilation produces target code on a foreign host. Emulation runs target code on a foreign host. They are often combined: code is cross-compiled and then tested on an emulator before it runs on real hardware.
External references
- GNU Project: GCC Internals, Configure Terms and History
- LLVM Project: Cross-compilation using Clang