Compiling programs for OpenWRT

When I was trying to compile my first OpenWRT program I searched around the web, but didn’t found any useful (at least to me) tutorials/guides that would help me. After spending a few hours behind a computer and figuring out how to do this I found the easiest way to do it.

The are two ways of compiling:

  • simply compiling your application
  • building a package for it

Differences? If you have a package it’s easier to install and uninstall it, and probably easier to update it. However, in most of my cases I just simply want to compile my source code to a single executable and copy it to the device. Here I will show you the easiest way to do it and go from source code to final executable in a few minutes.

What you need?

  • Linux distribution
  • OpenWRT Toolchain for processor which is in the target device

I’m currently focused on tiny routers called TP-Link WR703n, so my toolchain is available at: http://downloads.openwrt.org/attitude_adjustment/12.09/ar71xx/generic/ For others please visit www.openwrt.org and search for toolchain for your device.

Create folder and put create Makefile similar to this:

export STAGING_DIR=<PATH TO YOUR TOOLCHAIN>
 
CC=gcc
 
all: clean program
 
cc: CC=$(STAGING_DIR)/bin/mips-openwrt-linux-gcc
cc: clean program deploy
 
deploy:
scp program root@10.0.0.1:/path/to/where/you/want/to/put/your/executable/
 
program: main.o
$(CC) main.o -o program
 
main.o:
$(CC) -c main.c
 
clean:
rm -rf *.o program

Change path to your toolchain, your device’s IP address, path to where you want to put your executable and name of your C file. If you want to include additional files just copy main.o to something.o, add it to program: section, and change the names accordingly.
If you need to link your program with additional library (e.g. pthread), you can add that to CC variable (e.g. gcc -lpthread).

What can I do with this Makefile?

  • command make – compile program for your PC (compile with the toolchain available on your computer and be able to test it out on it)
  • command make cc – compile program for OpenWRT device and deploy it to the device
  • command make deploy – deploy compiled application to your device

Example file from one of my projects:

export STAGING_DIR=/home/dean/Downloads/OpenWrt-Toolchain-ar71xx-for-mips_r2-gcc-4.6-linaro_uClibc-0.9.33.2/toolchain-mips_r2_gcc-4.6-linaro_uClibc-0.9.33.2
 
CC=gcc -pthread
 
all: clean lightning-service
 
cc: CC=$(STAGING_DIR)/bin/mips-openwrt-linux-gcc -pthread
cc: clean lightning-service deploy
 
deploy:
scp lightning-service root@192.168.0.3:/apps/
 
lightning-service: serial_port.o main.o
$(CC) serial_port.o main.o -o lightning-service
 
main.o:
$(CC) -c main.c
 
serial_port.o:
$(CC) -c serial_port.c
 
clean:
rm -rf *.o lightning-service

This is an example of Makefile for a program that I use to control stairway lights at home. Lights are connected to my router via Launchpad MSP430 microcontroller, and I can communicate with them through TCP. I am also linking pthread library because I need threads (one for listening/communicating with serial port and one for TCP connections).

This is probably the simplest way you can compile your own programs without having to bother with creating your own packages.

Tagged with:
Posted in OpenWRT, Programming