tezvyn:

Cross-Compilation: Build Anywhere, Run Elsewhere

AI-drafted, machine-checkedSource: Wikipedia: Cross compileradvanced

A cross-compiler builds code for a different machine than the one it runs on. This lets you use a powerful PC to compile for a tiny IoT device or a mobile phone. The footgun is forgetting that the target's OS and libraries matter, not just.

WHY IT EXISTS Many devices are too resource-constrained to run a compiler themselves. An IoT sensor, a car's ECU, or even a smartphone lacks the CPU power, RAM, or storage for a full development toolchain. Cross-compilation solves this by using a powerful machine (the host) to do the heavy lifting of compiling code for the weaker machine (the target).

THE MENTAL MODEL A cross-compiler is like a translator who speaks English but writes in Japanese. The translator works in an English-speaking office (the host machine, e.g., your x86-64 laptop) but produces a document that is only readable in Japan (the target machine, e.g., an ARM-based Android phone). The key is the complete separation of the build environment from the runtime environment.

HOW IT WORKS A standard compiler is configured to generate code for the same architecture it runs on. A cross-compiler is the same underlying software (like Clang or GCC) but configured with a different 'target triple'—a string like aarch64-unknown-linux-gnu that specifies the target CPU architecture, vendor, operating system, and ABI. To successfully link an executable, the cross-compiler also needs access to the target platform's headers and libraries, often packaged together in a directory called a 'sysroot'.

WHEN TO USE IT Cross-compilation is critical in several domains. First, embedded systems development, for compiling code for microcontrollers and other small devices. Second, mobile development, like building an iOS app on a Mac or an Android app on Windows. Third, for performance in CI/CD, where one fast build server can produce binaries for multiple different platforms (Windows, macOS, Linux) without needing separate native machines. Finally, it's used to bootstrap new operating systems and compilers from scratch.

WHEN NOT TO USE IT If a native compiler is available and the target machine is powerful enough, using it is simpler as it avoids toolchain complexity. Cross-compilation also complicates build scripts that compile and then immediately run a tool as part of the build; you may need to build such tools for both the host and the target. Testing is also harder, as you cannot run the compiled binary on the host machine without an emulator (like QEMU) or physical target hardware.

ONE CANONICAL EXAMPLE A game developer uses a Windows PC (host, x86-64 architecture) to write C++ code for an Android game. They use the Android NDK, which contains a Clang cross-compiler. The compiler runs on Windows but is configured to target aarch64-linux-android. It generates ARM64 machine code and links it against Android's Bionic C library, producing a shared library (.so file). This file cannot run on Windows but is packaged into an APK and executed on an Android phone (the target).

Read the original → en.wikipedia.org

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.