-
Notifications
You must be signed in to change notification settings - Fork 6
Cross compile test application
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.
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:
-
Pc with debian and intel cpu architecture.
-
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.
We need a source file and a cross-compiler as we saw before.
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;
}
We will use two different cross-compilers (If you have a RPI use the first one, second one in other cases)
- 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
- 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:
- Choose Package the OpenWrt-based Toolchain (This is the most important thing because this is the cross compiler)
- Exit and save
- 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 toolchainWe 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.
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]
# warning
: now our docs are being published in our gitbook and available on our website. Visit http://docs.netbeast.co/
- Visit our site https://netbeast.co
- Mail us: staff [at] netbeast.co
- Report a bug or enter discussion at issues
- Other resources: Dashboard, Netbeast API