In this tutorial you will learn the basics how to cross compile a simple program for the Raspberry Pi 2 Model B.
Why would you like to cross compile?
The two major reasons why you would like to cross compile are:
- Increasing speed of compilation
- Decreasing wear out of SD card
Besides of that the lack of (disk) space can be an argument to decide to cross compile.
In this tutorial, cross compilation is performed at a desktop-computer with an Intel i5 processor, running Ubuntu 15.10 at 64 bits.
-
Create your working environment
First create a new working directory and step into this directory.
mkdir ~/raspberry cd ~/raspberry
-
Retrieve the Raspberry Tools
Clone the Raspberry Pi Tools GIT repository. Among the tools is a complete cross compilation environment.
git clone https://github.com/raspberrypi/tools
-
Create your program
Compose a simple program which outputs a text on the console. Create a file named
~/raspberry/helloworld.c with your favourite text editor like ‘vi‘ or ‘gedit’ containing these contents:#include int main(void){ printf("Hello world!\n\n"); return 0; }
-
Create a Makefile
Create a new file ~/raspberry/Makefile with these contents. Be sure to use TABs instead of SPACEs as indentation characters.
helloworld: helloworld.o $(CC) $(LDFLAGS) helloworld.o -o helloworld helloworld.o: helloworld.c $(CC) $(CFLAGS) -c helloworld.c clean: rm *.o helloworld
-
Compile your program
First we will export the environment variable CC containing as value the location of the cross compiler. At the same line we will start the make process.
export CC=~/raspberry/tools/arm-bcm2708/arm-bcm2708hardfp-linux-gnueabi/bin/arm-bcm2708hardfp-linux-gnueabi-gcc && make
-
Check output
When everything went well, in the current directory two new files are created: helloworld.o and helloworld. The later is of interest. This is our compiled program for the target environment. Check the target architecture by issuing the command:
file helloworld
… the output should read something like this:
helloworld: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux.so.3, for GNU/Linux 3.1.9, not stripped
Here we see that it is compiled for a 32 bit ARM architecture. This is at least completely different as the architecture of the build computer: 64 bit, Intel i5.
-
Transfer to Raspberry Pi
Now we must transfer this file to the Raspberry Pi. For simplicity is assumed that the Raspberry Pi is up-and-running already, including a network connection and SSH-daemon, so that we don’t have to leave our chair. In other cases you can use USB-storage or the SD-card to transfer the file. We can reach the Raspberry Pi via SSH using the host name ‘raspberrypi’ and can login as user ‘pi’ and password ‘raspberry’ (these are the defaults for Raspbian).
scp helloworld pi@raspberrypi:/home/pi/
-
Try your program
Now login into the Raspberry Pi and try your program
ssh pi@raspberrypi ./helloworld
The output should be: “Hello world!”
In case you see the error “Permission denied“, check the execution bit of the program file and try again.
chmod +x helloworld ./helloworld