The steps to build a cross-compiler are somewhat covered here and here. In theory, building a cross-compiler is a pretty straightforward process:
- Cross compile binutils, to get things like as (assembler), ld (linker), nm (list object symbols), etc.
- Cross compile gcc, which gives you a C and C++ compiler.
- Cross compile newlib, which gives you a minimal libc-like environment to program in.
Now onto the steps needed to build the cross compiling environment. We first need to make sure certain tools are installed. We'll install the development tools through yum:
Next we fetch the relevant versions of the packages:
$ sudo yum install gcc make tar wget bzip2 gmp-devel \ mpfr-devel libmpc-devel gcc-c++ texinfo ncurses-devel
Next we set some environment variables. This isn't strictly necessary, but will help us reduce errors in the following steps:
$ mkdir ~/cross-src $ cd ~/cross-src $ wget ftp://ftp.gnu.org/gnu/binutils/binutils-2.24.tar.gz $ wget ftp://ftp.gnu.org/gnu/gcc/gcc-4.8.2/gcc-4.8.2.tar.bz2 $ wget ftp://sources.redhat.com/pub/newlib/newlib-2.0.0.tar.gz
The TOPDIR environment variable is the directory in which the sources are stored. The TARGET environment variable is the architecture that we want our compiler to emit code for. For ARM chips without an operating system (like the STM32), we want arm-none-eabi. The PREFIX environment variable is the location we want our cross-compile tools to end up in; feel free to change this to something more suitable. The BUILDPROCS environment variable is the number of processors that we can use; we will use all of them while building to substantially speed up the build process. Finally, we need to add the location of the cross-compile binaries to our PATH so that later building stages can find it.
$ export TOPDIR=~/cross-src $ export TARGET=arm-none-eabi $ export PREFIX=~/opt/cross $ export BUILDPROCS=$( getconf _NPROCESSORS_ONLN ) $ export PATH=$PREFIX/bin:$PATH
Now we can start building. We first need to build binutils:
Basically we are unpacking binutils, doing an out-of-tree build (recommended), and then installing it. The flags to configure deserve some explanation. The --target flag tells binutils what target you want the tools to build for; that is, what kind of code will be emitted by the code. In our case, we want ARM with no operating system. The --prefix flag tells binutils that we want our tools to be installed to $PREFIX. The --enable-interwork flag allows binutils to emit a combination of ARM and THUMB code; if you don't know what that is, don't worry about it for now. Finally, the --disable-nls flag tells binutils not to build translation files, which speeds up the build. Assuming this step went fine on your development machine, there should be a set of tools in ~/opt/cross/bin (or whatever your top-level output directory is) called arm-none-eabi-*. If this didn't work, then you might want to try a newer or older version of binutils; you can't proceed any further without this working.
$ cd $TOPDIR $ tar -xvf binutils-2.24.tar.gz $ mkdir build-binutils $ cd build-binutils $ ../binutils-2.24/configure --target=$TARGET --prefix=$PREFIX \ --enable-interwork --disable-nls $ make -j$BUILDPROCS $ make install
With binutils built, we can now move on to gcc:
Here we are unpacking gcc and newlib (which is required for building gcc), doing an out-of-tree build of the initial part of gcc, and then installing it. The flags to configure deserve some explanation. The --target flag tells gcc what target you want the tools to emit code for. The --prefix flag tells gcc that we want our tools to be installed to $PREFIX. The --enable-interwork flag allows gcc to emit a combination of ARM and THUMB code. The --disable-nls flag tells gcc not to build translation files, which speeds up the build. The --enable-languages flag tells gcc which compilers we want it to build; in our case, both the C and C++ compilers. The --without-headers --with-newlib and --with-headers flags tells gcc that it not to use internal headers, but rather to use newlib and the headers from newlib. Assuming this step finished successfully, there should be a file called ~/opt/cross/bin/arm-none-eabi-gcc, which is the initial compiler. Again, if it didn't work, then you might want to try a newer or older version of gcc; you can't proceed any further without this.
$ cd $TOPDIR $ tar -xvf newlib-2.0.0.tar.gz $ tar -xvf gcc-4.8.2.tar.bz2 $ mkdir build-gcc $ cd build-gcc $ ../gcc-4.8.2/configure --target=$TARGET --prefix=$PREFIX \ --enable-interwork --disable-nls --enable-languages="c,c++" \ --without-headers --with-newlib \ --with-headers=$TOPDIR/newlib-2.0.0/newlib/libc/include $ make -j$BUILDPROCS all-gcc $ make install-gcc
With the initial compiler built, we can now build newlib:
Since we've already unpacked newlib, we skip that step. Here we are doing an out-of-tree build of newlib, using the compiler that we built in the last step. The configure flags have the same meaning as previously.
$ cd $TOPDIR $ mkdir build-newlib $ cd build-newlib $ ../newlib-2.0.0/configure --target=$TARGET --prefix=$PREFIX \ --enable-interwork $ make -j$BUILDPROCS $ make install
With newlib built, we can now go back and finish the build of gcc (the last step!):
This finishes the gcc build, and installs it to $PREFIX. That's it! You should now have a $PREFIX directory full of tools and headers useful for building code to run on the STM32.
$ cd $TOPDIR/build-gcc $ make -j$BUILDPROCS $ make install
Update Jan 8, 2014: Updated the formatting so it is more readable.