OpenWRT如何創(chuàng)建軟件包

這篇文章給大家分享的是有關(guān)OpenWRT如何創(chuàng)建軟件包的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。

成都創(chuàng)新互聯(lián)從2013年成立,先為南昌等服務(wù)建站,南昌等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為南昌企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。

第一步:生成SDK

make menuconfig 選上 “Build the OpenWRT SDK”

在 trunk目錄下,執(zhí)行:

$ make menuconfig

選擇對(duì)應(yīng)的"Target System"與"Target Profile",并選上"Build the OpenWrt SDK"。

OpenWRT如何創(chuàng)建軟件包

然后 Save,退出。再make一次。

$ make V=99

make 完成之后,在 bin/ar71xx/ 目錄下會(huì)生成SDK的壓縮文件:

OpenWrt-SDK-ar71xx-generic_gcc-4.8-linaro_uClibc-0.9.33.2.Linux-i686.tar.bz2

第二步:安裝SDK

將上面所生成的 OpenWrt-SDK-ar71xx-generic_gcc-4.8-linaro_uClibc-0.9.33.2.Linux-i686.tar.bz2 復(fù)制到其它路徑下(指可以不在OpenWrt的源碼路徑下),再解壓出來。

比如我將其放到 ~/Workspace/OpenWRT/ 路徑下:

$ cp bin/ar71xx/OpenWrt-SDK-ar71xx-generic_gcc-4.8-linaro_uClibc-0.9.33.2.Linux-i686.tar.bz2 ~/Workspace/OpenWRT
$ cd ~/Workspace/OpenWRT
$ tar jxvf OpenWrt-SDK-ar71xx-generic_gcc-4.8-linaro_uClibc-0.9.33.2.Linux-i686.tar.bz2

在 ~/Workspace/OpenWRT/ 路徑下就生成了 OpenWrt-SDK-ar71xx-generic_gcc-4.8-linaro_uClibc-0.9.33.2.Linux-i686 目錄。

為了方便,我將這個(gè)長(zhǎng)長(zhǎng)的目錄名簡(jiǎn)化為:OpenWrt-SDK。修改后,完整的路徑是:~/Workspace/OpenWRT/OpenWrt-SDK

據(jù)說這個(gè)目錄結(jié)構(gòu)跟 OpenWrt的源碼目錄結(jié)構(gòu)差不多。

第三步:創(chuàng)建helloworld項(xiàng)目

其實(shí),這里可以是任意我們想要加入的程序,庫等。這里就以helloword為例。

在任意路徑下,創(chuàng)建helloword項(xiàng)目。比如這里還是在 ~/Workspace/OpeWRT 目錄下。

$ cd ~/Workspace/OpenWRT
$ mkdir helloword
$ cd helloword
$ touch helloword.c Makefile

在 ~/Workspace/OpenWRT/ 目錄下創(chuàng)建了 helloword 目錄,并生成 helloword.c與Makefile文件。

如下為 helloworld.c的內(nèi)容:

#include <stdio.h>

int main()
{
    printf("This is my hello word!\n");
    return 0;
}

Makefile的內(nèi)容:

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

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

clean :
    rm *.o helloworld

首先,確保在程序沒問題,在本地能正常編過。為了檢驗(yàn)一下,可以就地 make 一下,看程序本身有沒有問題。

這個(gè)程序都如些之簡(jiǎn)單了,本人自己了make了一下,OK,再run了一下,正常。

第四步:創(chuàng)建helloworld包

進(jìn)入 OpenWrt/Packages/ 并在該目錄下創(chuàng)建 helloworld 目錄,并進(jìn)入該目錄。

$ cd ~/Workspace/OpenWrt/OpenWrt-SDK/package
$ mkdir helloworld
$ cd helloworld

將我們第三步寫的程序復(fù)制到這個(gè)目錄下來,更名為src。再新建一個(gè) Makefile 文件。

$ cp -r ../../../helloworld src
$ touch Makefile

整個(gè)過程下來,package目錄結(jié)構(gòu)如下:

