How to build a standalone GCC cross compiler using Melas
Building GNU GCC cross-compilers can be a somewhat dark art, and can be quite complicated if you are trying to do it for the first time, and especially frustrating if it is something that you need to do in order to reach some very different goal. Numerous tutorials have been written about how to do this (for various versions of GCC, Binutils and glibc/uclibc), as well as a number of tools, the most famous of which is probably Dan Kegel’s crosstool.
Melas is a generic build assistant tool, which can make this procedure quite easy in some scenarios, given the right mls file that describes the procedure. This post explains how to easily make a gcc cross-compiler with Melas using an existing mls definition file.
Prerequisites
- A working GNU C Compiler (gcc) setup for your host machine that can produce functioning executables
- An active internet connection to download the necessary source code files
- Melas installed on your computer, and mls-build in your path.
To install Melas, download and install the latest version first from the Melas project page.
Toolchain versions
A complete GCC (cross-)compiler toolchain consists of a number of components; namely binutils, gcc itself, and libc (usually glibc or uclibc). Each of these is released and versioned separately, and sometimes a specific mix/match of these is preferred or required. Luckily, the procedure to build is often roughly the same, and hopefully no great differences will be introduced by different versions. The exact versions to build can be configured in the mls file, a sample of which can be downloaded from the Melas project site at http://melas.ossproject.org/gcc4-cross.mls (this one is borrowed from the Igelle OS source tree).
As of this writing, the above file defaults to the following versions:
- Binutils 2.18
- GCC 4.2.4
- Glibc 2.7
- uClibc 0.9.29 (experimental)
As mentioned, uClibc support in the mls file is experimental, and may not work properly. All patches are obviously quite welcome to correct any issues :). If other versions are preferred, this can be achieved by editing the mls file accordingly.
Target architecture
Since we are building a cross-compiler, it is essential to be aware of the target architecture, which is usually expressed by a “target tuple”, eg. “i486-unknown-linux-gnu”, or “armv4t-unknown-linux-gnueabi”, etc. Any suitable target architecture can be chosen for this procedure, although much of them haven’t probably been tested. All feedback for this will be most welcome.
The supplied mls file has a particular speciality, though: if the string “uclibc” is supplied as part of the tuple, then a uclibc toolchain build is attempted. Otherwise, a glibc build is assumed. This allows, for instance, tuples like “armv4t-uclibc-linux-gnueabi” or “i486-uclibc-linux-gnu” to be used as targets for uclibc -based toolchains.
Executing the build
Once all the issues above have been considered and addressed, executing the actual build is quite easy:
GCCARCHTUPLE=your-arch-tuple mls-build --run=rebuild \
http://melas.ossproject.org/gcc4-cross.mls
Here, you must obviously substitute the “your-arch-tuple” to your desired tuple, as mentioned above. Then sit back and relax, this will take quite some time, but happily the whole process is managed for you (binutils get compiled, then a bootstrap gcc, then libc, then gcc again, etc.).
Obviously, if you have downloaded the mls file and made your own modifications, you must point mls-build to your modified mls file instead of the URL above.
The end-result
After a successful build, you will end up with a .deb file in your current directory representing your full cross-compiler toolchain. This is complete with gcc, g++, binutils (linker, assembler, etc.), as well as header files and link libraries for developing working binaries for the target.
(You can actually even extract the whole libc from within the packet to deploy on the target to make a running system, however if this is what you are after, it is probably best to look at Igelle, which includes a build system (based on Melas also) to make a complete functioning Linux system.)
Then, simply install the .deb file and start using the new cross-compiler; handy links are installed in /usr/bin so that the compiler is automatically in your PATH. Simply run [your-arch-tuple]-gcc to compiler, eg.
armv4t-glibc-linux-gnueabi test.c -o test-arm
(to compile an ARM program).
Expected issues
As mentioned, uclibc support has not been tested, and probably doesn’t work well on all targets. Similarly, different versions of the toolchain components have not been tested. All feedback on any successes, failures or other experiences is most welcome, as well as obviously initiatives for further improvements on this.




[...] recorded first by colorofmoney91 on 2008-08-28→ How to build a standalone GCC cross compiler using Melas [...]