User:Max/Building a GCC 4.9 cross compiler on OS X Mavericks
In this tutorial you will learn how to build a GCC cross compiler toolchain on Mac OS X (10.9, Mavericks). This includes building cross binutils, a cross compiler and libgcc for your target architecture.
To do this tutorial, I assume that you have a basic knowledge about using the Terminal and commands like cd and mkdir.
Installing GCC
To build a cross compiler from the GCC sources, you first need to have the GNU GCC installed on your system (called the "host system"). This compiler will later be used to compile the cross-toolchain.
There might already be a compiler showing up when using gcc in Terminal, but this is not the actual GNU GCC, it's Apples LLVM compiler. To install the real GNU GCC, we use Homebrew. Once you have Homebrew installed, open up a Terminal. Then install GCC with the brew commands:
brew tap homebrew/versions brew install --enable-cxx gcc49
This may take a while. It installs the GNU GCC 4.9 with additional support for C++. You should then check if GCC is working by using "gcc-4.9 --version" in the Terminal.
Installing the necessary libraries
For building GCC you also need to install the libraries GMP, MPFR and MPC on your host system. You can do this with the following brew commands:
brew install mpfr brew install gmp brew install libmpc
Once this is done, the necessary libraries are available on your system, and you are ready for building the cross compiler.
Downloading the sources
The following sources are required to build the cross compiler toolchain. The versions here are the ones I'll be using in the following (and that are in the moment I'm writing this the newest sources). Any newer/older version might - or might not - work as well.
Once you've downloaded the archive files of the current sources (namely in our case gcc-4.9.0.tar.gz and binutils-2.24.tar.gz), create a folder in your home directory (name it for example src) and unpack the archives there.
Building
Open up a terminal and cd to the src directory we just created. Inside of that directory, you should now have the folders gcc-4.9.0 and binutils-2.24 containing our necessary sources.
Preparing environment variables
We will now set some environment variables for the build process by using the export command. Take care - these variables are temporary, if you open a new terminal session, you'll have to enter them again.
First, we set the compiler variables right, to let the configure script and makefiles of binutils and gcc know that they need to use the compiler we just installed instead of the LLVM:
export CC=gcc-4.9 export CXX=g++-4.9 export CPP=cpp-4.9 export LD=gcc-4.9
Also, we define the following variables. The PREFIX is the path where you want to install the cross-binaries to. The TARGET determines your target platform, for example we want to build an i686-elf compiler. The PATH export we're doing here just appends the directory that you specified as the target for your cross-binaries to the PATH.
export PREFIX="$HOME/opt/cross" export TARGET=i686-elf export PATH="$PREFIX/bin:$PATH"
Now we are ready to continue building binutils and then the gcc and libgcc.
binutils
- Create a folder binutils-2.24-build in your ~/src folder
- cd to that folder
- From inside of that folder, we now run the configure script of binutils: ../binutils-2.24/configure --target=$TARGET --prefix="$PREFIX" --disable-nls --disable-werror
- Once the configure script has successfully completed its work, perform the build and installation: make make install
gcc and libgcc
- Create a folder gcc-4.9.0-build in your ~/src folder
- cd to that folder
- From inside of that folder, we now run the configure script for GCC:
(Note: the configure script might throw an error about not finding the installation of MPC/GMP/MPFR. To make it find these libraries, use the respective "--with-*" parameter. When using brew, this is by default "/usr/local", so "--with-gmp=/usr/local" etc. should work)
../gcc-4.9.0/configure --target=$TARGET --prefix="$PREFIX" --disable-nls --enable-languages=c,c++ --without-headers
- Once the configure script has successfully completed its work, perform the build and installation: make all-gcc make all-target-libgcc make install-gcc make install-target-libgcc
Checking the installation
You can now check if the cross compiler works by typing i686-elf-g++ --version. To make the new cross tools always available in Terminal, you will have to open your user .bash_profile like this:
nano ~/.bash_profile
Now, add the following line (if you changed where you installed your crosstools to, you have to change it accordingly):
export PATH=$HOME/opt/cross/bin:$PATH
Press ^x for saving, press y and then enter to confirm saving. The crosstools are now in your PATH and are available in your Terminal.
Location of crosstools and target-libgcc
If you used the PREFIX variable from above, you should find a opt folder in your home directory. Inside of that folder is a cross folder, which contains all the stuff we just built. This folder contains:
- a bin folder with the binaries of the cross compiler
- a lib/gcc/i686-elf/4.9.0/ folder which contains a include folder and libgcc.a for linking it with your kernel