Compiling the ARM Cortex M4 Toolchain Yourself

Nabil has a great blog post for compiling the ARM Cortex M3 toolchain yourself.

It appears that using his method for compiling the ARM toolchain allows you to make use of the FPU.

Nabil’s directions were great, but they only directly apply to Mac OS X. The German site UbuntuUsers also had a great article in their wiki that helped me get through some sticky places. Here are some directions for getting the ARM Cortex M4 toolchain built on Ubuntu:

First, we’ll need to get download some source packages:

You can do this by hand via the links below, either click on the project name to locate the latest releases yourself, or click on the package (.tar.gz) name to directly download the package I used:

binutilsbinutils-2.21.1a.tar.bz2
newlibnewlib-1.19.0.tar.gz
GCCgcc-4.6.2.tar.bz2
GDBgdb-7.3.1.tar.bz2

Or here is a quick set of wgets you could use:

[code lang=”bash”]
mkdir src
cd src
wget http://ftp.gnu.org/gnu/binutils/binutils-2.21.1a.tar.bz2
tar jxvf binutils-2.21.1a.tar.bz2
wget ftp://sources.redhat.com/pub/newlib/newlib-1.19.0.tar.gz
tar zxvf newlib-1.19.0.tar.gz
wget ftp://mirrors.kernel.org/gnu/gcc/gcc-4.6.2/gcc-4.6.2.tar.bz2
tar jxvf gcc-4.6.2.tar.bz2
wget http://ftp.gnu.org/gnu/gdb/gdb-7.3.1.tar.bz2
tar jxvf gdb-7.3.1.tar.bz2
[/code]

Second, let’s get some dependancies:

[bash]
$ sudo apt-get install flex bison libgmp3-dev libmpfr-dev libncurses5-dev
libmpc-dev autoconf texinfo build-essential libftdi-dev libexpat1 libexpat1-dev
[/bash]

Now, we’ll compile our toolchain:

Be sure to change /home/csamuelson/mat to the directory you would like to have the toolchain installed to.

Compile binutils

[code]
cd binutils-2.21.1/
./configure –target=arm-none-eabi –prefix=/home/csamuelson/mat –enable-interwork –enable-multilib –disable-nls –disable-libssp
make all
make install
cd ..
[/code]

Now start to compile GCC

We just get gcc bootstrapped (make all-gcc). Make sure –with-headers is set to the correct directory.

[code]
cd gcc-4.6.2/
mkdir objdir
cd objdir/
../configure –target=arm-none-eabi –prefix=/home/csamuelson/mat/ –enable-interwork –enable-multilib –enable-languages="c" –with-newlib –with-headers=../../newlib-1.19.0/newlib/libc/include/ –disable-libssp –disable-nls –with-system-zlib
make all-gcc
make install-gcc
cd ../..
[/code]

Compile newlib:

[code]
cd newlib-1.19.0/
./configure –target=arm-none-eabi –prefix=/home/csamuelson/mat –enable-interwork –enable-multilib –disable-libssp –disable-nls
make all
make install
cd ..
[/code]

Finish compiling GCC:

[code]
cd gcc-4.6.2/objdir/
make all
sudo make install
cd ../..
[/code]

Now make GDB:

[code]
cd gdb-7.3.1/
./configure –target=arm-none-eabi –prefix=/home/csamuelson/mat/ –enable-interwork –enable-multilib –disable-libssp –disable-nls
make all
sudo make install
[/code]

Some options explanations:
–prefix=/home/csamuelson/mat/: Set the install directory. I put it in a directory call /home/csamuelson/mat/ You will probably want to change this to a directory you want it installed in!
–enable-interwork: Allows ARM and Thumb code to be used
–enable-multilib: Build multible versions of some libs. E.g. one with soft float and one with hard
–disable-nls: Tells gcc to only support American English output messages
–disable-libssp: Don’t include stack smashing protection
–with-system-zlib: Fixes the error: configure: error: Link tests are not allowed after GCC_NO_EXECUTABLES

Hardware Floating Point
Now, when you run arm-none-eabi-gcc -print-multi-lib you will see support for the FPU:
[code]
$ arm-none-eabi-gcc -print-multi-lib
.;
thumb;@mthumb
fpu;@mfloat-abi=hard
[/code]

