openwrt 下编译ipk

 

    openwrt插件都是以.ipk方式发布的,ipk可以直接通过opkg安装到系统。上文我们编译了简单的hello,本文探究hello.ipk的编译过程。

一. 环境准备

cd ~/openwrt/bin/ar71xx
tar -xjvf OpenWrt-SDK-ar71xx-for-linux-x86_64-gcc-4.6-linaro_uClibc-0.9.33.2.tar.bz    #解压SDK包
cd OpenWrt-SDK-ar71xx-for-linux-x86_64-gcc-4.6-linaro_uClibc-0.9.33.2/package     #进入package目录

mkdir hello      #建立软件包目录

  

目录层次:
package/hello
               |--- /src/hello.c

               |--- /src/Makefile
               |--- Makefile

 

二. 编码
1. 编辑编译hello的Makefile
vi hello/src/Makefile

hello: hello.o
	$(CC) $(LDFLAGS) hello.o -o hello
hello.o: hello.c
	$(CC) $(CFLAGS) -c hello.c

clean:
	rm *.o hello

 
2. 编辑发布hello.ipk的Makefile
vi hello/Makefile

#Makefile
include $(TOPDIR)/rules.mk

# Nameand release number of this package
PKG_NAME:=hello
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 youare running Kamikaze, delete the DESCRIPTION
# variable below and uncomment the Kamikaze define
# directivefor the description below
define Package/hello
	SECTION:=utils
	CATEGORY:=Utilities
	TITLE:=Helloworld-- prints a snarky message
endef


# Specifywhat needs to be done to prepare for building the package.
# In ourcase, we need to copy the source files to the build directory.
# This isNOT the default.  The default uses thePKG_SOURCE_URL and the
#PKG_SOURCE which is not defined here to download the source from the web.
# Inorder to just build a simple program that we have just written, it is
# mucheasier to do it this way.
define Build/Prepare
	mkdir -p $(PKG_BUILD_DIR)
	$(CP) ./src/* $(PKG_BUILD_DIR)/
endef

# We donot need to define Build/Configure or Build/Compile directives
# Thedefaults are appropriate for compiling a simple program such as this one
# Specifywhere and how to install the program. Since we only have one file,
# thehello executable, install it by copying it to the /bin directory on
# therouter. 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
# commandto copy the binary file from its current location (in our case the build
#directory) to the install directory.
define Package/hello/install
	$(INSTALL_DIR) $(1)/bin
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/hello $(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 apackage.
$(eval $(call BuildPackage,hello))

 

三. 编译

cd ~/openwrt/bin/ar71xx/OpenWrt-SDK-ar71xx-for-linux-x86_64-gcc-4.6-linaro_uClibc-0.9.33.2    #sdk根目录
make    #编译

    如果顺利,会出现以下信息:

    

make[1]  world
make[2]  package/compile
make[3]  -C package/hello compile
make[2]  package/index

    编译成功会在 bin/arr71xx/packages 下生成 hello_1_ar71xx.ipk 


四. 上传并测试

 


 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值