89

I already asked this question on Stack Overflow, but I would like to know if anyone managed to build a GCC 4.7 toolchain for ARM cross-compilation (for a x86/x86-64 Linux host). There are many instructions for building GCC from source and many available cross-compilers for pre-4.7 GCC versions, just not the latest one.

Compiling on Rasp Pi itself works fine, but it's just a bit too slow for practical purposes.

I am eager to get compiling and I would like to use the latest and the best tools.

tlhIngan
  • 3,372
  • 5
  • 21
  • 33

8 Answers8

61

I found these instructions How to build a cross compiler for your Raspberry Pi. It is a great walk through using a crosstool-ng tool which simplifies configuring a cross-compiler build A LOT (it has a nice curses-based interface) and it supports GCC 4.7.

I've followed these steps and ended up with a successful build of a 4.7 cross-compiler.

Prerequisites: The following packages are required:bison, flex, gperf, gawk, libtool, automake, g++, ensure these are installed before proceeding.

  1. First download crosstool-ng from here (I used version 1.15.2).
  2. Unpack the distribution and do a ./configure / make / install
  3. Create a fresh directory somewhere in the file-system to build in and cd into it.
  4. Run ct-ng menuconfig. You will be presented with a nice set of menus to configure your build.
  5. Go into Paths and misc options. Enable Try features marked as EXPERIMENTAL.
  6. Choose a suitable Prefix directory. This is the directory that your compiler and libraries will get installed in (anything is fine basicallly, just make sure that the directory is empty).

    • NOTE: It is also important that you have write access to the chosen folder
  7. Go to the Target options menu.

    • Target architecture: arm
    • Endianness: little endian
    • Bitness: 32-bit
  8. You may also want to set floating point parameter to softfp (see this for more info), but hardfp is more appropriate for Raspbian.

  9. Go to Operating system menu and change Target OS to linux.
  10. Go to C compiler menu and choose gcc version 4.7.0 (the article recommends Linaro, but I managed to get it working with vanilla gcc). Also chose additional languages you want to be able to compile (C++, Fortran, ...)
  11. Go to C-library menu and chose one. The default is eglibc but that one didn't build fine for me, so I used glibc (the newest version).

    • NOTE: during the build step 13. eglibc can fail to build if subversion is not installed, as the source can't be retrieved from the repository
    • NOTE: eglibc is no longer a part of crosstool-ng as of version 1.21.0 due to its lack of development. See So long to eglibc. Use glibc as default.
  12. Exit the configuration tool while saving your changes.
  13. Run ct-ng build in the same directory. Wait a while (about 45 minutes in my case) and your cross compiler should be ready.

Seems to work great!

sputnick
  • 193
  • 2
  • 8
19

I am eager to get compiling and I would like to use the latest and the best tools.

In fact, the latest and best tools do not need to be built by yourself. On the Raspberry Pi Tool GitHub Repository you will find the X86_64 and x686 toolchains for cross-compiling software.

I recommend using the x86-linux64-cross-arm-linux-hardfp toolchain, as that will compile for Hard Floating Point, which will result in a much faster system.

$ arm-bcm2708hardfp-linux-gnueabi-gcc --version

arm-bcm2708-linux-gnueabi-gcc-4.5.1 (Broadcom-2708) 4.5.1
Copyright (C) 2010 Free Software Foundation, Inc.

Note: If you are using an existing kernel then you will have to use the toolchain that matches the kernel. HardFP applications will not work on a SoftFP kernel.


To use the toolchain simply check out the repository:

git clone https://github.com/raspberrypi/tools.git --depth 1

The --depth parameter will mean that you don't have to wait for the repository history to be downloaded as well (since we wont be using it).

Then add the binaries to your PATH variable:

export PATH=~/tools/arm-bcm2708/x86-linux64-cross-arm-linux-hardfp/bin:$PATH

Or to persist the PATH:

echo "export PATH=~/tools/arm-bcm2708/x86-linux64-cross-arm-linux-hardfp/bin:$PATH" >> ~/.bashrc
source ~/.bashrc

To compile with the tool chain you can now add the CROSS_COMPILE parameter. For example, when running make:

make CROSS_COMPILE=arm-bcm2708hardfp-linux-gnueabi- 

Or to make this easier, you can save the variable to bashrc again:

echo "export TARGET=arm-bcm2708hardfp-linux-gnueabi" >> ~/.bashrc
source ~/.bashrc

and now use the variable when compiling:

make CROSS_COMPILE=${TARGET}
Jivings
  • 22,656
  • 11
  • 94
  • 140
4

Note that when building the toolchain using ct-ng on centos 6.3 on a 64 bit system I was forced to deselect the option to statically link libstdc++ because static linking was not supported on the platform (apparently).

Also, while it would be great to use the prebuilt toolchain from the git repository, that chain does not seem to work on Centos 6.3 - probably because it is built for a more modern system or something. I did not really try to run this down.

3

In case you are going to cross-compile from the OS X : here is the great article (and the only one working on the web I found).

The greatest thing is author provides a complete precompiled toolchain, so you only need to download, unzip and mount dmg image. And thats it, you are ready to cross-compile.

koverman47
  • 109
  • 4
3

This guide may be helpful.

It helped me get mine going. I added some comments about tuning as well.

koverman47
  • 109
  • 4
3

If you want to benefit from a fast host for compiling things for your RPI I suggest to work in cross environment via chroot and QEMU. This by the way supersedes a cross compiler with no hassle.

Simply setup a Debian cross-environment using deboostrap/multistrap

(see chapter QEMU/debootstrap approach) and you are done.

koverman47
  • 109
  • 4
sparkie
  • 435
  • 3
  • 9
2

If you installed Ubuntu 64 bits edition and the tools from Raspberry Pi SVN and you are getting the error:

-bash: /home/I/toolchain/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf-c++: 
No such file or directory

just run:

sudo apt-get install ia32-libs
kolin
  • 950
  • 2
  • 12
  • 25
BirdOfPrey
  • 21
  • 1
1

The Carlson-Minot Inc. provides a prebuilt toolchain for both bare-metal and GNU/Linux ARM targets. This toolchain is based on the Mentor Graphics Sourcery Lite toolchain with fixes and adoptions for build on the OS X. See

Glorfindel
  • 620
  • 1
  • 9
  • 16