Skip to content
tezvyn:

Cross-Compilation: Build Anywhere, Run Elsewhere

Source: Wikipedia: Cross compilerHardHow cards are made

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).

Interview question

Which scenario would generally make cross-compilation a less ideal choice compared to native compilation?

  • a.Developing software for an embedded system with limited resources.
  • b.Compiling code that requires immediate execution of helper tools on the build machine as part of the build process.Correct
  • c.Building an application that needs to run on multiple diverse platforms from a single build server.
  • d.Bootstrapping a new operating system or compiler from scratch.
Why?

Cross-compilation complicates build scripts that compile and then immediately run a tool, as the tool would need to be built for the host architecture. In contrast, developing for embedded systems (Option A) is a primary reason to use cross-compilation due to resource constraints.

Just read this? Test yourself on what you have been reading.

Read the original → en.wikipedia.org

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.

See open roles