So how do we get it started?
As we need suitable tools to make stuff, what we need to do is building a tool chain.
Note that from here I will assume we are working in a *nix environment. If you are using Windows, good luck for you, because there are some extra steps waiting for you. I have no intention to start a war (once again) on the choose of OSes (since we are going to make a new one). But for developing, I guess the best choice goes to *nix.
It is recommended to follow the detailed instructions in osdev.org. Here are just some key steps.
0. Fetching packages
For people with experience of working with *nix, it is easy to guess that we are going to get a compiler or so. But please hold your haste of getting things done, what we actually need is a set of CROSS compilers, not a regular compiler that we use for making userspace applications.
But there is an exception. If you decide to make the whole system out of assembly, you can skip this chapter.
First, get the compiler tool chain tarballs from GNU. Our target is x86 (ix86) architecture, so choose wisely. We will need GCC and binutils for ix86 architecture.
You may wonder that since we are already living in x86, why do we ever need a new compiler to do the job? Because the compiler we know and love is specifically designed and optimized for our host OS. If we want to make something new with it, we will have to pass a glob of arguments to disable those optimizations. If that’s not bad enough, using these arguments may lead to a corrupted output that would never work correctly.
After finishing getting tarballs and extracting them to a proper place, we can start to compile and install.
Before we start to compile the cross compiler, we need to install these packages with system’s package manager:
- G++ (if building a version of GCC >= 4.8.0)
- GNU Make
- GNU Bison
- GNU GMP
- GNU MPFR
- GNU MPC
If there is development package (which name usually ends with -dev or -devel), you will have to also install the development package. If you don’t have any package manager, just get them from GNU and install it as how you usually do.
After install these packages, these commands should be usable:
You can consult
which for it.
2. The build
$PREFIX for installation prefix. (I would say
$HOME/opt/cross so you don’t need root permission to install and won’t screw up your host compiler.) And
$TARGET for build target. In this case it should be
i686-elf. Also remember set
$PATH to include cross compiler (
Build binutils first. The command is given below:
mkdir build && cd build && ../configure --target=$TARGET --prefix="$PREFIX" --with-sysroot --disable-nls --disable-werror && make && make install
If nothing goes wrong, get gcc built.
mkdir build && cd build && ../configure --target=$TARGET --prefix="$PREFIX" --disable-nls --enable-languages=c,c++ --without-headers && make all-gcc && make all-target-gcc && make install-gcc && make install-target-gcc
It may take a considerable time. Sit back and relax and pray for nothing would go wrong.
After everything was finished without any error, try to type
i686-elf-gcc. It should blame you for not providing any input. Don’t be sad for that — it works!
And that’s it. Have fun.