Cross compiling an i386- or i686-elf Win32 toolchain is an outstandingly complicated and painful process. Both Binutils and GCC fail to properly articulate the extent of software dependencies required to build them, resulting in a litany of spurious and confusing error messages being emitted during compilation. Even once you do have the required software dependencies in place, you will still run into roadblocks due to anomalies in how GCC performs cross compilation.
This repo provides a set of precompiled binaries to those who want to use get what they came for and move on (an i686-elf toolchain that, unlike others on the internet, includes cc1 and GDB), as well as a set of instructions for those that would like to build these things themselves. Also featured are a set of instructions for those that wish to install these tools on Mac OS X or Linux.
i686-elf-tools.sh will download
- GCC 7.1.0
- Binutils 2.28
- GDB 8.0
If you would like to change these versions, open the script in your favourite text editor and change the values of
GDB_VERSION. Instead of using MinGW32 or MinGW64, MXE is used, providing us with an awesome Win32 toolchain that always produces statically linked binaries that just work (and don't need random MinGW DLLs).
Note: if you already have MXE installed,
i686-elf-tools.sh won't add MXE to your PATH nor make MXE's version of GCC. Please ensure the MXE bin folder is on your path and that MXE's gcc has been built (run
make gcc in your MXE install directory), else you will experience issues during compilation.
Install a Debian based operating system, ideally 32-bit (i386). This procedure has successfully been performed on Debian 9.5 i386 and Ubuntu 18.04 64-bit (amd64).
Remove the CD-ROM source from
If you are running Ubuntu, you may need to modify
multiversein addition to
main. If you simply do the default Ubuntu install, these appear to be included by default.
-deb http://archive.ubuntu.com/ubuntu bionic main +deb http://archive.ubuntu.com/ubuntu bionic main universe multiverse deb http://archive.ubuntu.com/ubuntu bionic-security main deb http://archive.ubuntu.com/ubuntu bionic-updates main
Run the following commands.
sudo -sis optional, however if you are not running as root you will get repeated password request prompts during the course of the execution.
sudo -s wget https://raw.githubusercontent.com/lordmilko/i686-elf-tools/master/i686-elf-tools.sh chmod +x ./i686-elf-tools.sh ./i686-elf-tools.sh
A full run takes approximately 2 hours on a 4xCPU virtual machine.
When the script completes you will have two zip files containing your i686-elf toolchain
If you experience any issues, you can specify one or more command line arguments to only perform certain parts of the script. The following arguments are supported
- zip - zip it all up!
- linux - compile the linux toolchain only
- win - compile the windows toolchain only
# Compile binutils and gcc only ./i686-elf-tools.sh binutils gcc
win argument should only be used if the linux toolchain has already been compiled and you're experiencing issues with the Win32 part.
Logs are stored for each stage of the process under ~/build-i686-elf/build-xyz/xyz_stage.log
If you attempt to run
configure commands manually that depend on components of the linux i686-elf toolchain, ensure
~/build-i686-elf/linux/output/bin is on your path, else you may get errors about binaries being missing.
Mac OS X
Installing an i386-elf toolchain on Mac OS X is an outstandingly simple process
- Install Brew
- Download the i386-elf recepies
/usr/local/Library/Formula. As these formulae depend on one another, attempting to execute these directly with
brew install i386-elf-gcc.rb, etc will fail.
brew install i386-elf-binutils,
brew install i386-elf-gccand
brew install i386-elf-gdb
Extract the contents of
i686-elf-tools-linux.zip somewhere. By default GCC installs them under
To compile a newer i686-elf toolchain, invoke
i686-elf-tools.sh as follows
Why would I want to use this?
For building your own Operating System, of course!
How do I install this on Windows?
i686-elf-tools-windows.zip to your PC, all necessary programs can be found in the
bin folder. You can then put this folder in your
PATH, or simply browse to the programs in this folder manually. When running these programs, it is important to make sure all of the subfolders are kept together, as files outside of the bin folder are required for certain programs (such as GCC).
Does this include libgcc?
Seems so! For more information see the section How on earth did you compile libgcc? below.
Can I use MSYS/MSYS2/MinGW/MinGW/MinGW-w32/MinGW-w64/Cygwin, etc to do this?
No. But you can try. I got all sorts of crazy errors I was simply unable to resolve when I was looking at solutions to compile these tools. I have successfully compiled on Windows in the past, however there have been two issues with this:
- Executables had dependencies on MinGW/Cygwin libraries (most likely as I just didn't know how to statically link)
- GDB would randomly quit whenever I tried to type a command
Can I use
$DISTRO instead of Debian?
I originally tried to use CentOS 7 64-bit, however along the way I encountered various issues potentially attributable to bitness, resulting in my switching to a 32-bit OS to simplify troubleshooting. CentOS 7 32-bit cannot be used, as all the packages required by MXE are not available on yum.
If you are determined not to use Debian (or another Debian derivitive), please see the prerequisites for MXE. Note: you may need additional packages to these to successfully compile gcc, e.g. texinfo, readline-devel, etc. Google any error messages you get to reveal the appropriate package you need to install.
When running these steps manually and running
make for binutils I get an error GCC_NO_EXECUTABLES
The path to the compiler specified as
configure cannot be found on your
PATH. Update your
.bashrc and login/logout.
When running these steps manually I get i686-elf-gcc: command not found
This is caused by two bugs(?) in the GCC Makefile
- The file
makeis looking for is called
- To create
i686-elf-gccyou must compile a toolchain for linux first
You can try and hack the GCC makefile to execute
wine ./xgcc.exe instead, however you'll still run into issues when you try and compile libgcc. If you compile a linux i686-elf toolchain first, all your issues go away. Is this the correct way to do things? Who knows.
How on earth did you compile libgcc?
One does not simply
make all-target-libgcc for
host=MinGW32. The reason for this is that when libgcc is configured, both its target and host are set to i686-elf. As a result, libgcc's makefile will look for a host=linux, target=1686-elf
i686-elf-gcc, will only find your MinGW32
i686-elf-gcc.exe from your previous
make all-gcc (which it obviously can't execute) and thus will fail.
Attempting to trick the makefile by creating shell scripts on your path named after the missing binaries that internally invoke
wine <mingw32 version> does not work (you get strange errors on during compilation).
The solution to this therefore is to first compile a linux i686-elf toolchain, followed by the MinGW32 toolchain we're actually interested in. This helps solve other bugs in the compilation process, such as GCC attempting to perform selftests on your MinGW32 cross compiler (which won't work) yet looking for i686-elf-gcc instead (which wouldn't exist).
One would expect libgcc would have host=<the actual host> instead of i686-elf and thus generate a file
libgcc.dll for mingw32, but from what I've found that doesn't appear to be the case. If you do have issues with GCC not interfacing with libgcc properly, let me know!