打包用户程序到openwrt的固件bin文件
知道用户程序代码工程建立目录位置:openwrt/trunk/package,就可以开始建立工程了
1、建框架
进入openwrt/trunk/package,创建目录helloop,进入hellop目录,创建Makefile文件和src目录,进入src目录,创建helloop.c和Makefile
2、编辑helloop.c和同层makfile
/*******helloop.c start*********/
#include
int main()
{
printf("hello openwrt!\n");
return 0;
}
/************helloop.c end ********/
/*******Makefile*********/
# build helloworld executable when user executes "make"
helloop: helloop.o
$(CC) $(LDFLAGS) helloop.o -o helloop
helloop.o: helloop.c
$(CC) $(CFLAGS) -c helloop.c
# remove object files and executable when user executes "make clean"
clean:
rm *.o helloop
/************makefile*********/
3、编辑上层makefile,即helloop目录的makefile
include $(TOPDIR)/rules.mk
PKG_NAME:=helloop
PKG_RELEASE:=1
PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME)
include $(INCLUDE_DIR)/package.mk
define Package/helloop
SECTION:=utils
CATEGORY:=Utilities
TITLE:=helloop -- prints a snarky message
DEPENDS:= +libc
# DEPENDS:=+libstdcpp +libc
endef
define Build/Prepare
mkdir -p $(PKG_BUILD_DIR)
$(CP) ./src/* $(PKG_BUILD_DIR)/
endef
define Package/helloop/install
$(INSTALL_DIR) $(1)/bin
$(INSTALL_BIN) $(PKG_BUILD_DIR)/helloop $(1)/bin/
endef
$(eval $(call BuildPackage,helloop))
4、menuconfig配置勾选
在openwrt源码顶层目录make menuconfig后,
进入Utilities --->可以看到helloop,勾选。
<*> helloop.............. helloop -- prints a snarky message
5、编译整个工程,可以将用户程序打包至固件bin文件。
Make V=s
附:makefile解析
- #-----官方文档如下
- This is the OpenWrt SDK. It contains a stripped-down version of the buildroot. You can use it to test/develop packages without having to compile your own toolchain or any of the libraries included with OpenWrt.
- To use it, just put your buildroot-compatible package directory in the subdir 'package/' and run 'make' from this directory.
-
-
- #------ OPENWRT集成非官方包之Makefile规则
- include $(TOPDIR)/rules.mk
-
-
- PKG_NAME:=[软件包名字 和文件夹名称一样]
- PKG_VERSION:=[软件包版本 自己写个]
- PKG_RELEASE:=1
- PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME)
- include $(INCLUDE_DIR)/package.mk
- define Package/$(PKG_NAME)
- SECTION:=utils
- CATEGORY:=[软件包在menuconfig里的位置 比如Base system]
- DEPENDS:=[依赖包 两个之间通过空格分隔 前面加+为默认显示 选中该软件包自动选中依赖包 不加+为默认不显示 选中依赖包才显示]
- TITLE:=[标题]
- PKGARCH:=[平台 比如ar71xx 全部写all]
- MAINTAINER:=[作者]
- endef
- define Package/$(PKG_NAME)/description
- [软件包简介]
- endef
-
- #非本目录下的源码文件, 拷贝到此相应目录下.
-
- # 如../../xucommon/xucommon.c, 则将 xucommon.c 拷贝到此目录下的源码的 ../../
- define Build/Prepare
- mkdir -p $(PKG_BUILD_DIR)
- $(CP) ./src/* $(PKG_BUILD_DIR)/
- endef
-
- define Build/Configure
- endef
-
- define Build/Compile
- endef
-
- define Package/$(PKG_NAME)/conffiles
- [升级时保留文件/备份时备份文件 一个文件一行]
- endef
-
- define Package/$(PKG_NAME)/install
- $(CP) ./files/* $(1)/
- endef
-
- define Package/$(PKG_NAME)/preinst
- [安装前执行的脚本 记得加上#!/bin/sh 没有就空着]
- #!/bin/sh
- uci -q batch <<-EOF >/dev/null
- delete ucitrack.@aria2[-1]
- add ucitrack aria2
- set ucitrack.@aria2[-1].init=aria2
- commit ucitrack
- EOF
- exit 0
- endef
-
- define Package/$(PKG_NAME)/postinst
- [安装后执行的脚本 记得加上#!/bin/sh 没有就空着]
- #!/bin/sh
- rm -f /tmp/luci-indexcache
- exit 0
- endef
-
- Package/$(PKG_NAME)/prerm
- [删除前执行的脚本 记得加上#!/bin/sh 没有就空着]
- endef
- Package/$(PKG_NAME)/postrm
- [删除后执行的脚本 记得加上#!/bin/sh 没有就空着]
- endef
- $(eval $(call BuildPackage,$(PKG_NAME)))