15 thoughts on “Compiling the ARM Cortex M4 Toolchain Yourself”

  1. This works great!

    Two minors that made it work for me:
    For binutils ./configure: Add –no-werror

    After first gcc install:
    Create a link between cc and gcc

  2. I recently picked up a F4DISCOVERY board and was playing with it. I had trouble with a call to strncpy() but just wrote my own version. When strncmp() started causing trouble, I tried rebuilding the tools as described here. But that didn’t help.
    The problem was that in spite of compiling with -mthumb, the code had a 32 bit form of the blx instruction for this call. Which is illegal for the thumb.
    I fixed that problem by rebuilding newlib. Not really knowing what to try I used –enable-thumb and –disable-interwork for configure and it works for now.

    1. Hello,
      I also got problem with the libraries which are not compiled in thumb mode, but in arm mode, even in /thumb directory.
      Recompiling using -enable-thumb and -disable-interwork does not work on my side.
      If anybody had a clue, I’m interested …
      Tx in advance, and tx for the tuto.

  3. I built my toolchain according your suggestions here and tried to integrate the Toolchain into Code::Blocks

    Now I’m using gcc with the following flags to create the object-files:
    -mthumb -mthumb-interwork -mlittle-endian -Wall -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=hard
    which works fine

    and the gcc to do the linking with the following flags:
    -o bin/Debug/STM32F4-Test.elf
    -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16 -mthumb-interwork -mthumb -Wall -mlittle-endian -T”/home/andre/code/STM32F4-Test/stm32_flash.ld” -Wl,-Map,STM32F4-Test.map -g

    but when it comes to the Linking I get the following:
    error: obj/Debug/main.c.o uses VFP register arguments, bin/Debug/STM32F4-Test.elf does not

    I hope you can give me a hint where the problem might be. (when I’m using -mfloat-abi=soft everything runs without any problems but it wouldn’t use the Hardware-FPU)

  4. hi,

    works fine =) i used it with
    binutils-2.22
    gcc-4.7.1
    newlib-1.20.0
    gdb-7.4

    the only think what makes a littl probs is the newlib-1.20.0 …

    after the ./configure –target=arm-none-e………
    you need to do a
    ln -s arm-none-eabi-gcc arm-none-eabi-cc
    (newlib use arm-none-eabi-cc …)

    and do a
    export PATH=$PATH:/your_install/bin

    so it works fine =)

    greez
    Manuel

  5. Hi. I am getting an error when installing NewLib. It says: .././libgloss/arm/crt0.S:37: Error: unknown architecture `armv6s-m’
    What does this mean? Can you please help me with this.

    Thanks,
    Shiv

  6. hi sir .., i got the following error while compiling newlib 1.19.0:

    after hitting “make all” command i got following error

    /bin/bash: arm-none-eabi-cc: command not found
    make[5]: *** [lib_a-dummy.o] Error 127

    please help me to resolve this issue.

  7. Hi, When i compile gcc-4.6.2 i got these errors,

    configure: error: Building GCC requires GMP 4.2+, MPFR 2.3.1+ and MPC 0.8.0+.
    Try the –with-gmp, –with-mpfr and/or –with-mpc options to specify
    their locations. Source code for these libraries can be found at
    their respective hosting sites as well as at
    ftp://gcc.gnu.org/pub/gcc/infrastructure/. See also
    http://gcc.gnu.org/install/prerequisites.html for additional info. If
    you obtained GMP, MPFR and/or MPC from a vendor distribution package,
    make sure that you have installed both the libraries and the header
    files. They may be located in separate packages.

    i try to resolve those with using this site instructions
    http://stackoverflow.com/questions/9450394/how-to-install-gcc-from-scratch-with-gmp-mpfr-mpc-elf-without-shared-librari

    but still getting same problem.
    Please solve this problem.

    Thanks

  8. Hi, When i compile gcc-4.6.2 i got these errors,

    configure: error: Building GCC requires GMP 4.2+, MPFR 2.3.1+ and MPC 0.8.0+.
    Try the –with-gmp, –with-mpfr and/or –with-mpc options to specify
    their locations. Source code for these libraries can be found at
    their respective hosting sites as well as at
    ftp://gcc.gnu.org/pub/gcc/infrastructure/. See also
    http://gcc.gnu.org/install/prerequisites.html for additional info. If
    you obtained GMP, MPFR and/or MPC from a vendor distribution package,
    make sure that you have installed both the libraries and the header
    files. They may be located in separate packages.

    i try to resolve those with using this site instructions
    http://stackoverflow.com/questions/9450394/how-to-install-gcc-from-scratch-with-gmp-mpfr-mpc-elf-without-shared-librari

    but still getting same problem.
    Please solve this problem.
    Advance Thanks.

  9. Hi ATVREDDY,

    I installed succesfully with arm tool chain as mentioned by you. But I am facing the issue while compile & linker stage. Could you please share the commands to:
    1) compile
    2) link
    3) to henerate hex & elf file
    4) And also command to dump the log file

    Thanks,
    Deepak

Leave a Reply