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命令安裝之


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM