Difference between revisions of "Cross-compiling for the RaspberryPi"

From AIRWiki
Jump to: navigation, search
m (Step One: installing the cross-compiler)
(Prerequisites: added a missing library for Debian.)
 
(5 intermediate revisions by the same user not shown)
Line 3: Line 3:
 
== Prerequisites ==
 
== Prerequisites ==
 
* Install all the required software packages:
 
* Install all the required software packages:
  <code>sudo apt-get install git</code>
+
  <code>sudo apt-get install git rsync cmake</code>
  
 
* '''If you are using a 64-bit version of Ubuntu''', also install the 32-bit compatibility libraries:
 
* '''If you are using a 64-bit version of Ubuntu''', also install the 32-bit compatibility libraries:
 
  <code>sudo apt-get install ia32-libs</code>
 
  <code>sudo apt-get install ia32-libs</code>
 +
 +
* '''If you are using a 64-bit version of Debian''', the ia32-libs metapackage is no longer installable. Execute instead
 +
  <code>su -
 +
  dpkg --add-architecture i386
 +
  apt-get update && apt-get install libc6-i386 lib32stdc++6 zlib1g:i386</code>
  
 
* The guide will take for granted that you are using the official Raspbian OS image, downloadable from the RaspberryPi website.  
 
* The guide will take for granted that you are using the official Raspbian OS image, downloadable from the RaspberryPi website.  
Line 21: Line 26:
 
  <code>git clone git://github.com/raspberrypi/tools.git</code>
 
  <code>git clone git://github.com/raspberrypi/tools.git</code>
 
This will create, among other things, a <code>tools/arm-bcm2708</code> subfolder containing three different compiler toolchains:
 
This will create, among other things, a <code>tools/arm-bcm2708</code> subfolder containing three different compiler toolchains:
** arm-bcm2708-linux-gnueabi
+
 
** arm-bcm2708hardfp-linux-gnueabi
+
arm-bcm2708-linux-gnueabi
** gcc-linaro-arm-linux-gnueabihf-raspbian
+
 
 +
arm-bcm2708hardfp-linux-gnueabi
 +
 
 +
gcc-linaro-arm-linux-gnueabihf-raspbian
 +
 
  
 
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.
 
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.
Line 43: Line 52:
  
 
== Step Two: set-up your compiling environment ==
 
== Step Two: set-up your compiling environment ==
 +
 +
* Go back into the <code>raspberrypi</code> directory and create a subfolder named <code>rootfs</code>:
 +
  <code>cd
 +
cd raspberrypi
 +
mkdir rootfs</code>
 +
* Now tell <code>rsync</code> to copy the entirety of the <code>/usr</code> and <code>/lib</code> directories of your RaspberryPi filesystem into the newly created folder on your computer:
 +
<code>rsync -rl --delete-after --safe-links pi@$RASPI:/{lib,usr} $HOME/raspberrypi/rootfs</code>
 +
where <code>$RASPI</code> 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 <code>Toolchain-RaspberryPi.cmake</code> and insert the following into it:
 +
 +
SET(CMAKE_SYSTEM_NAME Linux)
 +
SET(CMAKE_SYSTEM_VERSION 1)
 +
# 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
 +
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
 +
# for libraries and headers in the target directories
 +
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
 +
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
 +
 +
* Now you are able to compile your own projects using <code>cmake</code>, by passing the path of the Toolchain specification file to the program (supposing that the sources are located into a <code>src</code> directory which in turn is located into <code>${PROJECT_DIR}</code>):
 +
<code>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</code>
 +
 +
== Step Three: Remote program execution through Eclipse ==
  
 
... to be continued ...
 
... to be continued ...
  
 
== References ==
 
== References ==
 +
* http://wiki.debian.org/Multiarch/HOWTO
 
* http://hertaville.com/2012/09/28/development-environment-raspberry-pi-cross-compiler/
 
* http://hertaville.com/2012/09/28/development-environment-raspberry-pi-cross-compiler/
 
* http://www.kitware.com/blog/home/post/426
 
* http://www.kitware.com/blog/home/post/426
 
* http://www.cmake.org/Wiki/CMake_Cross_Compiling
 
* http://www.cmake.org/Wiki/CMake_Cross_Compiling

Latest revision as of 16:47, 25 May 2013

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.

Prerequisites

  • 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:
cd
mkdir raspberrypi
cd raspberrypi
  • Download the "official" cross-compiler of the Raspberry Pi project through git:
git clone git://github.com/raspberrypi/tools.git

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

arm-bcm2708-linux-gnueabi

arm-bcm2708hardfp-linux-gnueabi

gcc-linaro-arm-linux-gnueabihf-raspbian


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:
PATH=$PATH:$HOME/raspberrypi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin
  • 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.
COLLECT_GCC=arm-linux-gnueabihf-gcc
COLLECT_LTO_WRAPPER=/home/tudhalyas/raspberrypi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/../libexec/gcc/arm-linux-gnueabihf/4.7.2/lto-wrapper
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=https://bugs.launchpad.net/gcc-linaro --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
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:
SET(CMAKE_SYSTEM_NAME Linux)
SET(CMAKE_SYSTEM_VERSION 1)
# 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
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
# for libraries and headers in the target directories
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
  • 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 ...

References