Skip to content

Build a cross compiler for FOOS

Travor Liu edited this page Sep 8, 2018 · 3 revisions

This page will show you how to build a cross-compiler for FOOS. A cross compiler is a tool that helps compile program into other binary formats (e.g. build arm code on x86 machine and vice versa). This tutorial is going to build you a cross compiler that builds i386-elf code on your machine. To do this, you need to have gcc and binutils installed.

Variables

To help you understand what these code mean, I declare a few variables in the first place:

export TARGET=i386-elf
export PREFIX=/usr/local

TARGET is the binary format the cross compiler will build, PREFIX is where the compiler will be installed

Binutils

Binutils is a tool that helps create and manipulate binary files (binary utilities). A program's compilation done by gcc expects binutils to link executables.

To build binutils, we download and unpack its tarball from here, for example:

$ wget https://ftpmirror.gnu.org/gnu/binutils/binutils-2.30.tar.gz
$ tar xvf binutils-2.30.tar.gz

For future use, I create a new directory called build-binutils to keep the source from being dirty

$ mkdir build-binutils && cd build-binutils
$ ../binutils-2.30/configure --prefix=$PREFIX --target=$TARGET \
> --disable-nls --disable-werror --with-sysroot
$ make
# make install

disable-nls: Disable native language support so the compiler only outputs English, and it really reduces the time to build

disable-werror: werror basically turns warnings into errors, sometimes you OS's compilation contains some "friendly" warnings.

GCC

Now you have successfully built binutils, so let's look at gcc. Somehow, any versions later or before 5.2.0 won't be built successfully, so I choose gcc-5.2.0.tar.gz to download.

tar xvf gcc-5.2.0.tar.gz
$ mkdir build-gcc && cd build-gcc
$ ../gcc-5.2.0/configure --prefix=$PREFIX --target=$TARGET \
> --enable-languages=c --disable-nls --disable-multilib --without-headers
$ make all-gcc
$ make all-target-libgcc
# make install-gcc
# make install-target-libgcc

Test it out

If everything works fine, you should be building FOOS successfully like this:

$ git clone git@github.com:TravorLZH/foos.git
$ cd foos
$ make
Clone this wiki locally