Why use a Cross Compiler?

Since obsidian.systems and zw3rk posted the Cross Compilation survey (and results), I have been writing about cross compiling to Raspberry Pi, and specifically building a SDK for Raspbian Jessie, and a Haskell cross compiler, one topic has been coming up repeatedly: Why use a Cross Compiler?

For those who work in the mobile application space, cross compilation is very likely an essential part of their day. iOS and Android apps are naturally cross compiled, even though this fact is mostly hidden away. In both cases the application is developed on a much more powerful machine than the one it is finally deployed to (or tested on).

Power Differences

This leads us to the first reason for using a cross compiler. If the machine that builds the software is much more powerful than the one that finally hosts and runs the software, it make economical sense to use the more powerful machine to do the compilation.

Resource Constraints

Another reason to use a cross compiler is the fact that the target that is finally hosting the software might be ressource constraint in a way that prohibits or severely hinders the execution of the compiler. While the ressources are perfectly fine for running the compiled software, they might not be adequate to run the compiler.

Environmental Restrictions

A third reason for using a cross compiler could stem from the restrictions put onto the environment that governs the target. If the target simply does not admit to running the compiler or necessary toolchain to compile the software, the software must be compiled on a different build machine.

Alternatives to cross compilation include building in an emulated environment that is sufficiently close to the target. The build environment is then identical or sufficiently similar to the host environment. If the compiler in question does not support cross compilation this might be the only option.

Finally with cross compilation the compiler now has to deal with two different machines. The build machine on which the cross compiler can run, but the software built by the cross compiler can not run on. And the host machine on which the built software can run, but the cross compiler can not. We will learn more about the challenges this presents for a Haskell cross compiler, and especially for Template Haskell next week.

Should you use a cross compiler or not? I don’t believe there is a general answer to this. It all comes down to the constraints as mentioned above; the feasibility and complexity of setting up (or obtaining) the SDK, the toolchain, and the compiler compared to setting up a virtual (or emulated) sufficiently similar environment to run a regular compiler in.