51

TL/DR: Where can I find more information about building a GCC 4.7.0 cross-compiling toolchain for ARM (gnueabi) platform (intended to run on a Raspberry Pi device)?

I have just got a brand new Raspberry Pi and I am very eager to start programming for it. I've managed to install the GCC toolchain (I am using the Arch Linux system image) and compiled some basic programs, all working fine.

I've also tried to compile the Boost libraries because I often use them in my projects and everything seemed to work fine by following the instructions (./bootstrap.sh + ./b2) except for the fact that the compilation was painfully slow. I left it on for a few hours but it barely got past the first few source files. After I left it running for the night, I discovered that the build process aborted due to RAM shortage.

So, my guess is that Rasp Pi is simply underpowered for compiling something of such size as Boost. So, cross-compilation comes to my mind. However, even though there is a lot of information about ARM cross compilation available online, I find it confusing. Where does one even start?

I have a recent GCC version (4.7.0) available on my Raspberry Pi, so I would ideally like to cross-compile with the same version. Where can I get the GCC 4.7.0 toolchain for ARM? (I will be compiling on x86 CentOS 6.2)

Edit:

I deallocated unneeded GPU memory and set up a 4GB swap partition on a USB drive, while build files are on a NFS share. Boost is now compiling much much faster, so it is manageable. I would still like to know how can I set up a GCC 4.7 toolchain for cross compilation on my x86 PC though, since I intend to do a lot of compiling and I would like it to be as fast as possible.

Edit 2:

