Compile Hello World! package

You are here:
Estimated reading time: 2 min

Create a simple Hello World! application

Add the source code directory and files

It is good practice to keep the source code of our applications separated from OpenWrt’s build system. In order to do that, we create a specific directory to save our applications and also one folder for each custom application.

In the vagrant box provided by Maestro, this has been done in the following way:

// This is the folder where the packages of this tutorial will be saved to
mkdir ~/workspace/packages/tutorial
// Create a new folder for the package 'helloworld'
cd ~/workspace/packages/tutorial
mkdir helloworld
// Creata a 'src' folder for the source code of the application
cd helloworld
mkdir src

Now, we create the source code file of our application:

touch ~/workspace/packages/tutorial/helloworld/src/helloworld.c

After that, use your preferred text editor and add the following content to the file helloworld.c:

#include <stdio.h>

int main(void)
{
	printf ("Hello World from Maestro Dev!!!\n");

	return 0;
}

Create GNU makefile

Next to the file helloworld.c add a makefile in order to compile the source code with GNU make:

touch ~/workspace/packages/tutorial/helloworld/src/makefile

Using a text editor, paste the following content into the file (it is important to respect tabs in this file, otherwise a syntax error will be raised when trying to compile):

TARGET=helloworld

all:$(TARGET)

CC=gcc
CFLAGS=-Wall
LDFLAGS+=

DEPS = $(wildcard *.h)

SRC = $(wildcard *.c)

OBJ = $(patsubst %.c, %.o, $(SRC))

%.o: %.c $(DEPS)
	$(CC) -c -o $@ $< $(CFLAGS)

$(TARGET):$(OBJ)
	$(CC) -o $@ $^ $(LDFLAGS)

.PHONY:clean

clean:
	rm -f $(TARGET) *.o

Test the makefile

Before trying to create a package that can be used with OpenWrt devices, let’s try to compile the application and check if it can run on the vagrant box:

cd ~/workspace/packages/tutorial/helloworld/src
make

If the above command has been successful, two new files (helloworld.o and helloworld) will be added to the app folder. The file helloworld is the binary that you can run to check that the application does what it is supposed to do:

// Run the binary
./helloworld
// This is the output from our application
Hello World from Maestro Dev!!!

Remove the two files created after compiling the source code as they are not needed:

make clean

Create a package for your application

The OpenWrt build system revolves heavily around the concept of packages. For the purpose of this tutorial, we will use the same structure for both packages and the source code of our applications. So, in this case the package is in the folder /home/vagrant/workspace/packages/tutorial/helloworld and its source code will be placed in the directory /home/vagrant/workspace/packages/tutorial/helloworld/src.

Create the package manifest file

Each package in the OpenWrt build system is described by a package manifest file. The manifest file is responsible for describing the package, what it does, and must at least provide instructions on where to obtain the source code, how to build it and which files should be contained in the final installable package.

In order for the source code of our application to become a package, and become a part of the package repository that we previously created, we will need to create a package manifest for it:

touch ~/workspace/packages/tutorial/helloworld/Makefile

With a text editor, paste following content into the Makefile:

include $(TOPDIR)/rules.mk
PKG_NAME:=helloworld
PKG_VERSION:=1.0.0

SOURCE_DIR:=./src

include $(INCLUDE_DIR)/package.mk

define Package/$(PKG_NAME)
	SECTION:=examples
	CATEGORY:=MaestroDev
	SUBMENU:=Tutorial
	TITLE:=Helloworld App
endef

define Package/$(PKG_NAME)/description
	Hello World application by Maestro Dev.
endef

define Build/Prepare
	mkdir -p $(PKG_BUILD_DIR)
	$(CP) $(SOURCE_DIR)/* $(PKG_BUILD_DIR)/
endef

define Package/$(PKG_NAME)/Build/Configure
endef

define Package/$(PKG_NAME)/Build/Compile
endef

define Package/$(PKG_NAME)/install
	$(INSTALL_DIR) $(1)/usr/bin
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/$(PKG_NAME) $(1)/usr/bin/
endef

$(eval $(call BuildPackage,$(PKG_NAME)))

Please, continue to the next article: Build and test the package.

Source: OpenWrt wiki [1, 2 and 3]

Was this article helpful?
Dislike 0 1 of 1 found this article helpful.
Views: 321
Back to Top