Skip to content

Cross compile test application

Luis edited this page Oct 12, 2015 · 7 revisions

How to cross compile an application for OpenWRT

1. Summary

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.


2. What is 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 make utility.

In that way, If we want to compile an application which needs the 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

We have:

  1. Pc with debian and intel cpu architecture.

  2. 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 arm architecture and compile it in our pc.

The result will be a "Compiled source file" which works in arm platform.


3. "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, second one in other cases)

  1. RPI toolchain

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

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 look to this Table of Hardware and check 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 there is no error 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

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. This 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 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.
####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.

##CONTACT

Netbeast: http://netbeast.co / [email protected]

Clone this wiki locally