24ea HOWTO: Build a cross compiler [Archive] - The FreeBSD Forums

PDA

View Full Version : HOWTO: Build a cross compiler


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

vand777
February 14th, 2012, 11:35
I'm experimenting with writing my own operating system
Hi Kris,

This is quite a challenging project... Will it be written from scratch or will derive from one of existing operating systems?

ssh2
February 14th, 2012, 14:24
kr651129 why not using ports for install this packages?

And may be you can write howto crosscompile binaries for windows on freebsd FreeBSD, for example x264, ffmpeg and other useful utilities?

kr651129
February 14th, 2012, 15:31
vand777 -- It will be from scratch, I've been working on it for the past couple months now. I've got a basic boot up with some standard functions, nothing fancy yet. Most of my work has been research and planning so far.

ssh2 -- I didn't use the ports because I'm not used to them yet. It was easier for me just to compile everything. I was Ubuntu as my primary OS so there is a little bit of learning for me to do. Those are good ideas that I might look into in the future.

DutchDaemon
February 15th, 2012, 00:33
Ports = compiling everything.

vand777
February 16th, 2012, 01:22
vand777 -- It will be from scratch, I've been working on it for the past couple months now. I've got a basic boot up with some standard functions, nothing fancy yet. Most of my work has been research and planning so far.


Thanks! And good luck!

DevSolar
February 16th, 2012, 15:23
kr651129 why not using ports for install this packages?

And may be you can write howto crosscompile binaries for windows on freebsd FreeBSD, for example x264, ffmpeg and other useful utilities?

The 'mingw' port is what you're looking for.

DevSolar
February 16th, 2012, 15:26
Most of this information was pieced together from http://wiki.osdev.org/GCC_Cross-Compiler but made to fit for FreeBSD.

It would actually be very helpful if you could point out what exactly it was that had to be "made to fit for FreeBSD", i.e. why the original tutorial didn't work for you. Because, that way, we (speaking for the people at osdev.org) could adapt our tutorial so a seperate "FreeBSD version" is not necessary.

debguy
April 13th, 2012, 00:19
gcc that I know of is found on GNU Linux (gnu.org) or free software foundation (FSF) (Savannah). The steps can get recursive if you're building for an all new OS / platform it depends :) The documentation for gcc fully describes how many times you may need to pre-re-compile gcc.

Both have copies of the latest GNU compilers (also fortran, binutils) and instructions how to build the set for _any unix / platform (this part you may need to hack in or start with a ./configure your OS distro has pre-hacked - only a few are provided pre-done).

However its instructions are more complicated than ./configure if you wish to build for another platform (see the instructions for compiling gcc).

DutchDaemon
April 13th, 2012, 01:52
It's a FreeBSD port, no need for all the voodoo.

lang/gcc GNU Compiler Collection 4.6
lang/gcc34 GNU Compiler Collection 3.4
lang/gcc42 GNU Compiler Collection 4.2
lang/gcc44 GNU Compiler Collection 4.4
lang/gcc46 GNU Compiler Collection 4.6
lang/gcc47 GNU Compiler Collection 4.7
lang/gcc48 GNU Compiler Collection 4.8

solskogen
April 13th, 2012, 09:48
binutils won't compile with FreeBSD's make. You must use gmake.

0