Using Ubuntu to cross compile for Raspberry Pi

 

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


You may also like...

5 Responses

  1. Greg Fenton says:

    Great, works flawlessly on ubuntu 12.10, apart from 2 modifications:
    sudo apt-get install gcc-arm-linux-gnueabihf
    and changing all instances of
    myuser

    to
    $USER
    Just need to get me a Raspberry Pi now 🙂
    Regards
    Greg

  2. Greg Fenton says:

    Commenting on here must have satiated the Gods – I have just won one of the fabled Blue Raspberry Pis!

    #bluepi

  3. admin says:

    what a lucky man!
    Did you take part to a contest?

  4. dart says:

    I would like get audacious working on pi .Right now there are some drivers missing. Is there anyway to convert the latest to Raspbian for the raspberry pi?
    Thank you in advance for any help in that sector.

    Dart Gar

  1. May 4, 2015

    […] compiled on a Linux Ubuntu 12.04 based PC via Rasbian Toolchain using instructions downloaded from http://www.xappsoftware.com. This allows programs to be compiled from a Terminal window. Using this, the C/C++ code can be […]

Leave a Reply

Your email address will not be published. Required fields are marked *