Compile helloworld package!0

After downloading the virtual machine with OpenWrt already compiled from here, let's compile the helloworld package for our Maestro router. Open the terminal on the Virtual Machine and execute following commands (lines preceded by // are comments and should not be typed):
// Go to folder code
cd ~/code
// Create a folder apps
mkdir -p apps
// Go to folder apps
cd apps
// Create a folder helloworld
mkdir helloworld
// Go to folder helloworld
cd helloworld
// Create two files: main.c Makefile
touch main.c Makefile
File main.c contains the source code of our application. File Makefile contains the instructions needed to compile it. Edit the file main.c with your favourite text editor and add following lines:
#include <stdio.h>
int main(void)
{
printf ("Hello from M&F's Hackathon in BCN!\n");
return 0;
}
Edit the file Makefile so it looks like this:
.SUFFIXES: .tar.gz .c
PKG_RELEASE:=1
PKG_VERSION:=0
TARGET = helloworld
SOURCES = main.c
OBJECTS = $(SOURCES:.c=.o)
LDFLAGS += -lpthread
OPENPATH=~/code/barrier_breaker/dl/$(TARGET).tar.gz
$(TARGET): $(OBJECTS)
$(CC) $(CFLAGS) $(LIBS) $(OBJECTS) -o $(TARGET) $(LDFLAGS)
copy:
mkdir p ./$(TARGET)$(PKG_VERSION).$(PKG_RELEASE)
cp rf $(SOURCES) Makefile ./$(TARGET)$(PKG_VERSION).$(PKG_RELEASE)
tar cz -f $(TARGET).tar.gz ./$(TARGET)$(PKG_VERSION).$(PKG_RELEASE)
cp ./$(TARGET).tar.gz ~/code/barrier_breaker/dl/
rm rf .tar $(TARGET) $(TARGET)$(PKG_VERSION).$(PKG_RELEASE) *.o *~
all: $(TARGET)
# Objects
%.o: %.c
$(CC) -c $(CFLAGS) $< -o $@
clean:
rm rf .tar $(TARGET) $(TARGET)$(PKG_VERSION).$(PKG_RELEASE) *.o *~ $(OPENPATH)
From folder ~/code/apps/helloworld, execute following commands:
// Compile the application
make
// Run the application
./helloworld
Once you run the application you should see the text Hello from M&F's Hackathon in BCN! printed to your terminal. Again from folder ~/code/apps/helloworld execute following commands to remove the executable from this folder and copy the source files to the right folder for OpenWrt:
make clean
make copy
Execute following commands from the terminal:
// Create folder helloworld so OpenWrt knows about the new package
mkdir ~/code/barrier_breaker/feeds/packages/utils/helloworld
// Create the Makefile so OpenWrt can compile the package
touch ~/code/barrier_breaker/feeds/packages/utils/helloworld/Makefile
Edit ~/code/barrier_breaker/feeds/packages/utils/helloworld/Makefile so it looks like this:
include $(TOPDIR)/rules.mk
PKG_NAME:=helloworld
PKG_VERSION:=0
PKG_RELEASE:=1
PKG_SOURCE:=$(PKG_NAME).tar.gz
PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)-$(PKG_VERSION).$(PKG_RELEASE)
include $(INCLUDE_DIR)/package.mk
define Package/$(PKG_NAME)
SECTION:=utils
CATEGORY:=Utilities
DEPENDS:=+libpthread
TITLE:=Hello World app
endef
define Package/$(PKG_NAME)/description
This is a sample application that simply prints the Hello World message to the screen.
endef
define Build/Configure
endef
define Build/Compile
$(MAKE) -C $(PKG_BUILD_DIR) $(TARGET_CONFIGURE_OPTS)
endef
define Package/$(PKG_NAME)/install
$(INSTALL_DIR) $(1)/bin
$(INSTALL_BIN) $(PKG_BUILD_DIR)/$(PKG_NAME) $(1)/bin/
endef
$(eval $(call BuildPackage,helloworld))
Go back to the terminal and execute following commands:
// Go to folder ~/code/barrier_breakercd ~/code/barrier_breaker
// Update all feeds
./scripts/feeds update -a
./scripts/feeds install -a
// Select your package to be compiled
make menuconfig
// Go to utils, find helloworld package and press the space bar twice
// so the package is marked with an *. Then save and exit.
// Finally compile your package
make package/helloworld/compile
To test that your package runs correctly on your router you will need SSH access . After you have configured access via SSH to your router, you can copy the compiled package to the router with following command:
scp ~/code/barrier_breaker/bin/ramips/packages/packages/helloworld_0-1_ramips_24kec.ipk root@192.168.1.1:/tmp
Now install the package and run it:
// Access your router via SSH
ssh root@192.168.1.1
// Go to /tmp folder where the package was copied
cd /tmp
// Install the package
opkg install helloworld_0-1_ramips_24kec.ipk
// Run the package
helloworld
After running the application, the router should print following text to screen: Hello from M&F's Hackathon in BCN! The source code is available to download as well:
Hello World package