Skip to content

Cross compile test application

Jon Senra Dearle edited this page Oct 30, 2015 · 7 revisions

How to cross compile an application for OpenWRT

Firstly, we will explain what cross-compilation is and why we do need this for our OpenWRT system. Secondly, We will create a very simple example, a "Hello World" application.


About Cross-compilation

OpenWrt is described as a Linux distribution for embedded devices.

However, you can't compile some applications like you do in Linux distributions (Ubuntu,Debian...) because OpenWRT doesn't have a make utility.

If we want to compile an application that need a make utility we need to use a cross-compiler.

A cross-compiler is a compiler capable of creating executable code for a platform other than the one on which the compiler is running.

Look at this scheme.

SOURCE ---------> CROSS COMPILER -------> COMPILED SOURCE

For this example we had:

  1. A computer with debian and intel cpu architecture.

  2. A RPi2 with OpenWRT and arm cpu architecture.

  • We can't compile the "Source" file in the rpi because it doesn't have the make utility.
  • We can't compile the "Source" file in our pc and then copy that in the rpi because they have different cpu architectures (intel vs arm) and it doesn't work.

We need a cross-compiler that simulates RPi's arm architecture and compile it on our pc.

The result will be a compiled source file which works in the arm platform.


"Hello Netbeast World" example application

We need a source file and a cross-compiler as we saw before.

Source File

In this case, we will use a simple hello world application programmed in c.

Copy this code and save it as example.c

#include<stdio.h>

int main () {
	printf("Hello Netbeast World\n");
	return 0;
}

Cross-Compiler

We will use two different cross-compilers (If you have a RPi use the first one, use the second one in other cases)

  1. RPI toolchain

This cross-compiler has been made for cross-compiling for the RPI. This ONLY works with this kind of device.

You can download it here : Rpi cross-compiler

git clone https://github.com/raspberrypi/tools
  1. OpenWRT toolchain

This cross-compiler has been provided by OpenWRT developers. This tool can support a lot of architectures (RPi included). You can check this Table of Hardware and find out if your device is supported.

You need to download the OpenWRT system

git clone git://git.openwrt.org/15.05/openwrt.git

Prepare system for compilation:

cd openwrt
./scripts/feeds update -a
./scripts/feeds install -a

Select your system architecture

make menuconfig

If no errors pop up a shell script menu will be showed.

Choose your correct system architecture:

menu

Choose Package the OpenWrt-based toolchain (This is the most important thing because this is the cross compiler)

toolchain

Exit and save

exit_toolchain

Compile

make

Now relax. After some time, you will get your toolchain compiled for your selected architecture.

###Cross-compilation

If you want to do this with the Raspberry pi Toolchain go to this section

If you want to do this with the OpenWRT Toolchain go to this section

Cross-Compilation With RPI toolchain

We have to choose the gcc compiler. Gcc is used for compiling C applications

tools/arm-bcm2708/gcc-linaro-arm-linux-gnuebihf-raspbian/bin/arm-linux-gnueabihf-gcc -static hello_world.c

The -static option allows to compile the application with required libraries statically. The result will be an a.out file. Copy it to your OpenWRT system and execute it

./a.out

You will see

"Hello Netbeast World"

You have cross-compiled your first application successfully.

Cross-Compilation with OpenWRT toolchain

We have to choose the gcc compiler. This is used for compiling C applications

openwrt/staging_dir/toochain-NAMEOFYOURARCHITETURE--SOMESTUFF/bin/yoursystem-gcc -static hello_world.c 
  • The -static option allow to compile the application with required libraries statically.

The result will be an a.out file.

Copy that to your OpenWRT sytem and execute it

./a.out

You will see

"Hello Netbeast World"
  • You have cross-compiled your first application successfully.

Clone this wiki locally