opwenWrt移植Helloworld测试

本文介绍如何使用OpenWrt SDK开发环境定制编译OpenWrt固件,并通过实例演示如何创建并安装一个简单的helloworld软件包。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文出自网络的总结,以及自己的实践,原文的文字和截图均来自自己的实践操作,欢迎转载,请注明出处http://blog.youkuaiyun.com/super_acuity/article/details/19618347

在上一节我讲到在bin目录下会生成OpenWrt-SDK-ramips-for-linux-i686-gcc-4.6-linaro_uClibc-0.9.33.2.tar.bz2,就是我们需要的包

解压OpenWrt-SDK-ramips-for-linux-i686-gcc-4.6-linaro_uClibc-0.9.33.2.tar.bz2

super@Ubuntu:~/openwrt/trunk/bin/ramips$ tar zxvf OpenWrt-SDK-ramips-for-linux-i686-gcc-4.6-linaro_uClibc-0.9.33.2.tar.bz2

super@Ubuntu:~/openwrt/OpenWrt-SDK-ramips-for-linux-i686-gcc-4.6-linaro_uClibc-0.9.33.2$ls


++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

说明:

(1).bin/.trx 文件: 这些都是在我们所选的target-system的类别之下,针对不同路由器型号、版本编译的路由器固件。这些不同路由器的型号和版本是openwrt预先设置好的,我们不需要更改。至于.bin和.trx的区别,一种说法是,第一次刷路由器的时候,需要用.bin文件,如果需要再升级,则不能再使用.bin文件,而需要用.trx文件。原因是,.bin是将路由器的相关配置信息和.trx封装在一起而生成的封包,也就是说是包含路由器版本信息的.trx。在第一次刷固件的时候,我们需要提供这样的信息,而在后续升级时,则不再需要,用.trx文件即可。

(2)packages文件夹: 里面包含了我们在配置文件里设定的所有编译好的软件包。默认情况下,会有默认选择的软件包。

(3)OpenWrt-SDK.**.tar.bz2:  这个也就是我们定制编译好的OpenWRT SDK环境。我们将用这个来进行OpenWrt软件包的开发。例如,我所编译好的SDK环境包为:OpenWrt-SDK-ramips-for-linux-i686-gcc-4.6-linaro_uClibc-0.9.33.2.tar.bz2
可以从名称上看出,target system是ramips,host system是linux-i686,使用的编译工具以及库是gcc-4.6-linaro_uClibc-0.9.33.2

(4)md5sums 文件: 这个文件记录了所有我们编译好的文件的MD5值,来保证文件的完整性。因为文件的不完整,很容易将路由器变成“砖头”。

需要主要的是,编译完成后,一定要将编译好的bin目录进行备份(如果里面东西对你很重要的话),因为在下次编译之前,执行make clean 会将bin目录下的所有文件给清除掉!!

2、 更改原有packages

在编译根目录下会有一个dl的目录,这个目录其实是“download”的简写,在编译前期,需要从网络下载的数据包都会放在这个目录下,这些软件包的一个特点就是,会自动安装在所编译的固件中,也就是我们make menuconfig的时候,为固件配置的一些软件包。如果我们需要更改这些源码包,只需要将更改好的源码包打包成相同的名字放在这个目录下,然后开始编译即可。编译时,会将软件包解压到build_dir目录下
当然,你也可以自己在dl里面创建自己的软件包,然后更改相关的配置文件,让openwrt可以识别这个文件包。

由于我的项目更改的内容是底层的,需要跟固件一起安装。所以,我使用的方法就是直接更改dl目录下软件包,然后重新进行固件编译。感觉类似于Linux的内核编译。反复编过十多次,没有任何问题。

3、 新建自己的packages
对于自己新建的package,而这个package又不需要随固件一起安装,换句话说,就是可以当做一个可选软件包的话。我们可以利用我们的SDK环境来单独编译,编译后会生成一个ipk的文件包。然后利用 opkg install xxx.ipk 来安装这个软件。

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

