Index To the Series
1. Raspberry Pi likes Open Source
2. Cross-Compiling for Raspberry Pi
3. Cross-Compiling ITK for Raspberry Pi
4. Raspberry Pi likes VTK
5. Raspberry Pi likes Node.js

 


 Cross-Compiling for Raspberry Pi

 

Using a Dell Precision M6700 (Ubuntu 12.10) to build binaries for the Raspberry Pi

 

This is a follow up on our exploration of the Raspberry Pi.

Thanks to Andrew Maclean who generously shared with us his recipe to cross-compile for the Raspberry Pi in the comments of our previous blog.

Two of the main challenges with cross-compilation are that:

  • There are many ways to do it.
  • Recipes out there have some missing ingredients.

Here we are following Andrew's recipe, and adding comments and small updates as we go about the process.

(Please share with us your variations, and improvements to this recipe).

 We are doing this in a DELL PRECISION M6700 Laptop running Ubuntu 12.10.

 

Note about the images in this blog post: We captured a good number of screenshots to document the process below. The images may appear in low resolution in the blog and may be hard to read, but if you click on them, you will get the full resolution image of the screenshot and text should appear very clearly on them.

 

Step 1. Build the Toolchain

Since we are going to run in laptop with an Intel processor, and we want to build object code for the ARM processor at the heart of the Raspberry Pi, we need a cross-compiler and its associated tools, which is usually called a "toolchain". Here we are using "crosstool-ng" to build such tool chain.

Following Andrew's advice, many of the instruction below follow this post by Chris Boot:
http://www.bootc.net/archives/2012/05/26/how-to-build-a-cross-compiler-for-your-raspberry-pi/

 

Step 1.1 Download crosstool-ng

We go to: http://crosstool-ng.org/#download_and_usage

and download the most recent version, that at the time of writing this blog was:  1.17.0
note that in the download page, the version numbers are sorted alphabetically (not numerically).
In my first visit, I went straight to the bottom of the page and erroneously grabbed version 1.9.3,
just because it was at the bottom of the page...

This link below, with the downloads sorted by date, might be useful to you:

The file 00-LATEST-is-1.17.0 should have also be a hint... if I were paying attention...  :-)

 

We created a directory to host it and then downloaded and extracted the sources by doing:

  • mkdir -p  ~/src/RaspberryPi/toolchain
  • cd ~/src/RaspberryPi/toolchain
  • wget http://crosstool-ng.org/download/crosstool-ng/crosstool-ng-1.17.0.tar.bz2
  • tar xjf crosstool-ng-1.17.0.tar.bz2
  • cd crosstool-ng-1.17.0

 

Step 1.2 Configure and Build

Here we continue following Chris Boot's instructions.

We chose to configure the tool to be installed in a local directory inside our home directory.

  • cd ~/src/RaspberryPi/toolchain/crosstool-ng-1.17.0
  • mkdir -p ~/local/crosstool-ng
  • ./configure --prefix=/home/ibanez/local/crosstool-ng

to get this to work, we had to install the following Ubuntu packages (most of which were listed in Andrew's recipe),

  • bison
  • cvs
  • flex
  • gperf
  • texinfo
  • automake
  • libtool

The whole is done with the command:

  • sudo aptitude install bison cvs flex gperf texinfo automake libtool 

then we can do

  • make
  • make install

and add to the PATH the bin directory where crosstool-ng was installed:

  • export PATH=$PATH:/home/ibanez/local/crosstool-ng/bin/

In some cases, it might be necessary to unset the LD_LIBRARY_PATH,
to prevent the toolchain from grabbing other shared libraries from the host machine:

  • unset LD_LIBRARY_PATH

 

Step 1.3 Build Raspberry Toolchain

Create a staging directory. This is a temporary directory where the toolchain will be configured and built, but it is not its final installation place.

  • mkdir -p ~/src/RaspberryPi/staging
  • cd ~/src/RaspberryPi/staging/
  • ct-ng  menuconfig

 

You will see a menu similar to:

  • Go into the option "Paths and misc options"
  • Enable the option "Try features marked as EXPERIMENTAL"

  • In the option "Prefix Directory (NEW)", one can set the actual destination directory where the toolchain will be installed.

  • In this case we choose to install in ${HOME}/local/x-tools/${CT_TARGET}.
    Others may prefer /opt/cross/x-tools/${CT_TARGET}, for example.

  • After you select < Ok >
  • Select the < Exit > option to go back to the main menu
  • There, select "Target options".

  • Change the Target architecture to arm.

  • Leave Endianness set to Little endian and 
  • Bitness set to 32-bit.

  • Use again the < Exit > option to go back to the main menu
  • Select "Operating System"

  • There, change the "Target OS" option from (bare-metal)

  • to the option "linux"

  • Take the <Select> option
  • Use the < Exit > option to get back to the main menu
  • Select "Binary utilities"

  • Select "binutils version"

  • Take the most recent version that is not marked as EXPERIMENTAL.
    In our case, that was version 2.21.1a

  • Go back to the main menu
  • Select "C compiler"

  • Enable the Show Linaro versions (EXPERIMENTAL) option.

  • Here we selected the "linaro-4.7-2012.10 (EXPERIMENTAL) "
    This is a bit newer than the version "linaro-4.6-2012.04 (EXPERIMENTAL)"
    that Chris Boot was using in his blog post, so here we are taking our chances...

  • Select that option.
  • Exist the configuration and
  • Save the changes

Note contributed by Scott Determan:

If you want to cross-compile code that used C++11 futures/promises,
then gcc needs to be build with the flag

                   --with-arch=armv6.

To do this, use the command

            ct-ng menuconfig,

and go to the item:

       Target options -> Architecture level

and set it to "armv6" (without quotes),

then you will be able to cross compile code that uses futures/promises.

 

Thanks Scott !

 Then, start the build process by typing

  • ct-ng  build

  • It was nice to see that the build projects uses the proper "make -j " options for parallel building
    and therefore makes use of all the available cores:

Not to be a whiner.... but,...
the problem with this,
is that it only gives us 18minutes and 9 seconds for the Coffee break   :-)

  • When the build process finishes, we end up with the toolchain installed in the "prefix" directory.
    In our case: ${HOME}/local/x-tools/${CT_TARGET}
  • More specifically:
    /home/ibanez/local/x-tools/arm-unknown-linux-gnueabi
  • Where we will find the following collection of executables in the "bin" directory:

  • We now add this directory to the PATH:
    export PATH=$PATH:/home/ibanez/local/x-tools/arm-unknown-linux-gnueabi/bin
  • We can then test the toolchain with a "hello world" small C program.
  • Compiling it locally as "helloworld" (in "aleph" which is the name of our Ubuntu Laptop).
  • Then copying it into the Raspberry Pi
  • and finally running it there

 

