How to Build a GCC Cross-Compiler for the Raspberry Pi
I stumbled upon a great howto on building GCC cross compilers by Jeff Preshing and used his script to build a cross-compiler to compile Raspberry Pi programs on my Linux x86 desktop. In this post I explain how I did it and show how it’s like to compile programs with this cross-compiler.
Gather Information From the Raspberry Pi System
I run uname -a
on my Raspberry Pi system to find out the kernel version, which
for me is 3.12.23
.
Configuring the Build
The cross compiler is built on the Linux system on which we want to compile our Raspberry Pi programs.
Download the build shell script from github and place into a newly created empty directory that we’ll use for downloading and building everything.
Open the script and change some variables according to the information gathered from the Raspberry Pi system.
1 2 3 4 5 6 |
|
A added --disable-werror
to the CONFIGURATION_OPTIONS
variable to disable
the -Werror
flag for binutils
compilation. -Werror
causes warnings to
become errors and binutils
was too optimistic to have this enabled for us.
Run and Wait
Install the prerequisites, execute the build_cross_gcc
script, and cross your
fingers.
1 2 3 |
|
The script is easy to understand and self documented. If it fails you will see which command caused the failure and you will be able to find the command inside the script to understand which build step is problematic.
If the script fails to download some files, check this page and download the appropriate packages manually.
You can comment the first lines of the script to avoid running them again when
you call build_cross_gcc
for the thousandth time.
Testing the Cross-Compiler
All the tools we need to compile Raspberry Pi programs are in the
/opt/cross/bin
directory.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
Compile the following C program using our new cross-compiler:
1 2 3 4 5 6 |
|
1 2 |
|
You can check it’s actually an ARM program:
1 2 3 4 5 6 |
|
Send the program to your Raspberry Pi using SSH:
1
|
|
Now we can check if it runs properly on the Raspberry Pi:
1 2 |
|
This is success! Happy cross-compiling.