实际操作:下面具体说下,如何编译一个helloword的软件包。

将上面的OpenWrt-SDK-ramips-for-linux-i686-gcc-4.6-linaro_uClibc-0.9.33.2.tar.bz2解压之后如下操作

1:   进入package

2:super@Ubuntu:~/openwrt/OpenWrt-SDK-ramips-for-linux-i686-gcc-4.6-linaro_uClibc-0.9.33.2/package$mkdir helloworld

3 :super@Ubuntu:~/openwrt/OpenWrt-SDK-ramips-for-linux-i686-gcc-4.6-linaro_uClibc-0.9.33.2/package$touch Makefile && mkdir src

编写Makefile

Makefile文件模板内容如下:
##############################################
# OpenWrt Makefile for helloworld program
#
#
# Most of the variables used here are defined in
# the include directives below. We just need to
# specify a basic description of the package,
# where to build our program, where to find
# the source files, and where to install the
# compiled program on the router.
#
# Be very careful of spacing in this file.
# Indents should be tabs, not spaces, and
# there should be no trailing whitespace in
# lines that are not commented.
#
##############################################

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))


4:super@Ubuntu:~/openwrt/OpenWrt-SDK-ramips-for-linux-i686-gcc-4.6-linaro_uClibc-0.9.33.2/package$cd src

5:super@Ubuntu:~/openwrt/OpenWrt-SDK-ramips-for-linux-i686-gcc-4.6-linaro_uClibc-0.9.33.2/package$touch hellowrold.c && Makefile

(1:)编写helloworld程序
编写helloworld.c
/****************
* Helloworld.c
* The most simplistic C program ever written.
* An epileptic monkey on crack could write this code.
*****************/

#include <stdio.h>
#include <unistd.h>
int main(void)
{
     printf("Hell! O' world, why won't my code compile?\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

                                  
在这两个文件的目录下,执行make 应该可以生成helloworld的可执行文件。执行helloworld后,能够打印出“Hell! O' world, why won't my code compile?”。 这一步,主要保证我们的源程序是可以正常编译的。下面我们将其移植到OpenWRT上。

(7)返回到SDK的根目录
执行make进行编译(make V=99可以查看详细编译信息)
编译过程会在build_dir目录下完成
编译结果会放在 bin/[yourtarget]/package目录下helloworld_1_ramips_24kec.ipk


(8)上传helloworld_1_bcm47xx.ipk
使用sftp软件上传helloworld_1_bcm47xx.ipk至路由器
执行 opkg install helloworld_1_bcm47xx.ipk
输入hello然后按Tab键,发现openwrt中已经有helloworld可执行命令。
执行 helloworld 查看程序的效果。

Hell! O' world, why won't my code compile?

注:关于上传部分说明,有些文章并不是很全,自己补充一下。

首先在浏览器输入你已经烧录的openwrt的路由器IP 地址;进入系统----->管理权,添加root密码,开启ssh服务(这里我把端口改成了22)


下载一个可以进行scp的软件,或者用linux自带的一安装ssh服务的linux系统,输入如下信息:

将helloworld_1_ramips_24kec.ipk 拷贝到路由器的tmp目录下

或者用WinSCP软件进行远程拷贝:

最后用serial进行连接,这里也有两种方式进行连接,一种是串口这个就不说了,一种就是基于上面的ssh进行登录

到此就编写好了一个helloworld测试,下一节将讲解package/helloworld/目录下的Makefile需要遵循OpenWrt的约定,然后将boa(webserver)整合到openwrt的package最后移植到路由器上,进行相关的cgi测试。本文出自网络的总结,以及自己的实践,原文的文字和截图均来自自己的实践操作,欢迎转载,请注明出处http://blog.youkuaiyun.com/super_acuity/article/details/19618347


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值