The Raspberry Pi has limited resources compared to a regular desktop PC. Development and compilation on the Raspberry Pi tends to be quite slow. 

In this tutorial I'm going to show to you how use Ubuntu 12.04 LTS to compile for raspberry pi running raspbian wheezy.

The toolchain I use is the one prepared by RTI (www.rti.com) for 32 and 64 bit Linux environment.


First I installed Ubuntu 12.04 into a virtual machine.

After the reboot I installed all the updates and the I rebooted the virtual machine.

Then I downloaded and extracted the prepared toolchain into a directory named toolchain:

$ mkdir -p /home/myuser/toolchain

$ cd /home/myuser/toolchain

$ wget https://s3.amazonaws.com/RTI/Community/ports/toolchains/raspbian-toolchain-gcc-4.7.2-linux32.tar.gz

$ tar xvzf raspbian-toolchain-gcc-4.7.2-linux32.tar.gz


if you are using a 64-bit linux you have to do 

$ wget https://s3.amazonaws.com/RTI/Community/ports/toolchains/raspbian-toolchain-gcc-4.7.2-linux64.tar.gz

$ tar xvzf raspbian-toolchain-gcc-4.7.2-linux64.tar.gz


last I exported the path to the toolchain binaries:

$ export PATH=/home/myuser/toolchain/raspbian-toolchain-gcc-4.7.2-linux32/bin:$PATH

This command shall be issued every time you'll reboot your linux box, so I think it is better to add this line into  .bashrc or .profile configuration files.


At this point I was ready to compile for raspbian-wheezy

A simple helloworld could help..

#include <stdio.h>

#include <stdlib.h>

int main(int argc, char **argv)

{

printf("Hello world\n");

}


to compile for linux x86 you have to issue the following command:

$ cc -o main_x86 main.c


to compile for raspbian you have to use the arm-linux-gnueabihf-gcc command, simply substitute the cc command with arm-linux-gnueabihf-gcc:

$ arm-linux-gnueabihf-gcc -o main_rasp main.c


the file utility could help us to get some info

$ file main_x86 main_rasp 

main_x86:  ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=0x746ec52e8c892d5fd72418892b620bb9dbb7bf62, not stripped

main_rasp: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, not stripped


now you have only to move the main_rasp executable into your Raspberry Pi box. Note that we have used dynamic linking. To use static linking you have to add the -static option to the compiling command:

$ arm-linux-gnueabihf-gcc -o main_rasp main.c -static


Gg1