Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cross-compilation for Raspberry Pi in GCC. Where to start?

People also ask

Does GCC support cross compilation?

GCC, a free software collection of compilers, can be set up to cross compile. It supports many platforms and languages. Cross-compiling GCC requires that a portion of the target platform's C standard library be available on the host platform.


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!


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.


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.


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.


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

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.


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.