openWRT自学---如何开发新的用户态模块-helloworld

以http://www.gargoyle-router.com/wiki/doku.php?id=openwrt_coding为参考文档

1.要获得openWRT的sdk环境。只要在Backfire的make menuconfig中选择:Build the OpenWRT SDK,然后make即可。会得到一个sdk的压缩包:OpenWrt-SDK-brcm47xx-for-Linux-x86_64-gcc-4.3.3+cs_uClibc-0.9.30.1.tar.bz2 (位置在bin/brcm47xx目录中)。这个SDK就是我们后面开发自己的模块的开发环境了(因为它模拟了我们开发的模块所要工作的目标平台的编译环境)

2.将OpenWrt-SDK-brcm47xx-for-Linux-x86_64-gcc-4.3.3+cs_uClibc-0.9.30.1.tar.bz2拷贝到其他地方并解压缩,然后将得到的目录名改短为:OpenWrt-SDK-brcm47xx;进入此目录后会发现其组成和之前使用的openWRT的开发包大致是一样的。

进入其中的OpenWrt-SDK-brcm47xx/package目录,并开始我们自己的模块,即helloworld的开发。

这里有一篇文档,可以指导如何使用此SDK环境:http://wiki.openwrt.org/doc/howto/obtain.firmware.sdk

3.创建一个子目录:helloworld(代表我们自己的模块),进入helloworld子目录。在此目录中创建一个用于让OpenWRT识别的Makefile。其内容如下:

#
# Copyright (C) 2007-2010 OpenWrt.org
#
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.
#

include $(TOPDIR)/rules.mk

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

define Package/helloworld
    SECTION:=utils
    CATEGORY:=Utilities
    TITLE:=Helloworld -- prints a snarky message
endef

define Package/helloworld/description
    helloworld,first self-made.
endef

define Build/Prepare
    mkdir -p $(PKG_BUILD_DIR)
    $(CP) ./src/* $(PKG_BUILD_DIR)/
endef

define Package/helloworld/install
    $(INSTALL_DIR) $(1)/bin                              ---- 这个决定了最后通过opkg命令,将此ipk安装到设备中的哪个目录?
    $(INSTALL_BIN) $(PKG_BUILD_DIR)/helloworld $(1)/bin/
endef

$(eval $(call BuildPackage,helloworld))

然后,再在此目录下创建一个新的子目录:src,并在其中创建实际的源代码文件(helloworld.c)和针对此源代码文件的Makefile---这个Makefile是用来编译helloworld模块自己的。源代码文件:helloworld.c的内容是:

#include <stdio.h>
#include <unistd.h>
int main(void)
{
     printf("hello world, my first self package \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

特别注意:

1.这里创建的两个Makefile,在ubuntu上,都不要使用Gedit来写,而要使用VIM来写,并特别保证命令之前使用TAB键盘进行空格操作。方能让Makfile识别出来 --- 我在这上面耗费了1天时间啊

2.不能使用make menuconfig来进行配置,否则会导致.config文件中只有一个关于helloworld模块的编译选项,其他与编译环境有关的配置项都消失了。这样会导致无法编译出结果;因此,在编写完上述Makefile和源代码后,只要在主目录上敲入make就可以了  -- 但是这里遗留一个问题:如果存在多个package,然而只要指定编译其中某一个,该怎么办呢?

**********************************************************

4.上述就是文件编写的过程了。如下是编译和使用

在OpenWrt-SDK-brcm47xx目录中,敲入make即可。然后就可以在bin/brcm47xx/packages目录下看到我们的编译成果了:helloworld_1_brcm47xx.ipk

5.接下来是将此文件拷贝到设备中。

首先,在设备中开启ssh服务;

然后,敲入命令:scp helloworld_1_brcm47xx.ipk root@192.168.1.1:      此命令敲入后会提示输入密码,则就把设备上的ssh的登陆密码输入即可。然后此ipk文件就拷贝到设备上的root目录中了   ---- 但是,这样还不是最终结果,我们还需要将此文件装载到设备上

最后,我们通过ssh连接到设备中,并敲入命令:opkg install helloworld_1_brcm47xx.ipk   --- 这样,就真正把此ipk装载到设备中了 :装载到bin目录中

我们验证一下:在设备的任意目录中,敲入helloworld,就可以看到输出了:Hello world, my first self package

6.补充说明

如果我们更新了helloworld的源码包,并再次编译出新的ipk。则要更新之,需要做如下工作:

    首先:将设备中的root目录下的当前的ipk删除之;并且将之前安装到设备的相同文件删除之(这个方法是opkg remove helloworld)

    然后:将新的ipk通过scp命令拷贝到设备中, 并再通过opkg命令安装之

posted @ 2014-04-20 17:35  拉古小师傅  阅读(3863)  评论(0编辑  收藏  举报