前些日子又搞起了OpenWRT,之前一直只是使用shell或者Lua实现自己的想法,随着C渐渐使用的顺畅了,打算学习下OpenWRT上程序的编写以及交叉编译。
首先列下目录结构
-
lee@ubuntu:~/OpenWrt-SDK-ar71xx-for-linux-i486-gcc-4.6-linaro_uClibc-0.9.33.2/package$ tree
-
.
-
|-- hiOpenWRT
-
| |-- Makefile
-
| `-- src
-
| |-- hiOpenwrt.c
-
| `-- Makefile
-
|-- Makefile
-
`-- rules.mk
这是在OpenWRT-SDK/package下的文件结构,
需要自己建的目录有hiOpenwrt/ 和hiOpenwrt/src/ ;
需要自己建的文件有:
src下的hiOpenwrt.c
-
#include <stdlib.h>
-
#include <stdio.h>
-
int main(void){
-
printf("Hi,OpenWRT!\n");
-
return 0;
-
}
src下Makefile:src下Makefile:
-
# build hiOpenwrt executable when user executes "make"
-
hiOpenwrt: hiOpenwrt.o
-
$(CC) $(LDFLAGS) hiOpenwrt.o -o hiOpenwrt
-
-
hiOpenwrt.o: hiOpenwrt.c
-
$(CC) $(CFLAGS) -c hiOpenwrt.c
-
-
#remove object files and executable when user executes "make clean"
-
clean:
-
rm *.o hiOpenwrt
当然,在hiOpenwrt/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)))
自己的hiOpenwrt的Makefile自己的hiOpenwrt的Makefile
-
##############################################
-
# OpenWrtMakefile for hiOpenwrt program
-
#
-
#
-
# Most ofthe variables used here are defined in
-
# theinclude directives below. We just need to
-
# specifya basic description of the package,
-
# whereto build our program, where to find
-
# thesource files, and where to install the
-
# compiled program on the router.
-
#
-
# Be verycareful of spacing in this file.
-
# Indentsshould be tabs, not spaces, and
-
# thereshould be no trailing whitespace in
-
# linesthat are not commented.
-
#
-
##############################################
-
include $(TOPDIR)/rules.mk
-
# Nameand release number of this package
-
PKG_NAME:=hiOpenWRT
-
PKG_RELEASE:=1
-
-
# Thisspecifies the directory where we're going to build the program.
-
# Theroot 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
-
-
# Specifypackage information for this program.
-
# Thevariables 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/$(PKG_NAME)
-
SECTION:=utils
-
CATEGORY:=Utilities
-
TITLE:=hiOpenwrt-- 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,
-
# thehelloworld 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/$(PKG_NAME)/install
-
$(INSTALL_DIR) $(1)/bin
-
$(INSTALL_BIN) $(PKG_BUILD_DIR)/hiOpenwrt $(1)/bin/
-
endef
-
-
-
# Thisline executes the necessary commands to compile our program.
-
# Theabove define directives specify all the information needed, but this
-
# linecalls BuildPackage which in turn actually uses this information to
-
# build apackage.
-
$(eval $(call BuildPackage,$(PKG_NAME)))
生成ipk的命令:
-
make package/hiOpenWRT/compile V=s
加上V=s的目的是为了详细打印。
生成出来的ipk文件将在bin/ar71xx/packages/下,同时,也可以在build_dir/ build_dir/下找到可执行文件以及源码。
-
lee@ubuntu:~/OpenWrt-SDK-ar71xx-for-linux-i486-gcc-4.6-linaro_uClibc-0.9.33.2$ tree | head -n 20
-
.
-
|-- bin
-
| |-- ar71xx
-
| | `-- packages
-
| | `-- hiOpenWRT_1_ar71xx.ipk
-
| `-- packages
-
|-- build_dir
-
| `-- build_dir
-
| `-- hiOpenWRT
-
| |-- hiOpenwrt
-
| |-- hiOpenwrt.c
-
| |-- hiOpenwrt.o
-
| |-- ipkg-ar71xx
-
| | `-- hiOpenWRT
-
| | |-- bin
-
| | | `-- hiOpenwrt
-
| | `-- CONTROL
-
| | `-- control
-
| `-- Makefile
-
|-- Config.in
一点总结:
目录结构很重要,如果目录结构不正确,那么Openwrt是不可能正确编译出ipk的。
在学习编译过程中出现了很多处错误,最让我头疼的是Makefile的格式,这部分很容易错误而编译失败。
Makefile编写规则:
-
target ... : prerequisites ...
-
command
-
...
-
...
target也就是一个目标文件,可以是Object File,也可以是执行文件。还可以是一个标签
(Label),对于标签这种特性,在后续的“伪目标”章节中会有叙述。
prerequisites就是,要生成那个target所需要的文件或是目标。
command也就是make需要执行的命令。(任意的Shell命令)
这是一个文件的依赖关系,也就是说,target这一个或多个的目标文件依赖于prerequisi
tes中的文件,其生成规则定义在command中。说白一点就是说,prerequisites中如果有一个以上的文件比target文件要新的话,command所定义的命令就会被执行。这就是 Makefile的规则。也就是Makefile中最核心的内容。
参考资料
http://www.cnblogs.com/laojie4321/archive/2012/03/27/2420241.html
http://blog.chinaunix.net/uid-27194309-id-3432651.html
阅读(3734) | 评论(0) | 转发(0) |