kr651129
February 14th, 2012, 04:11
I wrote this how to because I'm experimenting with writing my own operating system and I needed to build a cross compiler for this. I had a hard time learning how to do this but it's quite simple.
You will need to download the following packages from http://ftp.gnu.org/gnu/
binutils 2.22 (http://ftp.gnu.org/gnu/binutils/binutils-2.22.tar.gz)
gcc 4.6.2 (http://ftp.gnu.org/gnu/gcc/gcc-4.6.2/gcc-4.6.2.tar.gz)
(this is only for C, if you want support for C++ or any other language you will need to download it from http://ftp.gnu.org/gnu/gcc/)
gmp 5.0.4 (http://ftp.gnu.org/gnu/gmp/gmp-5.0.4.tar.bz2)
mpfr 3.1.0 (http://ftp.gnu.org/gnu/mpfr/mpfr-3.1.0.tar.gz)
mpc 0.8.2 (http://www.multiprecision.org/mpc/download/mpc-0.8.2.tar.gz)
Extract the files
$ tar xvf binutils-2.22.tar.gz
$ tar xvf gcc-4.6.2.tar.gz
$ tar xvf gmp-5.0.4.tar.bz2
$ tar xvf mpfr-3.1.0.tar.gz
$ tar xvf mpc-0.8.2.tar.gz
We want to create build directories so we don't clutter our source
$ mkdir build-binutils build-gcc build-gmp build-mpfr build-mpc
Let's set our environment variables
export PREFIX=/usr/local/cross
export TARGET=i586-elf
Obviously this is for i586-elf, you can make the target for whatever your needs are. This is beyond the scope of this howto but I do know that if you are trying to build a 64 bit elf compiler you can substitute i586-elf with x86_64-elf you should be able to elf with arm or whatever it is your trying to build for.
First we want to build binutils
$ cd build-binutils
$ ../binutils-2.22/configure --target=$TARGET --prefix=$PREFIX --disable-nls
$ make all
# make install
Now that binutils is installed we will need to install gcc's dependencies
$ cd ..
$ cd build-gmp
$ ../gmp-5.0.4/configure --prefix=$PREFIX
$ make
# make install
$ cd ..
$ ../mpfr-3.1.0/configure --prefix=$PREFIX
$ make
# make all
Now that everything has been built we can take care of gcc
$ cd ..
$ cd build-gcc
$ ../gcc-4.6.2/configure --target=$TARGET --prefix=$PREFIX --disable-nls --enable-languages=c --without-headers --with-gmp=$PREFIX --with-mpc=$PREFIX --with-mpfr=$PREFIX
$ gmake all
# gmail install
This installs your cross compiler under /usr/local/cross.
After this is all done you should build a C Library but that's for another time. Most of this information was pieced together from http://wiki.osdev.org/GCC_Cross-Compiler but made to fit for FreeBSD.
This is my first EVER howto so please, be kind ;) Give me any suggestions and I'll change this as errors pop up.
-Kris
You will need to download the following packages from http://ftp.gnu.org/gnu/
binutils 2.22 (http://ftp.gnu.org/gnu/binutils/binutils-2.22.tar.gz)
gcc 4.6.2 (http://ftp.gnu.org/gnu/gcc/gcc-4.6.2/gcc-4.6.2.tar.gz)
(this is only for C, if you want support for C++ or any other language you will need to download it from http://ftp.gnu.org/gnu/gcc/)
gmp 5.0.4 (http://ftp.gnu.org/gnu/gmp/gmp-5.0.4.tar.bz2)
mpfr 3.1.0 (http://ftp.gnu.org/gnu/mpfr/mpfr-3.1.0.tar.gz)
mpc 0.8.2 (http://www.multiprecision.org/mpc/download/mpc-0.8.2.tar.gz)
Extract the files
$ tar xvf binutils-2.22.tar.gz
$ tar xvf gcc-4.6.2.tar.gz
$ tar xvf gmp-5.0.4.tar.bz2
$ tar xvf mpfr-3.1.0.tar.gz
$ tar xvf mpc-0.8.2.tar.gz
We want to create build directories so we don't clutter our source
$ mkdir build-binutils build-gcc build-gmp build-mpfr build-mpc
Let's set our environment variables
export PREFIX=/usr/local/cross
export TARGET=i586-elf
Obviously this is for i586-elf, you can make the target for whatever your needs are. This is beyond the scope of this howto but I do know that if you are trying to build a 64 bit elf compiler you can substitute i586-elf with x86_64-elf you should be able to elf with arm or whatever it is your trying to build for.
First we want to build binutils
$ cd build-binutils
$ ../binutils-2.22/configure --target=$TARGET --prefix=$PREFIX --disable-nls
$ make all
# make install
Now that binutils is installed we will need to install gcc's dependencies
$ cd ..
$ cd build-gmp
$ ../gmp-5.0.4/configure --prefix=$PREFIX
$ make
# make install
$ cd ..
$ ../mpfr-3.1.0/configure --prefix=$PREFIX
$ make
# make all
Now that everything has been built we can take care of gcc
$ cd ..
$ cd build-gcc
$ ../gcc-4.6.2/configure --target=$TARGET --prefix=$PREFIX --disable-nls --enable-languages=c --without-headers --with-gmp=$PREFIX --with-mpc=$PREFIX --with-mpfr=$PREFIX
$ gmake all
# gmail install
This installs your cross compiler under /usr/local/cross.
After this is all done you should build a C Library but that's for another time. Most of this information was pieced together from http://wiki.osdev.org/GCC_Cross-Compiler but made to fit for FreeBSD.
This is my first EVER howto so please, be kind ;) Give me any suggestions and I'll change this as errors pop up.
-Kris