OpenWrt环境下做开发

From WRTnode Wiki Site
Jump to: navigation, search
This page is a translated version of the page Openwrt development and the translation is 100% complete.

Other languages:
English 100% • ‎中文(中国大陆)‎ 100%

1、搭建编译环境在WRTnode固件编译 文档里有详解,我们要做的就是在它基础上编译出一个自己的OpenWrt开发环境。

2、在源码的trunk目录下,执行

$make menuconfig

Openwrt development1.jpg

选中 [*]Build the OpenWrt SDK,保存退出

$make V=s

3、编译完成以后,在trunk/bin/ramips我们会看到OpenWrt-SDK-ramips-for-linux-x86_64-gcc-4.8-linaro_uClibc-0.9.33.2.tar.bz2 压缩包。 这个就是我们编译好的OpenWRT SDK环境。我们将在它下面来做OpenWrt软件包的开发。 下面来解析一下我们的SDK环境的含义,可以从名称上看出,target system是ramips,host system是Linux-x86_64,使用的编译工具以及库是 gcc-4.8-linaro_uClibc-0.9.33.2

4、解压,并进入SDK

$tar -jxvf OpenWrt-SDK-ramips-for-linux-x86_64-gcc-4.8-linaro_uClibc-0.9.33.2.tar.bz2
$cd OpenWrt-SDK-ramips-for-linux-x86_64-gcc-4.8-linaro_uClibc-0.9.33.2
$ls

Openwrt development2.png

我们接下来就要在package这个目录下做操作了。

5、开发hello wrold的软件包

(1)进入package目录:

$cd package

(2)在package目录下创建helloworld目录,并进入helloworld目录:

$mkdir helloworld
$cd helloworld

Openwrt development3.png

(3)在helloworld目录下,创建新目录 src,并进入到src目录,我们要在src目录下来写我们的hellowrold.c以及编译helloworld.c所需要的Makefile文件。

$mkdir src
$cd src
$touch helloworld.c
$touch Makefile

Openwrt development4.png

$vim helloworld.c
#include <stdio.h>
#include <unistd.h>
int main(void)
{
    printf("a hellowrold ipk for openwrt !!! \n");
    return 0;
 }
$vim Makefile
#build a Makefile for helloworld.c
helloworld: helloworld.o
    $(CC) $(LDFLAGS) helloworld.o -o helloworld
helloworld.o: helloworld.c
    $(CC) $(CFLAGS) -c helloworld.c
clean:
    rm *.o helloworld

(4)编写Makefile,这个Makefile文件是给OpenWRT读的。是用来生成软件包的,在上一步中我们写的Makefile是为了编译helloworld.c的,两个Makefile不同,也不在同一层目录下。在上一步中我们是在src目录下,现在我们要编写的Makefile要在上层目录,既是helloworld目录下

$cd ../
$touch Makefile

注: ../ 表示上一层目录

Openwrt development5.png

$vim Makefile
###############################################
#OpenWrt Makefile for helloworld program
##############################################
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))

(5)下面就是编译了,回到SDK的顶层,也就是下面这个目录:

Openwrt6.png

$make V=s

编译过程会在build_dir目录下完成,生成的ipk包,在当前目录下的 bin/ramips/packages下的helloworld_1_ramips_24kec.ipk

(6)将helloworld_1_ramips_24kec.ipk 上传到WRTnode上

如果您是在linux下可以用scp命令来上传你的软件包,使用方法:

$scp xxx/helloworld_1_ramips_24kec.ipk root@192.168.8.1:/usr/bin

xxx/ 填写你的helloworld_1_ramips_24kec.ipk 所在的目录,192.168.8.1是我的WRTnode的ip,当然这是根据你自己的WRTnode开发板的ip决定的

如果你是在windows系统下可以用winscp软件来上传到WRTnode。

(7)在WRTnode开发板上执行

$cd /usr/bin
$opkg install helloworld_1_ramips_24kec.ipk

执行 helloworld 查看程序的效果。

$helloworld

出现打印信息:

a hellowrold ipk for openwrt !!!

这里提供一个可用的OpenWrt SDK

为避免复制粘贴等操作对Makefile格式的影响,我们提供 helloworld package,可以直接下载,编译。

创建软件包Makefile的详解请查阅openwrt wiki

参考文献