Cross-Compiling for ARM

1. Introduction

Since the STM32 Discovery is based on a ARM Cortex M3 micro controller, a compiler able to produce binaries for this target is needed for developing programs for this board. In this article I will briefly describe which options are available for installing such a tool chain, and how you easily set them up on a Debian based Linux.

2. Summery

We will first have a quick look on some of the possible options available:

  • Install the gcc cross compiler binary suite provided by the angstrom distribution
  • Install codesourceries cross compiler binary suite
  • Compiler your own gcc cross compiler
  • Compile on a Linux system running on a ARM processor (to name a few: Toshiba AC100, Beagleboard, Pandaboard, Sheevaplug, …)

Which option you choose is completely left to you. They all provide the same result: a cross compiler, able to produce code for the ARM Cortex M3 target.

In the following sections, I will detail each of the above options.

3. Angstrom Cross Compiler Suite

Download a suitable binary distribution for your host architecture you are going to install the cross compiler to from here. E.g. if you are on a i686 32Bit Linux, you will need to download the “angstrom-YYYY.MM-i686-linux-armv7a-linux-gnueabi-toolchain.tar.bz2″ archive.

Extract the binary (as user root):

tar -jxvf angstrom-YYYY.MM-i686-linux-armv7a-linux-gnueabi-toolchain.tar.bz2 -C /

Add the binary directory “/usr/local/angstrom/arm/bin” to your path by appending the following entry to your “~/.profile”:

PATH="/usr/local/angstrom/arm/bin:$PATH"

And add the binary directory to your path (e.g. by adding an entry to “~/.profile”). After the binary directory is in your path, check if the following works:

After the binary directory is in your path, check if the following works:

arm-angstrom-linux-gnueabi-gcc -v

4. CoudeSourcery Cross Compiler Suite

Download a suitable binary distribution for your host architecture you are going to install the cross compiler to from here. Click the link “Sourcery G++ Lite YYYY.MM-VV for ARM EABI”, then download the “IA32 GNU/Linux TAR”.

Extract the binary to “/opt” (as user root):

tar -jxvf arm-YYYY.MM-VV-arm-none-eabi-i686-pc-linux-gnu.tar.bz2 -C /opt

Add the binary directory “/opt/arm-2011.03/bin” to your path by appending the following entry to your “~/.profile”:

PATH="/opt/arm-2011.03/bin:$PATH"

After the binary directory is in your path, check if the following works:

arm-none-eabi-gcc --version

5. Compile Your Own gcc Cross Compiler

Uwe Hermann did a great script which helps you to compile your own ARM cross compiler tool chain. See the instructions on his page for details.

6. Compile on a ARM Based Linux

To cross compile for the ARM Cortex M3 from an ARM based Linux the only thing you need is to install the gcc provided by your distribution. For example I use a Cortex A9 based Toshiba AC100 netbook for compiling my binaries for the STM32. The only thing to take care of is, that most of the source code examples you find on the internet, assume that you do NOT compile on a ARM based host. Thus in most cases you must remove the cross-compiler prefixes from the make files.

So if you come across something like:

CC = arm-none-eabi-gcc

or

CC = arm-elf-gcc

or

CC_PREFIX = arm-elf-

just remove the “arm-elf-” or “arm-none-eabi-” part, everyting should work.

7. Test the Tool Chain

To test our tool chain, we use the minimalistic example provided with the “st-util” from “texane“. Create a file called “main.c”, put in the following content:

typedef unsigned int uint32_t;

#define GPIOC           0x40011000 // port C
#define GPIOC_CRH       (GPIOC + 0x04) // port configuration register high
#define GPIOC_ODR       (GPIOC + 0x0c) // port output data register
#define LED_BLUE        (1<

Now set “CC” to the crosscompiler you installed earlier. E.g when using the angstrom toolchain we export:

export CC=arm-angstrom-linux-gnueabi-gcc

Then compile the sources:

$CC \
    -O2 \
    -mlittle-endian \
    -mthumb \
    -mcpu=cortex-m3 \
    -ffreestanding \
    -nostdlib \
    -nostdinc \
    main.c

This produces the file “a.out”. We now check if this is an ARM binary:

file a.out

This should give you something like this:

a.out: ELF 32-bit LSB executable, ARM, version 1 (SYSV), statically linked, not stripped

2 Comments to Cross-Compiling for ARM

  1. 20. Januar 2012 at 20:11 | Permalink

    you should have a look at:
    Gentoo Crosscompiler Suite:
    # emerge -av crossdev
    # crossdev -S armv7a-hardfloat-linux-gnueabi
    # emerge-armv7a-hardfloat-linux-gnueabi -av $PACKAGE
    or
    $ armv7a-hardfloat-linux-gnueabi-gcc $SOURCECODE

    it’s pretty cool, imho. But, it depends on gentoo :)

  1. By on 14. Januar 2013 at 18:15

Leave a Reply

You must be logged in to post a comment.