A Cross-Compiler for Your Raspberry Pi

Your pet dog knows it has to “Fetch” the Frisbee you throw out and to “Sit” when you command it to do so. The dog knows this since it has been trained to learn some commands. As its association with you grows, so does its vocabulary. As the dog is an intelligent creature, it sometimes chooses to ignore your commands, depending on its mood.

Fortunately, the Raspberry Pi or RBPi does not have moods to ignore your commands. Not possessing any intelligence of its own, it faithfully obeys (or tries to) what you throw at it. As a processor, your RBPi can understand only machine level language or MLL made up of two logic states ‘0’ and ‘1’, whereas you usually send it commands in C, BASIC, Python or similar high level languages or HLL.

Linux, the Operating System used in your RBPi has a compiler that translates your HLL codes into the MLL understood by the processor. As not all processors are the same, you must inform the compiler as to what processor it should translate the code for. Therefore, when you write code for your RBPi, the compiler creates a binary file, which consists of the machine code suitable for the processor in your RBPi board.

When you write your program code in HLL, there may be unknown errors called bugs that prevent the program from running smoothly on your RBPi. You have to spend time in debugging your program until the desired result is obtained. Most of the time, the bugs are not obvious and you are not sure why the program crashed or did not operate.

The amount of memory available in the Raspberry Pi and its CPU capabilities are limited. Therefore, when you have to debug and compile a long program, these limitations become a bottleneck. Debugging and compiling a lengthy program on a desktop or a laptop PC running Linux, is far faster than trying to compile it on an RBPi. The only problem is it will not be compiled for the RBPi processor, but rather for the processor within the PC.

The way out is to use a cross-compiler. This will run on the PC platform, but will generate the code necessary for the processor used in the Raspberry Pi. Using crosstool-ng is the simplest way to build a cross-compiler. A set of scripts is used to bring up a menuconfig type of interface allowing you to select your compiler settings. With the necessary inputs, the crosstool-ng downloads what else it needs, patches itself, configures itself, builds the cross-compiler and installs it for you. How to create the cross-compiler is detailed here.

Crosstools are notorious for the large amount of space they take up on the hard disk. This is due to the many files downloaded and intermediary build results necessary to be created when building up the crosstool. Be prepared with a 4-5GB amount of empty space on your HDD, as creating the crosstool-ng cross-compiler will take up at least 3.5GB. The Linaro C-compiler works well for the Raspberry Pi, although this is an experimental version.