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
  • 19
  • 33
  • This seems along the lines what I need to do as well. We are developing for an ARM5 running Arch Linux. Development is on Ubuntu Linux 10.04 LTS (today, maybe we upgrade that tomorrow, I don't know yet). Currently we are running GCC 4.4, but the things I need to do will be helpful if not require GCC 4.7 (minimum) if not GCC 4.8 (if we can manage it), for C++11 language features. It seems like all the tool chains available for ARM (5 or anything else) are helplessly out-dated. Is it possible to build a cross compiler tool chain from source and support the GCC we want? – mwpowellhtx Mar 01 '13 at 22:38
  • I have a little problem in the second step. in fact, I've done the ./configure and make commands. Everything works fine. but when i've tiped "install", i got an error message saying: install: missing file operand –  Jul 31 '13 at 07:57
  • See also: http://raspberrypi.stackexchange.com/q/14587/5538 I closed that as a dupe, but I thought it worth linking here because the accepted answer there goes into considerable detail not included here. – goldilocks Jul 26 '15 at 14:28

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
  • 73
  • 2
  • 8
  • Wow, 45 minutes is a long time. How quick is your system? I'm waiting now. – Jivings Jun 14 '12 at 15:46
  • Well, maybe it was a bit less, but it has to compile all the prerequisites first and after bintools and gcc also libc and libstdc++, so 45 min sounds reasonable to me. –  Jun 14 '12 at 15:55
  • Could we add details on how to install these properly and update the path, once tested of course? – Alex Chamberlain Jun 27 '12 at 16:38
  • 2
    Also, I think we should be using hardfp, as the RPi supports it. – Alex Chamberlain Jun 27 '12 at 16:39
  • May be, but IIRC, the Arch's GCC is configured with softfp. I don't have time to test this now, but I've made the answer community wiki, so feel free to improve it. –  Jun 28 '12 at 11:10
  • For reference, I did a build here on Ubuntu 12.04 (gcc 4.8 on Arch Linux failed to build the toolchain due to check_fds.c:85:1: internal compiler error: Segmentation fault). Total build time (excluding download time of 200 MiB of tarballs) on an i5-2320 is 16 minutes. Total disk space used for the build directory after finishing (w/o tarballs): 4.2 GiB. Output of ct-build: http://pastebin.com/LAfsVAzx. Everything was done in tmpfs, the installed x-tools directory was 102 MB. – Lekensteyn May 06 '13 at 21:57
  • @Jivings compile it with the -j flag, which will paralize the build process – Martin Erhardt Jun 07 '13 at 17:29
  • why should one use the latest version of glibc? Isn't it better to use the same version as the one provided by raspbian? – Christian Rapp Nov 15 '13 at 11:23
  • I read the other day that p-ip is hardfp and that hard and soft are incompatible (something about the library api, or kernel abi), if so then you must choose hardfp. – ctrl-alt-delor Jun 10 '14 at 20:33
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,538
  • 11
  • 90
  • 139
  • 1
    This seems to be gcc-4.5.1. –  Jul 18 '12 at 10:57
  • @Tibor Is that bad? – Jivings Jul 18 '12 at 11:04
  • @Jivings It totally does not answer the question! – Alex Chamberlain Jul 18 '12 at 11:26
  • How do you install these so that it works with -march? – Alex Chamberlain Jul 18 '12 at 12:17
  • 2
    I disapprove of the downvotes. @AlexChamberlain It suggests a valuable alternate method to the time consuming act of building your own toolchain. – Jivings Jul 18 '12 at 15:11
  • I didn't downvote... The question specifically says 4.7! – Alex Chamberlain Jul 18 '12 at 15:13
  • @AlexChamberlain Yes, fair enough. For your previous Q; You shouldn't need to provide -march should you? – Jivings Jul 18 '12 at 15:19
  • 2
    @Jivings: This is very helpful. Meanwhile it is gcc4.7.1 (the best I ever had) and floating point works right away. I couldn't make floating point work using Chris' original description that Tibor posted. Thanx a lot. –  Dec 15 '12 at 09:05
  • @tommiport: if floating point did not work when self-cross-compiling, you might have mixed have hardfp with softfp. – Blaisorblade Mar 02 '13 at 02:32
  • Meanwhile 4.8.3 and works like a charm. Just download the tools-master.zip (288M) and unzip only the needed directory. – Alex Oct 28 '14 at 21:41
  • I have done something very similar to this over the past week. This may not be the correct version of gcc, but this is still a very useful. – JayDev Jan 16 '15 at 11:48
  • @Jivings, I just tested your steps on a machine which already has an arm-linux-gnueabihf-g++ 5.3.1 compiler and it is evidently using the subdirectory /lib/i386-linux-gnu for the libc.so.6 dependency according to strace. This causes a clash of armv6 and armv7 components. How could we get around this error? Thank you – Frank May 06 '16 at 11:39
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
  • 8
  • 15