Monthly Archives: August 2014

Building a cross compiler

Building a cross compiler is not a hard task, but still if you are not doing it in the right way it can waste lot of your time. This blog post is based on the steps I had followed in my machine with Ubuntu 14.04 installed on it.

gnu

First, make sure that you keep all the source in a directory “$HOME/src”.

mkdir $HOME/src
cd $HOME/src
wget http://ftp.gnu.org/gnu/binutils/binutils-2.24.tar.gz

tar xvf binutils-2.24.tar.gz
wget http://ftp.gnu.org/gnu/gcc/gcc-4.9.1/gcc-4.9.1.tar.gz
tar xvf gcc-4.9.1.tar.gz

You can change the version number according to the latest one. The version number can be obtained from the gcc website.

Install few supporting packages before you start the build process,

sudo apt-get install libmpc-dev
sudo apt-get install libcloog-isl-dev
sudo apt-get install libisl-dev
sudo apt-get install libmpfr-dev
sudo apt-get install libgmp3-dev

Hope these packages are also in other linux distros.

Now we need to decide on where to install our new compiler,  it is dangerous to install it in the system directory. So we can create a directory $HOME/opt/cross. If you want it to be global “/usr/local/cross” is the ideal location for placing the compiler.

Preparing to compile by setting the correct PATH variable, this can either executed as a shell command or it can be include in the ~/.bashrc file,

export PREFIX="$HOME/opt/cross"
export TARGET=x86_64-elf 
export PATH="$PREFIX/bin:$PATH"

Taking the first step by building Binutils, 

cd $HOME/src 
mkdir build-binutils 
cd build-binutils 
../binutils-2.14/configure --target=$TARGET --prefix="$PREFIX" --disable-nls --disable-werror 
make -j12 
sudo make -j12 install

Building GCC, 

mkdir build-gcc
cd build-gcc
../gcc-4.9.1/configure --target=$TARGET --prefix="$PREFIX" --disable-nls --enable-languages=c,c++ --without-headers
make -j12 all-gcc
make -j12 all-target-libgcc
sudo make install-gcc
sudo make install-target-libgcc

Now you have a new compiler which does not have access to C library or C runtime.

Leave a comment

Filed under linux, Operating Systems