Heya,
Note: you can also adapt this tutorial for any other OS to create your own toolchain!
For my latest project,

, I had to find a way to compile my stuff for FreeBSD.
My problems:
1) I wasn't able to compile my project on my server which runs FreeBSD
2) I couldn't run the bot on my server using FreeBSD's Linux emulation because it was compiled for 64bit
3) How the fuck could I build a working toolchain?
It was easier than I expected. Much easier.
After my first ~5 trys (felt like >9000), I finally got my toolchain working using GCC 4.7.3 and binutils 2.23.1.
What do we need?
- All required sources (GCC, binutils, GMP, MPFR, MPC)
- FreeBSD header files
- FreeBSD stubs
- FreeBSD libc
First, acquire all the required sources.
GCC 4.7.3:

binutils 2.23.1:

MPC 1.0.1:

MPFR 3.1.2:

GMP 5.1.1:
Create a directory in which your toolchain should reside (I used /usr/freebsd-gcc)
Move all your downloaded files into a temporary folder, I used /usr/freebsd-gcc/tmp for that.
Extract them (.tar.bz2: tar xfj ; .tar.gz: tar xfz ).
Now you'll find (almost) all the needed sources for your toolchain. What's missing? Yep, the FreeBSD sources!
To make it easier for you, I've uploaded them all to my server:
Just download them for now, don't extract them anywhere yet.
Now we can go on with compiling the binutils.
Go to the directory where your downloaded archives are in and type the following:
Code:
tar xfz binutils-2.23.1.tar.gz
mkdir -p build/binutils
cd build/binutils
../../binutils-2.23.1/configure --enable-libssp --enable-gold --enable-ld --target=x86_64-pc-freebsd7.4 --prefix=/usr/freebsd-gcc
make
make install
This will build and install binutils to /usr/freebsd-gcc/.
Now you should extract the FreeBSD files you've downloaded earlier (fbsd7.4-libs-headers.tar.bz2).
Move the two folders in there (lib and include) to /usr/freebsd-gcc/x86_64-pc-freebsd7.4/.
After doing this, you're ready for compiling GCC!
First, we'll need to compile GMP as it's a necessary library for us.
Go back to the tmp directory in which your downloaded stuff is and do the following:
Code:
tar xfj gmp-5.1.1.tar.bz2
mkdir -p build/gmp
cd build/gmp
../../gmp-5.1.1/configure --prefix=/usr/freebsd-gcc --enable-shared --enable-static --enable-mpbsd --enable-fft --enable-cxx --host=x86_64-pc-freebsd7.4
make
make install
This will compile and install GMP 5.1.1.
Now you'll need MPFR:
Code:
tar xfj mpfr-3.1.2.tar.bz2
mkdir -p build/mpfr
cd build/mpfr
../../mpfr-3.1.2/configure --prefix=/usr/freebsd-gcc --with-gnu-ld --with-gmp=/usr/freebsd-gcc --enable-static --enable-shared --host=x86_64-pc-freebsd7.4
make
make install
This will compile and install MPFR 3.1.2.
And.. now.. the last thing before you can go on with compiling your beloved GCC: compile and install MPC:
Code:
tar xfz mpc-1.0.1.tar.gz
mkdir -p build/mpc
cd build/mpc
../../mpc-1.0.1/configure --prefix=/usr/freebsd-gcc --with-gnu-ld --with-gmp=/usr/freebsd-gcc --with-mpfr=/usr/freebsd-gcc --enable-static --enable-shared --host=x86_64-pc-freebsd7.4
make
make install
After doing all this, you're ready to compile GCC 4.7.3.
Because we do not want to replace our system stuff, we'll install GCC inside our toolchain directory.
Code:
tar xfj gcc-4.7.3.tar.bz2
mkdir -p build/gcc
cd build/gcc
../../gcc-4.7.3/configure --without-headers --with-gnu-as --with-gnu-ld --enable-languages=c,c++ --disable-nls --enable-libssp --enable-gold --enable-ld --target=x86_64-pc-freebsd7.4 --prefix=/usr/freebsd-gcc --with-gmp=/usr/freebsd-gcc --with-mpc=/usr/freebsd-gcc --with-mpfr=/usr/freebsd-gcc --disable-libgomp
LD_LIBRARY_PATH=/usr/freebsd-gcc/lib make
make install
This will take several minutes!
After this is done, you're ready to use your FreeBSD-GCC under Linux!
Go on and test it!
Create a file 'test.c':
Code:
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("It works!");
return 0;
}
Now try compiling it using your Linux GCC:
Code:
gcc test.c -o test-linux
file test-linux
And now using your Linux G++
Code:
g++ test.c -o test-linux-++
file test-linux-++
(should return this for Linux)
Code:
test-linux: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=0xe6069259f37d48488b519640f5136e2bd85950de, not stripped
Worked? Alright.. let's hope it's the same for our FreeBSD GCC!
Code:
LD_LIBRARY_PATH=/usr/freebsd-gcc/lib /usr/freebsd-gcc/bin/x86_64-pc-freebsd7.4-gcc test.c -o test-freebsd
file test-freebsd
Aaaand for our G++:
Code:
LD_LIBRARY_PATH=/usr/freebsd-gcc/lib /usr/freebsd-gcc/bin/x86_64-pc-freebsd7.4-g++ test.c -o test-freebsd-++
file test-freebsd-++
(should return this for FreeBSD)
Code:
test-freebsd: ELF 64-bit LSB executable, x86-64, version 1 (FreeBSD), dynamically linked (uses shared libs), for FreeBSD 7.4, not stripped
If everything worked well, you should have 2 Linux binaries and 2 FreeBSD binaries

Good luck!
Best regards, Nico
Used
for reference