Cross compiler

From Free net encyclopedia

A cross compiler is a compiler capable of creating executable code for a platform other than the one on which the cross compiler is run. Such a tool is handy when you want to compile code for a platform that you don't have access to, or because it is inconvenient or impossible to compile on that platform (as is the case with embedded systems, on which various kinds of microcontrollers run with a minimal amount of memory for their own purpose).

Contents

Uses of Cross Compilers

The fundamental use of a cross compiler is to separate the build environment from the target environment. This is useful in a number of situations:

  • Embedded computers where a device has extremely limited resources. For example, a microwave oven will have an extremely small computer to read its touchpad and door sensor, provide output to a digital display and speaker, and to control the machinery for cooking food. This computer will not be powerful enough to run a compiler, a file system, or a development environment.
  • Compiling for multiple machines. For example, a company may wish to support several different versions of an operating system or to support several different operating systems. By using a cross compiler, a single build environment can be maintained that would build for each of these targets.
  • Compiling on a server farm. Similar to compiling for multiple machines, a complicated build that involves many compile operations can be executed across any machine that is free regardless of its brand or current version of an operating system.
  • Bootstrapping to a new platform. When developing software for a new platform, or the emulator of a future platform, one uses a cross compiler to compile necessary tools such as the operating system and a native compiler.

Use of virtual machines (such as Java's JVM) ameliorate some of the problems for which cross compilers were developed. The virtual machine method allows the same compiler output to be by both the host and the target system.

The GCC cross compiler

gcc is a free cross compiler that supports dozens of platforms and a handful of languages. However, due to limited volunteer time and the huge amount of work it takes to maintain working cross compilers, in many releases some of the cross compilers are broken.

gcc relies upon the binaries of binutils for the targeted platform to be available. Especially important is the GNU Assembler. Therefore, binutils first has to be compiled correctly with the switch --target=some-target sent to the configure script. gcc also has to be configured with the same --target option. Then gcc can be compiled as normal provided that the tools binutils creates are available in the path. On unix-like operating systems with bash, that can be accomplished with the following:

PATH=/path/to/binutils/bin:$PATH make

Cross compiling gcc requires that a portion of the C standard library is available for the targeted platform on the host platform. At least the crt0, ... components of the library has to be available in some way. You can choose to compile the full C library, but that can be too large for many platforms. The alternative is to use newlib, which is a small C library containing only the most essential components required to get C source code compiled. To configure gcc with newlib, use the switch --with-newlib to the configure script.

Canadian Cross

This is a technique for building cross compilers for other machines. That is, use machine A to build a cross compiler that runs on machine B to create executables for machine C. When using the Canadian Cross with gcc, there may be four compilers involved.

On Machine A:

  • Use the proprietary native Compiler for machine A (1) to build gcc native compiler for machine A (2).
  • Use the gcc native compiler for machine A (2) to build gcc cross compiler from machine A to machine B (3)
  • Use the gcc cross compiler from machine A to machine B (3) to build gcc cross compiler from machine B to machine C (4)

Note that you will not be able to execute the resulting compiler on your build machine A. You would use gcc cross compiler from machine B to machine C (4) on machine B to compile an application into executable code that would then be copied to machine C and executed on machine C.

External links

es:Compilador cruzado pl:Cross-kompilator