Cross-Compiling for Raspberry Pi

This article shows how to cross compile programs written in C/C++ for the Raspberry Pi on the PC. It guides you in building your own cross compiler with the help of crosstool-NG

1. Download crosstool-ng

Download the latest stable release from the following link:

At the time writing this article this was 1.23.0

We will download it into a new directory "crosstool-ng" at your user's home directory. You can do this either via your browser or in the terminal by executing the below commands:

mkdir ~/crosstool-ng
cd ~/crosstool-ng
tar xf crosstool-ng-1.23.0.tar.xz

2. Configure and Build crosstool-ng

First we have to ensure that some required packages are installed, before we can run the configure script.

sudo apt-get install bison cvs flex gperf texinfo automake libtool help2man ncurses*

Also ensure that gcc and g++ compilers are installed:

sudo apt-get install gcc g++

We install the tool chain directly in the users home directory in the new folder x-tools (you can select any other path also). In this example the user name is christian, so replace it with you user name accordingly.

cd crosstool-ng-1.23.0
mkdir ~/x-tools
./configure --prefix=/home/christian/x-tools

Now we can run the installation

make install

Finally add the bin directory of the installed crosstool-ng into the PATH:

export PATH=$PATH:/home/christian/x-tools/bin/

3. Build Raspberry Pi Cross Compiler

We create a temporary directory where the cross compiler will be configured and built, but it is not the finall installation directory.

mkdir ~/x-tools-temp
cd ~/x-tools-temp
ct-ng menuconfig

Now you will see the menu of crosstool-NG: crosstool-NG

Go into the option "Target options": crosstool-NG-  Target options

Change "Target architecture" to "arm": crosstool-NG - Target options - Target architecture

Select "Suffix to the arch-part" crosstool-NG - Target options - Suffix to the arch-part

Set it to "v6": crosstool-NG - Target options - Suffix to the arch-part

Ensure that "Endianness" is set to "Little endian" and "Bitness" is set to "32-bit": crosstool-NG - Target options - Endianness and Bitness

Select "Architecture level": crosstool-NG - Target options - Architecture level

Set it to "armv6" crosstool-NG - Target options - Architecture level

Go into the option "Floating point": crosstool-NG - Target options - Floating point

Change it to "hardware (FPU)": crosstool-NG - Target options - Floating point

Go back into main menu and select "Operating System": crosstool-NG - Operating System

Select "Target OS": crosstool-NG - Operating System

Set it to "linux": crosstool-NG - Operating System

Go back into main menu and select "C compiler": crosstool-NG - C compiler

Scroll down to "C++" and enable it: crosstool-NG - C compiler

Exit back into the main menu and select "Save": crosstool-NG - Save configuration

Keep default configuration name ".config": crosstool-NG - Save configuration

And exit the menuconfig.

Finally, start the build by typing "ct-ng build", depending your machine this may take some time: crosstool-NG - build

Done! The cross compiler is ready to use: crosstool-NG - cross-compiler

Let's test it with a "Hello World" programm: * write a "Hello World" program and save it as hello.c * compile on you local PC * copy the generated file to the Raspberry Pi * Login at the Raspberry Pi * Execute the program which was compiled on the PC crosstool-NG - cross-compiler