6 – Cross-Compiler (GCC, Linker, ELF) on Windows 10 using WSL (Windows Subsystem for Linux)

How to install GCC on WSL (Windows Subsystem for Linux)?

As you all are already aware about that I am developing an operating system. I like to use Windows as my main operating system. I love Windows and have been using it since Windows 3.11.

To develop an operating system, one need cross compiler – read more here – https://wiki.osdev.org/Why_do_I_need_a_Cross_Compiler%3F


On Windows, I did manage to get the cross compiler (gcc) working but I had a huge trouble with cross linker. I could not get gcc to link and also could not get ld to link object files which gives me cross compiled or cross linked files. I turned to Linux and got the gcc cross-compiler setup along with linker and it all started working. That’s when Windows Subsystem for Linux (WSL) came into my mind and I thought it will be worth giving it a try and see if I can get it to work on Windows and yes I did manage to make it work on Windows. If you want to do the same, please follow the steps that I have given below. Or you can also follow the video posted on YouTube.

If you would like to know how to setup Windows Subsystem for Linux on Windows 10, check out this blog post – Installing and Setting up Windows Subsystem for Linux on Windows 10 – Running Linux on Windows without Virtualization

YouTube Video:

Now comes an interesting part, where we are going to compile GCC compiler with a target of i386-elf which will make compiler and linker cross compiler. This we will be able to use in our next operating system tutorial.

Following steps Windows as well as Linux user needs to do:

Type following command:

sudo apt update
sudo apt install build-essential
sudo apt-get install gcc-multilib
sudo apt-get install libmpc-dev
sudo apt remove gcc

sudo nano /etc/apt/sources.list

This will open file in nano editor. At end of the file put below two lines:

deb http://dk.archive.ubuntu.com/ubuntu/ xenial main
deb http://dk.archive.ubuntu.com/ubuntu/ xenial universe

Now press Ctrl+X and then Press Y. This will save file.

Now type following commands one by one:

sudo apt update
sudo apt install g++-4.9
sudo apt install g++-4.9-multilib
sudo update-alternatives --remove-all gcc
sudo update-alternatives --remove-all g++
sudo ln -s /usr/bin/gcc-4.9 /usr/bin/gcc
sudo ln -s /usr/bin/g++-4.9 /usr/bin/g++
sudo ln -s /usr/include/asm-generic /usr/include/asm

NOTE: Two commands sudo update-alternatives –remove-all gcc and
sudo update-alternatives –remove-all g++ may give error, ignore them.

Ubuntu by default comes with gcc 7, as of now we are going to use gcc 4.9 and ubuntu default package sources does not have gcc 4.9, so we are modifying the source list and adding deb package list from which we will be able to get gcc 4.9. Also we are changing default gcc command link to use gcc 4.9.

Now that we have gcc 4.9 setup and running.. lets finally compile cross-compiler and linker. Run following commands:

export PREFIX="/usr/local/i386elfgcc"
export TARGET=i386-elf
export PATH="$PREFIX/bin:$PATH"

mkdir /tmp/src
cd /tmp/src
curl -O http://ftp.gnu.org/gnu/binutils/binutils-2.24.tar.gz
tar xf binutils-2.24.tar.gz
mkdir binutils-build
cd binutils-build
../binutils-2.24/configure --target=$TARGET --enable-interwork --enable-multilib --disable-nls --disable-werror --prefix=$PREFIX 2>&1 | tee configure.log
sudo make all install 2>&1 | tee make.log

Now the final ones:

cd /tmp/src
curl -O https://ftp.gnu.org/gnu/gcc/gcc-4.9.1/gcc-4.9.1.tar.bz2
tar xf gcc-4.9.1.tar.bz2
mkdir gcc-build
cd gcc-build
../gcc-4.9.1/configure --target=$TARGET --prefix="$PREFIX" --disable-nls --disable-libssp --enable-languages=c --without-headers
sudo make all-gcc
sudo make all-target-libgcc 
sudo make install-gcc 
sudo make install-target-libgcc 

Above command will take several minutes, approximately 10 to 15 minutes. Refer to screenshots given below to see what each output should look like:

Run last command:

ls /usr/local/i386elfgcc/bin

Now refer to the last screenshot in above gallery, if your output is similar to what is shown in screenshot. We have been successful in compiling and installing cross-compiler and linker on our machine.

Now if you want to use any command you can simply type:


or if you are on Windows and want to execute above command via command prompt, all you have to do is prefix it with wsl like below:

wsl /usr/local/i386elfgcc/bin/i386-elf-ld

and you should be able to see below output on Windows via command prompt:

Here we conclude this tutorial on how to setup Windows Subsystem for Linux on Windows 10. As well as Cross Compiler / Linker on Linux.

In next tutorial we will see how we can use this and build our first initial kernel. 😀

About the Author