第一步创建目录编写代码
首先在<BUILDROOT>/package/
目录下创建一个目录,其名称一般为你要添加的软件包的名称。比如 helloworld,然后在<BUILDROOT>/package/helloworld
添加一个Makefile文件并且创建一个目录 src,用来保存自己写的代码。
src中加入两个文件 helloworld.c 和 Makefile。结构如下:
- 将以下内容分别拷进src的hellworld与Makefile
#include <stdio.h>
void main()
{ printf("hello word!\n");}
helloworld : helloworld.o
$(CC) $(LDFLAGS) helloworld.o -o helloworld
helloworld.o : helloworld.c
$(CC) $(CFLAGS) -c helloworld.c
clean :
rm *.o helloworld
- 将以下内容复制到外层Makefile
cd .. ; vi Makefile
include $(TOPDIR)/rules.mk
PKG_NAME:=helloworld
PKG_VERSION:=1.0
PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)
include $(INCLUDE_DIR)/package.mk
define Package/helloworld
CATEGORY:=Helloworld
TITLE:=Helloworld -- by hzlarm
endef
define Build/Prepare
mkdir -p $(PKG_BUILD_DIR)
$(CP) ./src/* $(PKG_BUILD_DIR)/
endef
define Package/helloworld/install
$(INSTALL_DIR) $(1)/bin
$(INSTALL_BIN) $(PKG_BUILD_DIR)/helloworld $(1)/bin/
endef
$(eval $(call BuildPackage,helloworld))
第二步 编译
到顶层目录 make menuconfig
配置(默认原始SDK都配置好,可以跑通的前提下)
选中为*:(这里选择 * 将其编译进固件。或者选为M编译模块)
保存编译 make V=s
或者make V=99
都可以,编译时打印更多的信息。在openwrt/include/verbose.mk
文件里面说如果V=99,那么V=s。
模块编译选择 make package/helloworld/compile V=s
第三步 运行
- 把生成文件拷到开发板
openwrt/bin/ramips/packages/base/helloworld_xxx.ipk
安装
opkg install helloworld_xxx.ipk
$ which helloworld
/bin/helloworld
- 把固件烧写进去,因为helloworld已经包含在固件中。
openwrt/bin/<arch>/xxx.bin
就是编译生成的文件。