Definition
Cross-compilation is building a program on one kind of computer so that it runs on a different kind. The computer that runs the compiler, the tool that translates source code into machine instructions, is called the host. The computer that will run the finished program is called the target. A compiler used this way is a cross-compiler. Its output follows the target’s rules, not the host’s: machine instructions, data sizes, function-calling rules and memory addresses. Building for the same kind of computer as the host is called native compilation.
The idea
A microcontroller §6.1, a small computer on one chip, typically has far too little memory to run the compiler that builds its program, so the program is built on a desktop computer. The desktop has a 64-bit x86 processor and an operating system. The microcontroller has a 32-bit Arm processor and no operating system. The two processors do not understand the same instructions, so a program built for the desktop cannot run on the chip.
The core idea is that a compiler is a translator, and a translator does not have to live where its readers live. A translator in London can write a letter in Japanese for a reader in Tokyo, but the letter must also follow the reader’s conventions for dates, addresses and units. For a program, those conventions are the size of each data type, the rules for passing values between functions, the library of standard functions, and the memory addresses that the program may occupy. The picture stops being accurate in one respect: a processor, unlike a human reader, does not cope with a small mistake. A convention taken from the host by mistake can pass the build without an error message and fail on the target.
How it works
Four choices fix what a cross build produces.
The compiler target. The processor family, the operating system and the binary conventions (described in the next item) that the output is for, usually written as a target triple, a short hyphenated name.
arm-none-eabimeans: Arm processor, no operating system, the binary conventions that Arm defines for embedded programs. Options such as-mcpu=cortex-m4name the exact processor model. Together they fix the instruction set architecture §4.38, the set of machine instructions and registers that the compiler may use.The ABI §12.47 (application binary interface). The rules that separately compiled pieces of code must share to call each other: the size and alignment §4.22 of each data type, where alignment is the address multiple at which a value must start, the layout of structures, and which registers, the processor’s own storage cells, carry function arguments and results. The target fixes most of them. Options such as
-mfloat-abi, which on Arm selects whether floating-point values (numbers with a fractional part) are passed in the general registers or in separate floating-point registers, fix the rest. Theeabiin the triple above names Arm’s ABI for embedded programs.The C library and the compiler’s runtime library. Precompiled code for standard functions such as
memcpy, and for operations that the processor lacks, such as division on a processor without a divide instruction.The linker script §12.5. A text file that tells the linker §12.4, the tool that joins the compiled pieces into one program, at which memory addresses of the target to place the code and the data.
A cross build then runs in this order.
The build system, the tool that runs the commands of a build, is told to cross-compile. With CMake §12.12, a tool that generates build instructions, a settings file called a toolchain §12.2 file (a toolchain is a compiler with its companion tools and libraries) names the cross-compiler and sets the variable
CMAKE_SYSTEM_NAMEto the name of the target’s operating system, or toGenericfor a target without one. Setting that variable is what makes CMake treat the build as a cross build.The cross-compiler translates each source file into an object file, the compiled form of that one file, for the target. The widely used compiler
gccis a separate program for each target, named with the triple as a prefix, such asarm-none-eabi-gcc. The compiler Clang §12.17 is one program for all targets and takes the triple in its-targetoption.The linker joins the object files with the target’s libraries and places everything at the addresses that the linker script gives. The finished program is then copied to the target, or run in an emulator, a program that imitates the target’s processor. The host’s own processor cannot run the program directly when its instructions differ from the target’s.
Worked example
One line of C is compiled twice without any change, once for the host and once for a target:
struct record { void *next; uint32_t count; };
The structure holds a pointer, which is a memory address, and a 32-bit number. The host compiler is gcc on 64-bit x86 Linux, whose target triple is x86_64-linux-gnu. The cross-compiler is arm-none-eabi-gcc with the option -mcpu=cortex-m4. The two compilers lay the structure out differently:
| host build | target build | |
|---|---|---|
| size of a pointer | 8 bytes | 4 bytes |
position of count in the structure |
byte 8 | byte 4 |
| size of the structure | 16 bytes | 8 bytes |
| processor named in the output file | x86-64 | Arm |
The arithmetic for the host: the pointer takes 8 bytes and count takes 4, which is bytes. On this host a pointer must start at an address that is a multiple of 8. The compiler therefore makes the structure a multiple of 8 bytes long, so that every pointer in a row of such structures starts at a multiple of 8. It adds 4 unused padding bytes §4.27: . For the target: bytes, already a multiple of 4, so nothing is added.
The result means that the two builds disagree about data, not only about instructions. If a host program writes the 16 bytes of one structure to a file, the target program reads 8 bytes and looks for count at byte 4, which in the host’s bytes is the middle of the pointer. A test that passes when this code is compiled for the host has tested the 16-byte layout only.
Stated precisely
The output of a cross build depends on the four choices above, on the source files, and on the remaining options and settings of the build tools. The host’s own processor, data sizes and libraries are not among these inputs. They can enter the output only by mistake, for example when the build finds a header file (a file of shared declarations) or a library of the host in place of the target’s.
The contract for linking is: every object file and every library joined into one program must use only instructions that the target processor has, and all of them must follow the same ABI. The tools check only part of this contract. The linker rejects an object file made for a different processor family. The linker for Arm also rejects a mix of the two ways of passing floating-point arguments. Its message says that one file uses VFP register arguments, meaning floating-point registers, and the output does not. The linker does not compare the structure layouts or the argument types that two files assume.
Limits and common mistakes
A build that succeeds proves that translation and linking finished. It does not prove that the intended target was selected. If the setting that selects the target is missing, a build system that falls back to the host’s own compiler builds for the host, and no message says that the target is wrong.
A test that runs on the host tests a different program. The host build has the host’s data sizes, the host’s C library and the host’s instructions. It does not exercise the target’s interrupts §6.11 (hardware signals that pause the running code), hardware devices or timing.
The word host has two meanings. In everyday use, and in CMake, the host is the computer that runs the build. Many open-source packages are prepared for a build by a configure script that the tool Autoconf generates. In such a script, --build names the computer that runs the build, and --host names the computer that will run the program.
Data that host and target exchange needs a format that is defined byte by byte. The raw memory contents of a structure are not such a format, as the worked example shows.
Questions answered
Which compiler target, libc, ABI, and linker script determine the output?
The compiler target, the C library (libc), the application binary interface (ABI) and the linker script that determine the output are the ones that the build actually used, and the build’s own command lines and output files show which those were. A project name or the name of a development kit settles none of the four. The C library holds the precompiled standard functions of the C language, and the ABI is the set of binary rules that compiled pieces of code share.
Compiler target: the target triple of the compiler program that ran, together with the processor options on its command line. A target triple is a short hyphenated name for the processor family, the operating system and the binary rules that a compiler builds for. The command
arm-none-eabi-gcc -dumpmachine, for example, prints the triplearm-none-eabi.libc: the library file that the linker, the tool that joins the compiled pieces into one program, opened. The linker’s map file, a report that it writes on request, lists that file. The processor and ABI options select the file. In one installation of version 13.2 of
arm-none-eabi-gcc, the options-mcpu=cortex-m4 -mthumbtake the libraries from the directorythumb/v7e-m/nofp, and adding-mfloat-abi=hard -mfpu=fpv4-sp-d16changes it tothumb/v7e-m+fp/hard, which holds a different copy of every library.ABI: fixed by the target and by ABI options such as
-mfloat-abi. Every compiled file and every library in the program must have been built with the same choice.Linker script: the text file that tells the linker at which memory addresses to place the program, passed to the linker with the option
-T. If none is passed, the linker uses a default script that is built into it, which the commandld --verboseprints.
All four must describe the same target. To establish them for one build, read the complete command lines that the build ran and the map file of the link.
Related entries: Toolchain (§12.2), Linker script (§12.5), Instruction set architecture (ISA) (§4.38), Native application binary interface (ABI) (§12.47), CMake (§12.12), Struct padding and packing (§4.27). Further reading: [82, 108].