Jump to content

Cross compiler: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Rei-bot (talk | contribs)
m robot Adding: pt:Compilador cruzado
Clarify confusing sentence
Line 32: Line 32:
* 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 ''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.
The end-result cross compiler (4) will not be able to run 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 <code>build.sh</code>" 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.
For instance, [[NetBSD]] provides a [[POSIX]] [[Unix shell]] script named <code>build.sh</code>" 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.

Revision as of 20:03, 10 November 2007

A cross compiler is a compiler capable of creating executable code for a platform other than the one on which the 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.

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. Since debugging and testing may also require more resources than is available on an embedded system, cross-compilation can be less involved and prone to errors than 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 set up to compile 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) resolves some of the reasons for which cross compilers were developed. The virtual machine paradigm allows the same compiler output to be used across multiple target systems.

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.

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 requires that a compiled copy of binutils be available for each targeted platform. 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. GCC can then be run normally provided that the tools, which binutils creates, are available in the path, which can be done using the following (on UNIX-like operating systems with bash):

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

Cross compiling GCC requires that a portion of the target platform's C standard library be available on the host platform. At least the crt0, ... components of the library must be available. You may 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 compile C source code. To configure GCC with newlib, use the switch --with-newlib.

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.

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 run 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.[1]

See also

References

  1. ^ "4.9 Canadian Crosses". CrossGCC. Retrieved 2007-10-11. This is called a `Canadian Cross' because at the time a name was needed, Canada had three national parties.