Building a Cross-Compiler (GCC for example)
A cross compiler is a compiler capable of creating executable code for a platform other than the one on which the compiler is run. It is a the first tool that one must use for an embedded platform. This article takes GCC for example to introduce several different approaches to build a cross-compiler.
The easiest way of building a cross compiler must be using the Crosstool project. Crosstool is a set of scripts designed for building and testing several versions of gcc and glibc for most architectures supported by glibc. Using it involves downloading the source code and making one of the presupplied files feed the right parameters into the script that builds the compiler. Crosstool will download the right software, even the patches, necessary to make the software work on the target platform. A cool handy-dandy matrix listing supported platforms and software versions can be found here.
However, if your project requires support for an alternate C library, crosstool becomes more difficult to use.
Many developers prefer to use uClibc, a smaller implementation of the C library. For those users, something similar to Crosstool called buildroot can be used. Besides building a cross-compiler, the ‟buildroot‟ can also be used for building a root filesystem for the board based on the related BusyBox project.
Finally, for those who do not want to use ready-made tools, building a cross-compiler by hand would be not as daunting as one thinks. The follow steps outline the process, where $TARGET is the target processor and $INSTALLAT is the directory where the compiler will reside after being built:
1. Download and build binutils:
$./binutils-/configure --target=$TARGET --prefix=$INSTALLAT
$ make ; make install
2. Copy the include and asm from the board's kernel to $INSTALLAT
$ cp -rvL $KERNEL/include/linux $KERNEL/include/asm $INSTALLAT/include
3. Download and build bootstrap GCC. It is best to build the bootstrap GCC in its own directory and not the directory where it has been unpacked.
$ mkdir ~/$TARGET-gcc ; cd ~/$TARGET-gcc
$../gcc-/configure --target=$TARGET --prefix=$INSTALLAT
--with-headers=$INSTALLAT/include --enable-languages="c" -Dinhibit_libc
$ make all ; make install