编译OpenWrt的应用程序可以参考OpenWrt内部其他应用程序的例程,来编写成自己的应用程序
一、OpenWrt源代码获取与编译
1.1、搭建环境
下载OpenWrt的官方源码:
c
git clone https://github.com/openwrt/openwrt.git
1.2、安装编译依赖项
c
sudo apt update -y
sudo apt full-upgrade -y
sudo apt install -y ack antlr3 asciidoc autoconf automake autopoint binutils bison build-essential \
bzip2 ccache cmake cpio curl device-tree-compiler fastjar flex gawk gettext gcc-multilib g++-multilib \
git gperf haveged help2man intltool libc6-dev-i386 libelf-dev libglib2.0-dev libgmp3-dev libltdl-dev \
libmpc-dev libmpfr-dev libncurses5-dev libncursesw5-dev libreadline-dev libssl-dev libtool lrzsz \
mkisofs msmtp nano ninja-build p7zip p7zip-full patch pkgconf python2.7 python3 python3-pyelftools \
libpython3-dev qemu-utils rsync scons squashfs-tools subversion swig texinfo uglifyjs upx-ucl unzip \
vim wget xmlto xxd zlib1g-dev python3-setuptools
1.3、更新 feeds
进入openwrt目录后执行以下指令
c
./scripts/feeds update -a
./scripts/feeds install -a
1.4、配置编译选项
根据自己的平台来选择编译选项
c
make menuconfig
1.5、下载 dl 库
进入openwrt目录后执行以下指令
c
make download -j8
1.6、编译
c
make V=s -j1
二、OpenWrt应用源代码分析
OpenWrt应用源代码存放目录为package/utils
以fbtest应用程序为例进行分析。在fbtest文件夹下面有一个Makefile文件和src文件夹,而src文件夹下有模块源码fbtest.c和源码编译Makefile
2.1、顶层makefile分析
顶层makefile内容如下
c
#
# Copyright (C) 2012 OpenWrt.org
#
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.
#
include $(TOPDIR)/rules.mk
PKG_NAME:=fbtest
PKG_RELEASE:=1
PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME)
include $(INCLUDE_DIR)/package.mk
define Package/fbtest
SECTION:=utils
CATEGORY:=Utilities
TITLE:=Frame buffer device testing tool
DEPENDS:=@DISPLAY_SUPPORT
endef
define Build/Configure
endef
define Build/Compile
$(MAKE) -C $(PKG_BUILD_DIR) \
CC="$(TARGET_CC)" \
CFLAGS="$(TARGET_CFLAGS) -Wall" \
LDFLAGS="$(TARGET_LDFLAGS)"
endef
define Package/fbtest/install
$(INSTALL_DIR) $(1)/usr/sbin
$(INSTALL_BIN) $(PKG_BUILD_DIR)/fbtest $(1)/usr/sbin/
endef
$(eval $(call BuildPackage,fbtest))
2.2、源码makefile
c
CC = gcc
CFLAGS = -Wall
OBJS = fbtest.o
all: fbtest
%.o: %.c
$(CC) $(CFLAGS) -c -o $@ $<
fbtest: $(OBJS)
$(CC) -o $@ $(OBJS)
clean:
rm -f fbtest *.o
2.3、源码fbtest.c
fbtest.c就是应用程序的实际代码
三、编写自己的OpenWrt应用代码
将fbtest文件夹复制为myapp_test,放置到fbtest相同的目录下
3.1、顶层makefile
修改顶层makefile如下:
c
#
# Copyright (C) 2012 OpenWrt.org
#
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.
#
include $(TOPDIR)/rules.mk
PKG_NAME:=myapp_test
PKG_RELEASE:=1
PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME)
include $(INCLUDE_DIR)/package.mk
define Package/myapp_test
SECTION:=utils
CATEGORY:=Utilities
TITLE:=Frame buffer device testing tool
DEPENDS:=@DISPLAY_SUPPORT
endef
define Build/Configure
endef
define Build/Compile
$(MAKE) -C $(PKG_BUILD_DIR) \
CC="$(TARGET_CC)" \
CFLAGS="$(TARGET_CFLAGS) -Wall" \
LDFLAGS="$(TARGET_LDFLAGS)"
endef
define Package/myapp_test/install
$(INSTALL_DIR) $(1)/usr/sbin
$(INSTALL_BIN) $(PKG_BUILD_DIR)/myapp_test $(1)/usr/sbin/
endef
$(eval $(call BuildPackage,myapp_test))
3.2、源码makefile
c
CC = gcc
CFLAGS = -Wall
OBJS = myapp_test.o
all: myapp_test
%.o: %.c
$(CC) $(CFLAGS) -c -o $@ $<
myapp_test: $(OBJS)
$(CC) -o $@ $(OBJS)
clean:
rm -f myapp_test *.o
3.3、源码myapp_test.c
c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <fcntl.h>
int main (int argc,char **argv)
{
printf ("myapp_test\n");
return 0;
}
四、编译自己的OpenWrt应用代码
4.1、配置
在OpenWrt源码顶级目录下输入下面的命令配置内核
c
make menuconfig
依次按照如下顺序选择:
4.2、编译
在OpenWrt源码顶级目录下输入下面的命令编译应用程序
c
make package/myapp_test/compile V=s
编译完成之后会在下面的目录中看到我们的应用程序
此应用程序和普通的linux应用程序的装载方法一样,不再赘述。