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.

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

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.