Cross-compiling

Cross compiling in Linux

Introduction

To cross compile means to generate object code that is meant to be run on an architecture other than the one you were using to build that object code.

Some well known cross complilation scenarios are:

This document will focus on the latter, for example, on building object code for PowerPC or MIPS while using intel-based Linux.

Figuring out the architecture

UNIX uses three identifiers for each architecture. These are concatenated on a single string. These are: Some well known architecture strings are: First thing you need to figure out is the correct architecture string for your platform. We'll use MIPS Litte-Endian as our target platform, so the correct architecture string will be mipsel-unknown-linux-gnu.

Where to install

Most people like to install the cross compiling utilities in some separate place, like /usr/cross-tools/. I personally like them to reside side-by-side with my current environment. If you like to use my settings change the prefixes in the examples from /usr/cross-tools/ to /usr. I decline any responsability for any damage you might get to your system if you use the /usr prefix. You have been warned!.

Necessary tools

The following examples are for MIPS, but you can use other architectures. The most basic tools you will need are: These two tools will allow for a basic development environment, but they will only allow you to build the linux kernel mostly.

Building binutils

You can fetch binutils from The GNU site. Extract the software to your favorite location.

Enter the directory and configure the package:
./configure --target=mipsel-unknown-linux-gnu --prefix=/usr/cross-tools
Then if everything goes OK, just type:
make

This will build the necessary tools.

The only thing you need to do is install the package. To do so just do:

make install

It will install binutils under the prefix you specified.

You can then check if you have the tools there.

Building gcc

gcc will have to know where you installed binutils. It will use them later to assemble and link the target object code.

Using a cross-environment to build the Kernel

Althrough a kernel-only system is mostly useless, the kernel plays a major role in modern operating systems. If you are looking for your own linux distribution (or if you are building from scratch for an embedded system), you will need to roll your own kernel. To do so, you will use the basic utilities (binutils and gcc) you have built before.