package
|-- helloworld
|   |-- Makefile
|   `-- src
|       |-- helloworld.c
|       `-- Makefile
`-- Makefile

package/Makefile 不要去修改它。

我們要編寫的是 package/helloworld/Makefile 這個(gè)文件。

在這個(gè)文件中,我們要描述 helloworld 包的信息,比如:如何配置、如何編譯、如何打包、安裝等等信息。
這個(gè)文件與一般的 Makefile 格式還不一樣,詳見OpenWrt上的說明文檔:OpenWrt官網(wǎng)Packages說明

這里我就依照例子編寫 helloworld/Makefile:

include $(TOPDIR)/rules.mk

PKG_NAME:=helloworld
PKG_RELEASE:=1

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
    It's my first package demo.
endef

define Package/helloworld/Prepare
    echo "Here is Package/Prepare"
    mkdir -p $(PKG_BUILD_DIR)
    $(CP) ./src/* $(PKG_BUILD_DIR)/
endef

define Package/helloworld/install
    echo "Here is Package/install"
    $(INCLUDE_DIR) $(1)/bin
    $(INCLUDE_BIN) $(PKG_BUILD_DIR)/helloworld$(1)/bin/
endef

$(eval $(call BuildPackage, helloworld))

然后回到 OpenWrt-SDK 目錄下,執(zhí)行:make V=s,結(jié)果有以下錯(cuò)誤提示:

$ make V=s
ERROR: please fix package/helloworld/Makefile - see logs/package/helloworld/dump.txt for details

說是我寫的這個(gè)Makefile有錯(cuò),請(qǐng)查看 dump.txt 文件。無奈只好去看看到底錯(cuò)在哪里啰。

打開 OpenWrt-SDK/logs/package/helloworld/dump.txt 文件:

Package:  helloworld
Version: 1
Depends: +libc +SSP_SUPPORT:libssp +USE_GLIBC:librt +USE_GLIBC:libpthread 
Conflicts: 
Menu-Depends: 
Provides: 
Section: opt
Category: Extra packages
Title: 
Maintainer: 
Source: 
Type: ipkg
Description: 

@@

Makefile:32: *** invalid syntax in conditional.  Stop.

前面那么多行信息沒什么用,最重要的是最后一行,好像是說32行條件語法錯(cuò)誤。趕緊打開 package/helloworld/Makefile,定位到32行看看。結(jié)果是:

$(eval $(call BuildPackage, helloworld))

這個(gè),我何錯(cuò)之有呢?

最后反復(fù)排查,原來是 "BuildPackage,">。不會(huì)吧!多個(gè)空格少個(gè)空格都會(huì)導(dǎo)致語法錯(cuò)誤?!

好了,改正過來了。

$(eval $(call BuildPackage,helloworld))  #去掉空格

現(xiàn)在 make V=s 不再是剛才那個(gè)錯(cuò)了。

make[3]: Entering directory `/home/hevake_lcj/Workspace/OpenWRT/my-packages/OpenWrt-SDK/package/helloworld'
CFLAGS="-Os -pipe -mno-branch-likely -mips32r2 <此處省略好長(zhǎng)串...>" 
CXXFLAGS="-Os -pipe -mno-branch-likely -mips32r2 <此處省略好長(zhǎng)串...>" 
LDFLAGS="<此處省略好長(zhǎng)串...>" 
make -j1 -C /home/hevake_lcj/Workspace/OpenWRT/my-packages/OpenWrt-SDK/build_dir/target-mips_34kc_uClibc-0.9.33.2/helloworld/. 
AR="mips-openwrt-linux-uclibc-gcc-ar" 
AS="mips-openwrt-linux-uclibc-gcc -c -Os -pipe -mno-branch-likely -mips32r2 -mtune=34kc -fno-caller-saves -fhonour-copts -Wno-error=unused-but-set-variable -msoft-float" 
LD=mips-openwrt-linux-uclibc-ld 
NM="mips-openwrt-linux-uclibc-gcc-nm" 
CC="mips-openwrt-linux-uclibc-gcc" 
GCC="mips-openwrt-linux-uclibc-gcc" 
CXX="mips-openwrt-linux-uclibc-g++" 
RANLIB="mips-openwrt-linux-uclibc-gcc-ranlib" 
STRIP=mips-openwrt-linux-uclibc-strip 
OBJCOPY=mips-openwrt-linux-uclibc-objcopy 
OBJDUMP=mips-openwrt-linux-uclibc-objdump 
SIZE=mips-openwrt-linux-uclibc-size 
CROSS="mips-openwrt-linux-uclibc-" 
ARCH="mips" ;
make[4]: Entering directory `/home/hevake_lcj/Workspace/OpenWRT/my-packages/OpenWrt-SDK/build_dir/target-mips_34kc_uClibc-0.9.33.2/helloworld'
make[4]: *** No targets specified and no makefile found.  Stop.  # 錯(cuò)誤:沒有找 Makefile 文件?。?
make[4]: Leaving directory `/home/hevake_lcj/Workspace/OpenWRT/my-packages/OpenWrt-SDK/build_dir/target-mips_34kc_uClibc-0.9.33.2/helloworld'
make[3]: *** [/home/hevake_lcj/Workspace/OpenWRT/my-packages/OpenWrt-SDK/build_dir/target-mips_34kc_uClibc-0.9.33.2/helloworld/.built] Error 2
make[3]: Leaving directory `/home/hevake_lcj/Workspace/OpenWRT/my-packages/OpenWrt-SDK/package/helloworld'
make[2]: *** [package/helloworld/compile] Error 2
make[2]: Leaving directory `/home/hevake_lcj/Workspace/OpenWRT/my-packages/OpenWrt-SDK'
make[1]: *** [/home/hevake_lcj/Workspace/OpenWRT/my-packages/OpenWrt-SDK/staging_dir/target-mips_34kc_uClibc-0.9.33.2/stamp/.package_compile] Error 2
make[1]: Leaving directory `/home/hevake_lcj/Workspace/OpenWRT/my-packages/OpenWrt-SDK'
make: *** [world] Error 2

為什么 build_dir/target-mips_34kc_uClibc-0.9.33.2/helloworld 目錄下沒有 Makefile 文件?

我們要好好排查一個(gè) package/helloworld/Makefile 文件中的 Package/helloworld/Prepare 宏。

define Package/helloworld/Prepare
    echo "Here is Package/Prepare"
    mkdir -p $(PKG_BUILD_DIR)
    $(CP) ./src/* $(PKG_BUILD_DIR)/
endef

好像這個(gè)宏壓根沒有被執(zhí)行到。

為什么呢?

<今天太晚了,明天再整>

<接著昨晚的問題>

最后與例子仔細(xì)比對(duì),發(fā)現(xiàn)原來我將 "Build/Prepare" 寫成了 "Package/helloworld/Prepare"。

最終完整的 Makefile 文件如下:

include $(TOPDIR)/rules.mk

PKG_NAME:=helloworld
PKG_RELEASE:=1

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
    It's my first package demo.
endef

define Build/Prepare   #已修正
    echo "Here is Package/Prepare"
    mkdir -p $(PKG_BUILD_DIR)
    $(CP) ./src/* $(PKG_BUILD_DIR)/
endef

define Package/helloworld/install
    echo "Here is Package/install"
    $(INSTALL_DIR) $(1)/bin
    $(INSTALL_BIN) $(PKG_BUILD_DIR)/helloworld $(1)/bin/
endef

$(eval $(call BuildPackage,helloworld))   #已去除逗號(hào)后面的空格

這次 make -j1 V=s 成功了。生成了 helloworld_1_ar71xx.ipk 。find 一下,看在哪里。

$ find -name helloworld*.ipk
./bin/ar71xx/packages/base/helloworld_1_ar71xx.ipk

第五步:試驗(yàn)helloworld

將剛生成的 helloworld_1_ar71xx.ipk 文件用 scp 傳到目標(biāo)路由上。本人的路由IP為:192.168.1.2

$ scp bin/ar71xx/packages/base/helloworld_1_ar71xx.ipk root@192.168.1.2:
root@192.168.1.2's password: 
helloworld_1_ar71xx.ipk                 100% 1993     2.0KB/s   00:00

SSH登陸路由器,并安裝 helloworld_1_ar71xx.ipk包。

$ ssh root@192.168.1.2
root@192.168.1.2's password: 

BusyBox v1.23.2 (2015-05-03 12:46:04 CST) built-in shell (ash)
  _______                     ________        __
 |       |.-----.-----.-----.|  |  |  |.----.|  |_
 |   -   ||  _  |  -__|     ||  |  |  ||   _||   _|
 |_______||   __|_____|__|__||________||__|  |____|
          |__| W I R E L E S S   F R E E D O M
 -----------------------------------------------------
 CHAOS CALMER (Bleeding Edge, r45594)
 -----------------------------------------------------
  * 1 1/2 oz Gin            Shake with a glassful
  * 1/4 oz Triple Sec       of broken ice and pour
  * 3/4 oz Lime Juice       unstrained into a goblet.
  * 1 1/2 oz Orange Juice
  * 1 tsp. Grenadine Syrup
 -----------------------------------------------------
root@OpenWrt:~# ls
helloworld_1_ar71xx.ipk
root@OpenWrt:~# opkg install helloworld_1_ar71xx.ipk 
Installing helloworld (1) to root...
Configuring helloworld.
root@OpenWrt:~#

安裝完成后,執(zhí)行一下試試看。

root@OpenWrt:~# helloworld 
This is my hello word!

用which命令查看 helloworld 安裝的路徑:

root@OpenWrt:~# which helloworld
/bin/helloworld

在 /bin/ 路徑下。

感謝各位的閱讀!關(guān)于“OpenWRT如何創(chuàng)建軟件包”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

本文題目:OpenWRT如何創(chuàng)建軟件包
URL鏈接:http://muchs.cn/article48/gdeghp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機(jī)面包屑導(dǎo)航、定制網(wǎng)站網(wǎng)頁設(shè)計(jì)公司、品牌網(wǎng)站設(shè)計(jì)、品牌網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

手機(jī)網(wǎng)站建設(shè)