A cross compiler is a compiler that runs on one platform/architecture but generates binaries for another platform/architecture. With devices like the Raspberry Pi, where you really don’t have much CPU or memory to work with, if you’re doing any heavy compiling (like when working on the kernel) a cross compiler is the only way to go.

While there are a lot of different methods for building cross-compilers, by far the quickest and easiest is to use crosstool-ng. This is a set of scripts that bring up a menuconfig-like interface to choose your compiler settings, then goes off and downloads what it needs, patches it, configures it, builds it and installs it all for you.

For this article I use:

  1. Slackware64 14.0 (Linux)
  2. crosstool-ng

Crosstool-NG Overview

Crosstool-ng is a set of scripts that bring up a menuconfig-like interface to choose compiler settings. The project aims to build a toolchain. Crosstool-ng also support alternative components such as uClibc, glibc, or eglibc.

Insight

Our example will build a cross compiler toolchain for Raspberry Pi, which use ARMv6 and running Linux as operating system. This will assume our OS on Pi only Linux and we cannot use this toolchain to build for other OS (RISC OS, etc).

Obtain Material

Download crosstool-ng from their official site. The latest version available per August 30th 2013 is crosstool-ng 1.18. Once the download complete, extract the content.

We also need to install crosstool-ng to our system. Let’s say we want to install it to /opt/crosstool-ng

To complete above, do following:

For installation of crosstool-ng, make sure you do make install using root privileges.

Preparing Environment

We need a working directory for compiling our cross-compiler. This will contain toolchain configuration, downloaded files, and intermediary build results. This is not where the final location the toolchain will end up. You should reserve some space, at least 3.5GB.

Create a directory, you choose it. For example, I will use ~/working as the working directory. Go to that directory.

Compilation & Installation

Go to working directory.

Run ct-ng script to display you a configuration menu. Make sure /opt/crosstool-ng/bin is in your search path.

You should see some configuration options there.

Here is the important stuff:

Path and misc options

  • Enable “Try features marked as EXPERIMENTAL”.
  • Change the prefix directory, from ${HOME}/x-tools/${CT_TARGET} to /opt/cross/x-tools/${CT_TARGET}

Target options

  • Change the “Target Architecture” to arm
  • Change the “Endiannes” to Little endian
  • Change the “Bitness” to 32-bit

Operating System

  • Change “Target OS” to Linux

Binary utilities

  • Change “binutils version” to the latest one you can find which isn’t marked as experimental (at this time 2.22)

C Compiler

  • Enable “Show Linaro versions”
  • In the gcc version field, choose the Linaro version of gcc

Once the configuration done, save it.

To build the toolchain, do following:

This should take some times, minutes in my case.

Once it done, you should have cross toolchain reside on /opt/cross/x-tools/arm-unknown-linux-gnueabi/bin. You might want to add this to your search path.

Testing

First, test the version

Now test for the compilation. Write this source code as test.c.

and compile it.

Leave a Reply

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

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">