openWrt 交叉编译 和 ipk包生成

1、搭建开发环境
首先,我们需要一个为路由器定制的开发环境,在执行make menuconfig后,会出现下图:
menuconfig
其中,
Target Profile (TP-LINK TL-WR720N) 定制路由器的系统版本,大根据不同的路由器进行不同的选择;
[*] Build the OpenWrt SDK 编译一个SDK开发环境(默认情况下,此项未勾选)。
编译过程中需要通过官网下载很多相关的软件包,所以必须保证能够顺利连上外网。由于下载速度的限制,编译过程大概需要数小时。编译结束后,所有的产品都会放在编译根目录下的bin/[Target]/. 例如:我所编译的产物都放在./bin/ar71xx/下,其中文件主要有几类:
(1)bin/[Target]/xxx 文件: 这些都是在我们所选的Target System的类别之下,针对不同路由器型号、版本编译的路由器固件。这些不同路由器的型号和版本是openwrt预先设置好的,我们不需要更改。
(2)packages文件夹: 里面包含了我们在配置文件里设定的所有编译好的软件包。默认情况下,会有默认选择的软件包。
(3)OpenWrt-SDK-*.tar.bz2: 这个也就是我们定制编译好的OpenWRT SDK环境。我们将用这个来进行OpenWrt软件包的开发。例如,我所编译好的SDK环境包为:/bin/ar71xx/OpenWrt-SDK-ar71xx-for-unknown-x86_64-gcc-4.8-linaro_uClibc-0.9.33.2.tar.bz2
可以从名称上看出,target system是ar71xx,host system是unknown-x86_64,使用的编译工具以及库是gcc-4.8-linaro_uClibc-0.9.33.2。
(4)md5sums 文件: 这个文件记录了所有我们编译好的文件的MD5值,来保证文件的完整性,
(5)将OpenWrt-SDK-*.tar.bz2拷贝到开发的目录中,tar xvf OpenWrt-SDK-*.tar.bz2, 会发现该目录与编译openwrt的目录相同,我的目录解压缩为/home/zinc/OpenWrt-SDK-ar71xx-for-unknown-x86_64-gcc-4.8-linaro_uClibc-0.9.33.2

2、交叉编译环境
在使用buildroot对openwrt进行编译之后,在buildroot目录下会有一个名叫staging_dir的目录,针对当前平台的toolchain都在这个目录下。
2.1 增加toolchain的目录到PATH目录中
nano -w ~/.bash_profile
添加代码:
# add openWrt cross-compile path
$PATH:/home/zinc/OpenWrt-SDK-ar71xx-for-unknown-x86_64-gcc-4.8-linaro_uClibc-0.9.33.2/staging_dir/toolchain-mips_34kc_gcc-4.8-linaro_uClibc-0.9.33.2
STAGING_DIR=/home/zinc/OpenWrt-SDK-ar71xx-for-unknown-x86_64-gcc-4.8-linaro_uClibc-0.9.33.2/staging_dir/
export STAGING_DIR

2.2 编译方法
mips-openwrt-linux-uclibc-gcc helloworld.c -o helloworld
./configure –target=mips-openwrt-linux-uclibc
make CC=mips-openwrt-linux-uclibc-gcc LD=mips-openwrt-linux-uclibc-ld

3、IPK环境
对于自己新建的package,而这个package又不需要随固件一起安装,换句话说,就是可以当做一个可选软件包的话。我们可以利用我们的SDK环境来单独编译,编译后会生成一个ipk的文件包。然后利用 opkg install xxx.ipk 来安装这个软件。
3.1 首先,编写helloworld程序
编写helloworld.c

/****************
* Helloworld.c
* The most simplistic C program ever written.
* An epileptic monkey on crack could write this code.
*****************/

#include 
#include 
int main(void)
{
     printf("Hell! O' world, why won't my code compile?\n\n");
     return 0;
}

编写Makefile文件

# build helloworld executable when user executes "make"

helloworld: helloworld.o
	$(CC) $(LDFLAGS) helloworld.o -o helloworld

helloworld.o: helloworld.c
	$(CC) $(CFLAGS) -c helloworld.c

# remove object files and executable when user executes "make clean"
clean:
	rm *.o helloworld

在这两个文件的目录下,执行make 应该可以生成helloworld的可执行文件。执行helloworld后,能够打印出“Hell! O’ world, why won’t my code compile?”。 这一步,主要保证我们的源程序是可以正常编译的。下面我们将其移植到OpenWRT上。

3.2 进入SDK
cd /home/zinc/OpenWrt-SDK-ar71xx-for-unknown-x86_64-gcc-4.8-linaro_uClibc-0.9.33.2
可以看到里面的目录结构跟我们之前source的目录结构基本相同,所需要编译的软件包,需要放置在package目录下

3.3 在package目录下创建helloworld目录
cd package
mkdir helloworld
cd helloworld

3.4 创建src目录,拷贝 helloworld文件
mkdir src
cp /home/zinc/helloworld/helloworld.c src
cp /home/zinc/helloworld/Makefile src

(6)在helloworld目录下创建Makefile文件
这个Makefile文件是给OpenWRT读的,而之前写的那个Makefile文件是针对helloworld给编译其读的。两个Makefile不在同一层目录下。

##############################################
# OpenWrt Makefile for helloworld program
#
#
# Most of the variables used here are defined in
# the include directives below. We just need to
# specify a basic description of the package,
# where to build our program, where to find
# the source files, and where to install the
# compiled program on the router.
#
# Be very careful of spacing in this file.
# Indents should be tabs, not spaces, and
# there should be no trailing whitespace in
# lines that are not commented.
#
##############################################

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))

3.5 返回到SDK的根目录
执行make进行编译
编译过程会在build_dir目录下完成
编译结果会放在 bin/ar71xx/package目录下helloworld_1_ar71xx.ipk

3.6 上传helloworld_1_ar71xx.ipk
使用sftp软件上传helloworld_1_ar71xx.ipk至路由器
执行 opkg install helloworld_1_ar71xx.ipk
输入hello然后按Tab键,发现openwrt中已经有helloworld可执行命令。
执行 helloworld 查看程序的效果。

Hell! O’ world, why won’t my code compile?

关于Zeno Chen

本人涉及的领域较多,杂而不精 程序设计语言: Perl, Java, PHP, Python; 数据库系统: MySQL,Oracle; 偶尔做做电路板的开发,主攻STM32单片机
此条目发表在C/C++, Linux分类目录。将固定链接加入收藏夹。