温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

OpenWRT如何创建软件包

发布时间:2021-12-18 11:30:32 来源:亿速云 阅读:312 作者:小新 栏目:互联网科技

这篇文章给大家分享的是有关OpenWRT如何创建软件包的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

第一步:生成SDK

make menuconfig 选上 “Build the OpenWRT SDK”

在 trunk目录下,执行:

$ make menuconfig

选择对应的"Target System"与"Target Profile",并选上"Build the OpenWrt SDK"。

OpenWRT如何创建软件包

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

$ make V=99

make 完成之后,在 bin/ar71xx/ 目录下会生成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 复制到其它路径下(指可以不在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 目录。

为了方便,我将这个长长的目录名简化为:OpenWrt-SDK。修改后,完整的路径是:~/Workspace/OpenWRT/OpenWrt-SDK

据说这个目录结构跟 OpenWrt的源码目录结构差不多。

第三步:创建helloworld项目

其实,这里可以是任意我们想要加入的程序,库等。这里就以helloword为例。

在任意路径下,创建helloword项目。比如这里还是在 ~/Workspace/OpeWRT 目录下。

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

在 ~/Workspace/OpenWRT/ 目录下创建了 helloword 目录,并生成 helloword.c与Makefile文件。

如下为 helloworld.c的内容:

#include <stdio.h>

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

Makefile的内容:

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

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

clean :
    rm *.o helloworld

首先,确保在程序没问题,在本地能正常编过。为了检验一下,可以就地 make 一下,看程序本身有没有问题。

这个程序都如些之简单了,本人自己了make了一下,OK,再run了一下,正常。

第四步:创建helloworld包

进入 OpenWrt/Packages/ 并在该目录下创建 helloworld 目录,并进入该目录。

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

将我们第三步写的程序复制到这个目录下来,更名为src。再新建一个 Makefile 文件。

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

整个过程下来,package目录结构如下:

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

package/Makefile 不要去修改它。

我们要编写的是 package/helloworld/Makefile 这个文件。

在这个文件中,我们要描述 helloworld 包的信息,比如:如何配置、如何编译、如何打包、安装等等信息。
这个文件与一般的 Makefile 格式还不一样,详见OpenWrt上的说明文档:OpenWrt官网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 目录下,执行:make V=s,结果有以下错误提示:

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

说是我写的这个Makefile有错,请查看 dump.txt 文件。无奈只好去看看到底错在哪里啰。

打开 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行条件语法错误。赶紧打开 package/helloworld/Makefile,定位到32行看看。结果是:

$(eval $(call BuildPackage, helloworld))

这个,我何错之有呢?

最后反复排查,原来是 "BuildPackage,">。不会吧!多个空格少个空格都会导致语法错误?!

好了,改正过来了。

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

现在 make V=s 不再是刚才那个错了。

make[3]: Entering directory `/home/hevake_lcj/Workspace/OpenWRT/my-packages/OpenWrt-SDK/package/helloworld'
CFLAGS="-Os -pipe -mno-branch-likely -mips32r2 <此处省略好长串...>" 
CXXFLAGS="-Os -pipe -mno-branch-likely -mips32r2 <此处省略好长串...>" 
LDFLAGS="<此处省略好长串...>" 
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.  # 错误:没有找 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 文件?

我们要好好排查一个 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

好像这个宏压根没有被执行到。

为什么呢?

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

<接着昨晚的问题>

最后与例子仔细比对,发现原来我将 "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))   #已去除逗号后面的空格

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

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

第五步:试验helloworld

将刚生成的 helloworld_1_ar71xx.ipk 文件用 scp 传到目标路由上。本人的路由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:~#

安装完成后,执行一下试试看。

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

用which命令查看 helloworld 安装的路径:

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

在 /bin/ 路径下。

感谢各位的阅读!关于“OpenWRT如何创建软件包”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI