Cross-compiling for the RaspberryPi

From AIRWiki

Jump to: navigation, search

This page will guide you to setup a complete cross-compiling environment for the RaspberryPi board. This procedure is tailored for and tested on Ubuntu 12.10, but it can be easily adapted for the use on other distributions.



  • Install all the required software packages:
sudo apt-get install git rsync cmake
  • If you are using a 64-bit version of Ubuntu, also install the 32-bit compatibility libraries:
sudo apt-get install ia32-libs
  • If you are using a 64-bit version of Debian, the ia32-libs metapackage is no longer installable. Execute instead
 su -
 dpkg --add-architecture i386
 apt-get update && apt-get install libc6-i386 lib32stdc++6 zlib1g:i386
  • The guide will take for granted that you are using the official Raspbian OS image, downloadable from the RaspberryPi website.
  • Moreover, you will need a working SSH connection between the Raspberry and your computer (not covered by this guide).

Step One: installing the cross-compiler

  • Select a directory in which you will install everything. For the purposes of this guide, I will consider a subdirectory of your home folder, ~/raspberrypi . Create it and then access it:
mkdir raspberrypi
cd raspberrypi
  • Download the "official" cross-compiler of the Raspberry Pi project through git:
git clone git://

This will create, among other things, a tools/arm-bcm2708 subfolder containing three different compiler toolchains:




We are going to use the third of these toolchains, since it's the most optimized and supports the FPU that is available on the RaspberryPi board.

  • Go back to your home directory, and open your .bashrc configuration file with any text editor, like nano, gedit or leafpad, and add this string at the bottom:
  • Now, either logout and login back again, or just close and reopen the terminal window, and type:
arm-linux-gnueabihf-gcc -v

If all goes well, you should see something like

tudhalyas@gondor:~$ arm-linux-gnueabihf-gcc -v
Using built-in specs.
Target: arm-linux-gnueabihf
Configured with: /cbuild/slaves/oort61/crosstool-ng/builds/arm-linux-gnueabihf-raspbian-linux/.build/src/gcc-linaro-4.7-2012.08/configure --build=i686-build_pc-linux-gnu --host=i686-build_pc-linux-gnu --target=arm-linux-gnueabihf --prefix=/cbuild/slaves/oort61/crosstool-ng/builds/arm-linux-gnueabihf-raspbian-linux/install --with-sysroot=/cbuild/slaves/oort61/crosstool-ng/builds/arm-linux-gnueabihf-raspbian-linux/install/arm-linux-gnueabihf/libc --enable-languages=c,c++,fortran --disable-multilib --with-arch=armv6 --with-tune=arm1176jz-s --with-fpu=vfp --with-float=hard --with-pkgversion='crosstool-NG linaro-1.13.1+bzr2458 - Linaro GCC 2012.08' --with-bugurl= --enable-__cxa_atexit --enable-libmudflap --enable-libgomp --enable-libssp --with-gmp=/cbuild/slaves/oort61/crosstool-ng/builds/arm-linux-gnueabihf-raspbian-linux/.build/arm-linux-gnueabihf/build/static --with-mpfr=/cbuild/slaves/oort61/crosstool-ng/builds/arm-linux-gnueabihf-raspbian-linux/.build/arm-linux-gnueabihf/build/static --with-mpc=/cbuild/slaves/oort61/crosstool-ng/builds/arm-linux-gnueabihf-raspbian-linux/.build/arm-linux-gnueabihf/build/static --with-ppl=/cbuild/slaves/oort61/crosstool-ng/builds/arm-linux-gnueabihf-raspbian-linux/.build/arm-linux-gnueabihf/build/static --with-cloog=/cbuild/slaves/oort61/crosstool-ng/builds/arm-linux-gnueabihf-raspbian-linux/.build/arm-linux-gnueabihf/build/static --with-libelf=/cbuild/slaves/oort61/crosstool-ng/builds/arm-linux-gnueabihf-raspbian-linux/.build/arm-linux-gnueabihf/build/static --with-host-libstdcxx='-L/cbuild/slaves/oort61/crosstool-ng/builds/arm-linux-gnueabihf-raspbian-linux/.build/arm-linux-gnueabihf/build/static/lib -lpwl' --enable-threads=posix --disable-libstdcxx-pch --enable-linker-build-id --enable-plugin --enable-gold --with-local-prefix=/cbuild/slaves/oort61/crosstool-ng/builds/arm-linux-gnueabihf-raspbian-linux/install/arm-linux-gnueabihf/libc --enable-c99 --enable-long-long
Thread model: posix
gcc version 4.7.2 20120731 (prerelease) (crosstool-NG linaro-1.13.1+bzr2458 - Linaro GCC 2012.08)

Step Two: set-up your compiling environment

  • Go back into the raspberrypi directory and create a subfolder named rootfs:
cd raspberrypi
mkdir rootfs
  • Now tell rsync to copy the entirety of the /usr and /lib directories of your RaspberryPi filesystem into the newly created folder on your computer:
rsync -rl --delete-after --safe-links pi@$RASPI:/{lib,usr} $HOME/raspberrypi/rootfs

where $RASPI is the IP address of the Raspberry. You can put this line of code in a text file and save it as a shell script for your convenience.

  • Create a text file called Toolchain-RaspberryPi.cmake and insert the following into it:
# specify the cross compiler
SET(CMAKE_C_COMPILER $ENV{HOME}/raspberrypi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf-gcc)
SET(CMAKE_CXX_COMPILER $ENV{HOME}/raspberrypi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf-g++)
# where is the target environment
SET(CMAKE_FIND_ROOT_PATH $ENV{HOME}/raspberrypi/rootfs)
# search for programs in the build host directories
# for libraries and headers in the target directories
  • Now you are able to compile your own projects using cmake, by passing the path of the Toolchain specification file to the program (supposing that the sources are located into a src directory which in turn is located into ${PROJECT_DIR}):
mkdir ${PROJECT_DIR}/build
cd ${PROJECT_DIR}/build
cmake -G"Eclipse CDT4 - Unix Makefiles" -D CMAKE_BUILD_TYPE=Debug -D CMAKE_TOOLCHAIN_FILE=$HOME/raspberrypi/Toolchain-RaspberryPi.cmake ../src

Step Three: Remote program execution through Eclipse

... to be continued ...