OpenWrt增加软件包的方法

来自WRTnode Wiki
跳转至: 导航搜索
此页面有未被标记进行翻译的更改

其他语言:
English • ‎中文(中国大陆)‎

搭建环境

搭建编译环境参考:搭建OpenWrt交叉编译环境如何编译一个WRTnode的固件两篇文档。
按照文档,搭建了OpenWrt交叉编译环境及下载、编译WRTnode的sdk。当然你也可以在OpenWrt官网下载源码编译。

添加软件包

添加软件包最常见的方法是Build the OpenWrt SDK,今天为大家奉上一种简单的方法。
$(TOPDIR)目录下有package文件夹,就在这个文件夹下添加自己的软件包。就添加万能的hello wrold

编辑Makefile

  • 进入package目录
cd package
  • 在package目录下创建helloworld目录,并进入helloworld目录
mkdir helloworld
cd helloworld
  • 在helloworld目录下,创建新目录 src之后进入到src目录;在src目录下新建helloworld.c及编译helloworld.c所需要的Makefile文件
mkdir src
cd src
touch helloworld.c
touch Makefile
  • 编辑helloworld.c及编译helloworld.c所需要的Makefile文件
vim helloworld.c
#include <stdio.h>
#include <unistd.h>
int main(void)
{
	printf("a helloworld ipk for OpenWrt !!! \n");
	return 0;
}
vim Makefile
 #build a Makefile for helloworld.c
 helloworld: helloworld.o
     $(CC) $(LDFLAGS) helloworld.o -o helloworld
 helloworld.o: helloworld.c
     $(CC) $(CFLAGS) -c helloworld.c
 clean:
     rm *.o helloworld
  • 编辑Makefile,这个Makefile是给OpenWrt读的,是用来生成一个OpenWrt的软件包;在上一步中我们写的Makefile是为了编译helloworld.c的;可以看出两个Makefile差别很大,也不在同一层目录下
cd ../
touch Makefile

注: ../ 表示上一层目录

  • 编辑Makefile
vim Makefile
###############################################
#OpenWrt Makefile for helloworld program
##############################################
include $(TOPDIR)/rules.mk
# Name and release number of this package
PKG_NAME:=helloworld
PKG_RELEASE:=1
# This specifies the directory where we're going to build the program.
# The root build directory, $(BUILD_DIR), is by default the  build_mipsel
# directory in your OpenWrt SDK directory
PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME)
include $(INCLUDE_DIR)/package.mk
# Specify package information for this program.
# The variables defined here should be self explanatory.
# If you are running Kamikaze, delete the DESCRIPTION
# variable below and uncomment the Kamikaze define
# directive for the description below
define Package/helloworld
	SECTION:=utils
	CATEGORY:=Utilities
	TITLE:=Helloworld -- prints a snarky message
endef
# Uncomment portion below for Kamikaze and delete DESCRIPTION variable above
define Package/helloworld/description
    If you can't figure out what this program does, you're probably
    brain-dead and need immediate medical attention.
endef
# Specify what needs to be done to prepare for building the package.
# In our case, we need to copy the source files to the build directory.
# This is NOT the default.  The default uses the PKG_SOURCE_URL and the
# PKG_SOURCE which is not defined here to download the source from the web.
# In order to just build a simple program that we have just written, it is
# much easier to do it this way.
define Build/Prepare
	mkdir -p $(PKG_BUILD_DIR)
	$(CP) ./src/* $(PKG_BUILD_DIR)/
endef
# We do not need to define Build/Configure or Build/Compile directives
# The defaults are appropriate for compiling a simple program such as   this one
# Specify where and how to install the program. Since we only have one  file,
# the helloworld executable, install it by copying it to the /bin directory on
# the router. The $(1) variable represents the root directory on the router running
# OpenWrt. The $(INSTALL_DIR) variable contains a command to prepare the install
# directory if it does not already exist.  Likewise $(INSTALL_BIN) contains the
# command to copy the binary file from its current location (in our case the build
# directory) to the install directory.
define Package/helloworld/install
	$(INSTALL_DIR) $(1)/bin
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/helloworld $(1)/bin/
endef
# This line executes the necessary commands to compile our program.
# The above define directives specify all the information needed, but this
# line calls BuildPackage which in turn actually uses this information to build a package.
$(eval $(call BuildPackage,helloworld))

如果你感觉文件太多不知道位置关系,献上一张截图:

Helloworld.png

选中软件包

make menuconfig
Utilities  --->  [*]helloworld

编译软件包

返回到$(TOPDIR)目录,编译:

make package/helloworld/compile V=s

这条命令仅仅是编译这一个软件包,所以用时会比较短。如果编译过程中没有错误,进入目录$(TOPDIR)/bin/ramips/packages下就可以看到helloworld_1_ramips_24kec.ipk
clean 这个软件包:

make package/helloworld/clean V=s

当然这两条命令合在一起用:

make package/helloworld/{clean,compile} V=s

如果你想把软件包编译进固件里还是需要用:

make V=s

运行软件包

SCP命令将helloworld_1_ramips_24kec.ipk 上传到WRTnode上
在WRTnode开发板上执行

opkg install /tmp/helloworld_1_ramips_24kec.ipk

执行 helloworld 查看程序的效果。

helloworld

出现打印信息:

 a helloworld ipk for OpenWrt !!!

为避免复制粘贴等操作对Makefile格式的影响,我们提供 helloworld package,可以直接下载,编译。
创建软件包Makefile的详解请查阅openwrt wiki