This article describes how to setup your machine to cross compile programs for some other architecture. This involves setting up is called as a
"cross compile tool chain". We are going to use crosstool-ng for this purpose. It is a collection of scripts that will download, compile, install the entire tool chain required for cross compiling. We will create a "ARM" based tool chain in this example.
We will be installing the toolchain in the "cctools" directory inside our home folder. Also note that all commands are run as normal user unless otherwise mentioned. Let us create the directory for the same.
$cd
$mkdir $HOME/cctools
$cd $HOME/cctools
Now, go to
http://crosstool-ng.org and download and extract the stable version of the script.
$wget http://crosstool-ng.org/download/crosstool-ng/crosstool-ng-1.12.0.tar.bz2
$tar jxvf crosstool-ng-1.12.0.tar.bz2
$cd crosstool-ng-1.12.0
You also need to install the following packages (run as root):
$sudo apt-get install libtool automake texinfo flex bison subversion cvs expat libexpat1-dev python-dev libncurses5-dev
Once this is done, we will create a separate build directory.
$mkdir $HOME/cctools/build
Now, let us configure crosstool-ng to build in that directory.
$./configure --prefix=$HOME/cctools/build
Compile and install crosstool-ng.
$make
$make install
This will install crosstool-ng inside the build directory that we specified. We need to add this build directory to the PATH shell variable.
$export PATH="${PATH}:$HOME/cctools/build/bin"
This has completed the installation of the crosstool-ng.
(Note : You can add this to your ~/.bashrc file for future use)
Next step is to setup the system for cross compilation. We will create a separate directory for the "ARM" tool chain.
$mkdir $HOME/cctools/arm
$cd $HOME/cctools/arm
Now run the crosstool-ng configuration script in this directory.
$ct-ng menuconfig
This will show you a menu driven interface for selecting the target architecture, path for download and installation of the tool chains, architecture specific optimization and features, etc. In this configuration window change the relevant options for "ARM" based tool chain.
You can directly copy a predefined configuration file for the architecture from the $HOME/cctools/crosstool-ng-1.12.0/samples folder and then run the above "$ct-ng menuconfig" tool.
eg :
$cp $HOME/cctools/crosstool-ng-1.12.0/samples/arm-cortex_a8-linux-gnueabi/crosstool.config .config
Also you need to change the prefix directory where you want the tool chain installed. We will build it inside the build directory where we initially installed the crosstool-ng.
Paths and misc options > Prefix directory : ${HOME}/cctools/build/${CT_TARGET}
Once this is done press "ESCAPE" and save the configuration file. The file is saved as ".config", you can even hand edit this file directly if you want.
Now the final step is download, extract and build the tool chain. All of this is done automatically by the crosstool-ng by executing:
$ct-ng build
It downloads around 200MB source files and takes around 1 hour on a "3 Ghz dual core with 4 GB RAM" machine for compilation. The source files are downloaded in the ".build" directory. Once this completes the tool chain is available in the ${HOME}/cctools/build/ directory, the folder name is dependent of the architecture selected. You can add this to the PATH shell variable.
$export PATH="${PATH}:$HOME/cctools/build/arm-cortex_a8-linux-gnueabi/bin"
(Note : You can add this to your ~/.bashrc file for future use)
Let us cross-compile one sample "Hello World" C program that will execute on a "ARM" machine. The C program filename is "sample.c"
$arm-cortex_a8-linux-gnueabi-gcc sample.c -o sample -Wall
This will compile the executable file "sample" for the "ARM" architecture.