Step 1.4 Build the C++ compiler in the toolchain

By default, our process above only built the C compiler.

We are now going to build the C++ compiler as well.

To build the C++ compiler we do the following:

  • We go back to the staging directory:
    /home/ibanez/src/RaspberryPi/staging


  • and run the configuration process
    ct-ng menuconfig

  • We go into the "C compiler" option

  • Enable the option "C++"

  • Save and Exit
  • and type again
       "ct-ng  build"
    to build the toolchain.

This time it took 13 minutes 18 seconds

and we have now the new C++ components in the toolchain binary directory

 Time to test the C++ compiler with a Hello World.

  • We build it locally
  • Copy the executable to the Raspberry Pi
  • Login in the Raspberry Pi
  • Execute the cross-compiled executable

This completes the set up of the tool chain.

We are now ready to use CMake to cross compile bigger projects.

 

 


 

 

Step 2. One CMake File to Rule Them All !

We now turn our attention to the Cross Compilation instructions of the CMake Wiki

The first step here is to write a .cmake file that points to the toolchain.

In our case we choose to call this file:

  • Toolchain-RaspberryPi.cmake

and put on it the following content

 


# this one is important
SET(CMAKE_SYSTEM_NAME Linux)
#this one not so much
SET(CMAKE_SYSTEM_VERSION 1)

# specify the cross compiler
SET(CMAKE_C_COMPILER
/home/ibanez/local/x-tools/arm-unknown-linux-gnueabi/bin/arm-unknown-linux-gnueabi-gcc)

SET(CMAKE_CXX_COMPILER
/home/ibanez/local/x-tools/arm-unknown-linux-gnueabi/bin/arm-unknown-linux-gnueabi-g++)

# where is the target environment
SET(CMAKE_FIND_ROOT_PATH
/home/ibanez/local/x-tools/arm-unknown-linux-gnueabi)

# 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)


Note here that the path

  • /home/ibanez/local/x-tools/arm-unknown-linux-gnueabi

is the base directory where we installed the toolchain.

and the file names

  • arm-unknown-linux-gnueabi-gcc
  • arm-unknown-linux-gnueabi-g++

are the names of the generated C and C++ compilers respectively.

 

We now put the cmake toolchain file in the directory:

  • /home/ibanez/bin/RaspberryPi/CMakeToolChain

Then we create a CMake-based Hello World example:

  • mkdir -p /tmp/hello/src
  • mkdir -p /tmp/hello/bin
  • cd /tmp/hello/src

write here a CMakeLists.txt file with just:

cmake_minimum_required(VERSION 2.8)
project(HelloWorld)
add_executable(HelloWorld HelloWorld.cxx )
target_link_libraries(HelloWorld)


and the associated HelloWorld.cxx file with:

#include <iostream>
int main() {
  std::cout << "Hello World++ !" << std::endl;
  return 0;
  }

Then, we can change directories to the bin directory and configure with CMake, by pointing to the toolchain file as:

  • cd /tmp/hello/bin
  • cmake -DCMAKE_TOOLCHAIN_FILE=/home/ibanez/bin/RaspberryPi/CMakeToolChain/Toolchain-RaspberryPi.cmake ../src
  • and simply build with "make"
  • Then copy the resulting executable to the Raspberry Pi, and run it.

 

Step 3. Setting up additional bin and lib files for cross compiling.

If you need access to the libraries on the RaspberryPi for compiling
(for example if you have built the latest boost libriaries on the RaspberryPi
and it is installed in /usr/local), you can copy these to a directory on your
host computer using rsync that will preserve the symlinks.
  • On your host machine you may also have to install rsync
    •  sudo aptitude install rsync
  • On the RaspberryPi install rsync:
    •  sudo apt-get istall rsync
  • Create a folder on the cross compiling machine. For example, here we call it: ~/bin/RaspberryPi
    • mkdir -p ~/bin/RaspberryPi
  • cd to this folder
    • cd  ~/bin/RaspberryPi
  • and do the following:
    • rsync -rl pi@raspberrypi.bigpond:/lib .
    • rsync -rl pi@raspberrypi.bigpond:/usr .

Remember to run these rsync commands whenever new libraries are added to the RaspberryPi system or when the RaspberryPi is upgraded.

 

 

This concludes our introduction to Cross Compilation for the Raspberry Pi using CMake.

 

Please share with us your comments, and suggestions for improving this process.

Share