Since GCC 4.7.0 is relatively new, there does not seem to be a pre-built cross-compiler (i386->ARM). I will probably have to build one myself, which seems an non-trivial task (I've tried and failed). Does anyone know of a tutorial to follow for building a GCC cross-compiler, hopefully for one of the recent versions?

I've tried with this great shell script (which worked great for building a same-arch compiler) and I've successfully built binutils and GCC's prerequisites, but then GCC build kept failing with many cryptic errors. I am really lost here, so I would greatly appreciate your help.

GCC on Raspberry Pi was configured with

--prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib 
--mandir=/usr/share/man --infodir=/usr/share/info 
--with-bugurl=https://bugs.archlinux.org/ 
--enable-languages=c,c++,fortran,lto,objc,obj-c++ --enable-shared 
--enable-threads=posix --with-system-zlib --enable-__cxa_atexit 
--disable-libunwind-exceptions --enable-clocale=gnu 
--disable-libstdcxx-pch --enable-libstdcxx-time 
--enable-gnu-unique-object --enable-linker-build-id --with-ppl 
--enable-cloog-backend=isl --enable-lto --enable-gold 
--enable-ld=default --enable-plugin --with-plugin-ld=ld.gold 
--with-linker-hash-style=gnu --disable-multilib --disable-libssp 
--disable-build-with-cxx --disable-build-poststage1-with-cxx 
--enable-checking=release --host=arm-unknown-linux-gnueabi 
--build=arm-unknown-linux-gnueabi 

Edit 3:

I managed to build a 4.7 GCC toolchain for ARM (yay!) using this shell script as suggested by user dwelch in the comments. I also built newlib and libstdc++ using this article as a guide. The toolchain works fine, but hen I run the executable on my Raspberry Pi, it fails with Illegal instruction. What could be the cause of that?

  • 1
    Try the zram package if it's not already installed. This will create a compressed RAM swap, which help more than you think. (Weirdly enough.) Also add a decently fast USB device, either a USD HDD or a high quality USB stick and put swap on that. – Prof. Falken Jun 11 '12 at 01:58
  • 3
    Also, in this thread you can read how to disable graphics memory so you gain 32 megs of RAM: http://www.raspberrypi.org/phpBB3/viewtopic.php?f=5&t=4831&p=65410 – Prof. Falken Jun 11 '12 at 02:06
  • 1
    Wow, thank you. I actually had 128MB allocated for GPU. No wonder everything was so sluggish. –  Jun 11 '12 at 08:33
  • 1
    Also try zram, it's really awesome. Makes a *world* of difference on the EeePC701 which has an 800MHz pre-Atom (old-style Celeron-M) CPU and 512 megs of RAM. – Prof. Falken Jun 11 '12 at 08:43
  • Couldn't find 4.7 however, GCC 4.6 ARM cross compilers are available at [https://launchpad.net/gcc-arm-embedded/+download](https://launchpad.net/gcc-arm-embedded/+download). – panickal Jun 11 '12 at 01:51
  • There are some x86 toolchains here: https://github.com/raspberrypi/tools/tree/master/arm-bcm2708 – dave Jun 11 '12 at 01:41
  • For the posterity, I forgot to set `CONFIG_HAVE_ARCH_SECCOMP_FILTER=y`, `CONFIG_SECCOMP_FILTER=y` and `CONFIG_VFP=y` when compiling the `linux-rpi-3.14.y` kernel, and I was experimenting a lot of `Illegal instruction` when booting a `2014-01-07-wheezy-raspbian.img`. – Avio Jun 01 '14 at 19:13
  • Possible duplicate of [Installing Raspberry Pi Cross-Compiler](http://stackoverflow.com/questions/19162072/installing-raspberry-pi-cross-compiler) – Ciro Santilli OurBigBook.com Sep 07 '16 at 08:23

8 Answers8

25

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.

Seems to work great!

4

http://github.com/dwelch67/raspberrypi buildgcc directory there is a script for both 4.7 gcc/gnu and 3.0 clang/llvm. gnu one derived from the script at the mpx project at opencores, I cut out gdb and libgcc, glibc, etc took it down to a compiler. if you want that other stuff cut and paste arm for mips.

Tobias Kienzler
  • 25,759
  • 22
  • 127
  • 221
old_timer
  • 69,149
  • 8
  • 89
  • 168
4

Here is a step-by-step guide How to build Raspberry Pi cross-compiler in Windows. The reason you are getting Illegal Instruction error is latest Raspbian is hardfp-enabled and requires appropriate patches for gcc and eglibc to support hardfp. Otherwise generated code will use different ABI, i.e. pass function arguments in different registers, so crash at runtime.

Also misconfiguring GCC for ARMv7 (Raspebby Pi is ARMv6) may lead to Illegal Instruction error. Be sure to specify --with-arch=armv6 option when configuring GCC.

The guide linked above is based on GCC 4.6.3 though. But I guess it should work with GCC 4.7 too.

Mikhail Kupchik
  • 308
  • 2
  • 8
2

I couldn't find a 4.7 gcc, only a 4.6. For information, I compiled the Pi's kernel on a fairly old Ubuntu machine (1Ghz, 768MB RAM) following the guidelines on this page: Kernel compilation

The build took about 90 minutes, compared to the 6 hours it is suggested that it would take on the Pi. The kernel runs fine on the Pi. Perhaps you can adapt these guidelines for cross-compiling other projects on your x86 Linux machine.

Community
  • 1
  • 1
NickT
  • 23,844
  • 11
  • 78
  • 121
2

You can try my open-sourced pre-built/pre-compiled GCC Cross & Native Compiler Binaries for Raspberry Pi from this up-to-date GitHub repository:

This project contains the UpToDate set of Precompiled/Pre-Built Raspberry pi GCC Cross & Native Compilers Binaries, saving your tons of time(No compiling or Error Handling needed whatsoever). Just Extract, Link & Enjoy complete GCC(Raspberry Pi) functionality in your Machine. You can use its native compilers for Raspberry Pi(Can be used along with old & slow 6.3.0 GCC), Or use the Cross-Compiler in any Linux Machine(Tested on Latest Ubuntu/bionic x64) to compile programs for your Raspberry Pi. All these compilers binaries are Raspberry Pi hardware optimized for enhanced overall performance.

Supported GCC Versions:

  • GCC 6.3.0
  • GCC 7.4.0
  • GCC 8.2.0
  • GCC 8.3.0

Supported Raspberry Pis:

  • All Raspberry Pi versions/models are currently supported.
  • Any other ARM Devices with similar Hardware configurations may also work.

Supported Environments:

  • Cross-Compiler: All Linux Distros (x32/x64) are currently supported.
  • Native-Compiler: All Raspberry Pi version/model with Raspbian OS is supported. Other OS may/may-not work.

Supported Languages:

  • C++
  • C
  • Fortran
Community
  • 1
  • 1
abhiTronix
  • 1,248
  • 13
  • 17
1

You could try the Raspberry-GCC-4.7.3 toolchain.

It's a pre-built gcc-4.7.3 toolchain for armv6 with hardfp using gnueabi. I'm using it to cross compile c++11 for a raspberrian target.

Please note it only works on linux x86_64 hosts.

mskfisher
  • 3,291
  • 4
  • 35
  • 48
kallaballa
  • 337
  • 2
  • 8
1

The dockcross project is a great starting point for a completely deodorized tool chain, you could build not just for Pi but for anything else. Here are the commands:

docker run --rm dockcross/linux-armv7 > ./dockcross-linux-armv7
chmod +x ./dockcross-linux-armv7
./dockcross-linux-armv7 bash -c '$CC test/C/hello.c -o hello_arm'

It has cmake built in as well.

henrycarteruk
  • 12,708
  • 2
  • 36
  • 40
Zeev Glozman
  • 101
  • 1
  • 1
0

As you've noted, building on the Raspberry Pi itself is slow, but reliable (since it's not cross-compiling), provided you don't run out of memory. Perhaps it's possible to tweak the memory and swap settings to make larger builds possible (if not fast). I know on the forums, people are talking about putting root partitions on SD, user partitions on USB drives, and of course it will probably make a difference to get a fast (class 6 or better) SD card. It's possible, over time, that the compilation performance will improve as the software improves. Or, maybe Raspberry Pi 2 will have an improved chipset and more RAM. :-)

mlepage
  • 2,372
  • 2
  • 19
  • 15
  • Thank you for the suggestion but 30 minutes on RPi versus 1 minute on my laptop makes it VERY impractical for development where I have a lot of rebuilding to do. If the difference wasn't so drastic, I'd definitely be building on the device. –  Jul 02 '12 at 17:47
  • Yeah I'm in the same boat, about 30 min build time on the RPi. I'm going to develop in Linux (Ubuntu VM on Mac OS X, actually), then just do builds on RPi when I want to test on it. I'm hoping that will be adequate for my purpose (it's a reasonable setup) but if not, I'll be cross-compiling too. – mlepage Jul 02 '12 at 17:59
  • I don't know if my Pi is different, but I have one from 2012. It is overclocked to 1000MHz. Yet, it is able to compile my largest program ~10k lines of actual code (not comments, etc.). I get compile times of around 30s to 3 minutes depending on whether I delete all the output objects. I rarely get the GCC segfault. However, it is really a hassle to debug and change code on the fly with that compile time! I debug all my programs on my Laptop with Ubuntu and then compile them for the Pi! Works perfectly! – yash101 Sep 12 '14 at 03:39