Cross compiler

From Wikipedia, the free 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. Cross compiler tools are generally found in use to generate compiles for embedded system or multiple platforms. It is a tool that one must use for a platform where it is inconvenient or impossible to compile on that platform, like microcontrollers that run with a minimal amount of memory for their own purpose. It has become more common to use this tool for paravirtualization where a system may have one or more platforms in use.

Contents

[edit] 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. Due to the limited resource to also use tools to test the product, cross-compilation on embedded systems is typically more involved and prone to errors than with native compilation.
  • 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 used by both the host and the target system.

Typically the hardware architecture differs (e.g. compiling a program destined for the MIPS architecture on an x86 computer) but cross-compilation is also applicable when only the operating system environment differs, as when compiling a FreeBSD program under Linux, or even just the system library, as when compiling programs with uClibc on a glibc host.

[edit] GCC and cross compilation

gcc, (a free software collection of compilers), can be set up to cross compile. It supports many platforms and 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, which 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 have 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.

The GNU autotools packages (i.e. autoconf, automake, and libtool) use the notion of a build platform, a host platform, and a target platform. The build platform is where the code is actually compiled. The host platform is where the compiled code will execute. The target platform usually only applies to compilers. It represents what type of object code the package itself will produce (such as cross-compiling a cross-compiler); otherwise the target platform setting is irrelevant. For example, consider cross-compiling a video game that will run on a Dreamcast. The machine where the game is compiled is the build platform while the Dreamcast is the host platform.

[edit] Canadian Cross

The Canadian Cross is a technique for building cross compilers for other machines. Given three machines A, B, and C, one uses 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:

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

The end-result cross compiler (4) will not be able to execute the resulting compiler on your build machine A; instead you would use it on machine B to compile an application into executable code that would then be copied to machine C and executed on machine C.

For instance, NetBSD provides a POSIX Unix shell script named "build.sh" which will first build its own toolchain with the host's compiler; this, in turn, will be used to build the cross-compiler which will be used to build the whole system.

The term Canadian Cross came about because at the time that these issues were all being hashed out, Canada had three national political parties.

[edit] External links

[edit] See also