之前写过一篇日志,是关于如何搭建自己的OpenWRT开发环境。经过最近一段时间的开发学习和实践,对OpenWRT环境的开发有了一定的了解。在这里将我的开发心得做个整理。

1、搭建开发环境

首先,我们需要一个为路由器定制的开发环境,具体可以参考我的另一篇日志:《搭建自己的OpenWrt开发环境》。这里只做一个简单的补充,在执行make menuconfig后,会出现下图:

其中,图中红框部分是我定制路由器的系统版本,大家可以根据不同的路由器进行不同的选择;绿框部分表示我们需要编译一个SDK开发环境(默认情况下,此项未勾选)。

编译过程中需要通过官网下载很多相关的软件包,所以必须保证能够顺利连上外网。由于下载速度的限制,编译过程大概需要数小时。编译结束后,所有的产品都会放在编译根目录下的bin/yourtarget/. 例如:我所编译的产物都放在./bin/brcm47xx/下,其中文件主要有几类:

(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环境包为:/bin/brcm47xx/OpenWrt-SDK-brcm47xx-for-Linux-x86_64-gcc-4.3.3+cs_uClibc-0.9.30.1.tar.bz2

可以从名称上看出,target system是brcm47xx,host system是Linux-x86_64,使用的编译工具以及库是4.3.3+cs_uClibc-0.9.30.1。

(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的软件包。

(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上。

(2)将OpenWrt-SDK-brcm47xx-for-Linux-x86_64-gcc-4.3.3+cs_uClibc-0.9.30.1.tar.bz2解压

tar –xvf OpenWrt-SDK-brcm47xx-for-Linux-x86_64-gcc-4.3.3+cs_uClibc-0.9.30.1.tar.bz2

(3)进入SDK

cd OpenWrt-SDK-brcm47xx-for-Linux-x86_64-gcc-4.3.3+cs_uClibc-0.9.30.1

可以看到里面的目录结构跟我们之前source的目录结构基本相同,所需要编译的软件包,需要放置在package目录下

(4)在package目录下创建helloworld目录

cd package

mkdir helloworld

cd helloworld

(5)创建src目录,拷贝 helloworld文件

mkdir src

cp /home/wrt/test/helloworld.c src

cp /home/wrt/test/Makefile src

(6)在helloworld目录下创建Makefile文件

这个Makefile文件是给OpenWRT读的,而之前写的那个Makefile文件是针对helloworld给编译其读的。两个Makefile不在同一层目录下。

touch Makefile

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

(7)返回到SDK的根目录

执行make进行编译

编译过程会在build_dir目录下完成

编译结果会放在 bin/[yourtarget]/package目录下helloworld_1_bcm47xx.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?

【End】

希望对大家能有帮助 :)

-----------------------------------------------------------

openwrt开发的更多相关文章

  1. 我的openwrt开发相关文章

    openwrt学习笔记: 在openwrt的学习过程中,走了非常多的弯路.一直以来有个期盼.希望能够出个简易教程,希望openwrt的同仁们能够更加高速的入手. . openwrt学习笔记(三十二): ...

  2. 【转】OpenWRT开发自定义应用方法

    [转]OpenWRT开发自定义应用方法 转自:http://blog.csdn.net/rudyn/article/details/38616783 OpenWRT编译成功完成后,所有的产品都会放在编 ...

  3. 搭建自己的OpenWrt开发环境

    1.  安装环境Linux系统,如果在CentOS上操作,需安装如下依赖包:yum install binutils bzip2 gawk gcc gcc-c++ gettext makencurse ...

  4. 搭建OpenWrt开发环境(包括编译过程)

    OpenWrt是一个高度模块化.高度自动化的嵌入式linux发行版,其编译和安装过程比普通的linux发行版而言,要简单太多了.如果您是新手,您那恐惧的心大可放到肚子里,呵呵.对于新手来说最麻烦的恐怕 ...

  5. openwrt教程 第一章 物联网&amp;openwrt开发概述

    1.1 我们的宗旨 互联网.移动互联网的时代已经过去,物联网的时代已经来临!2014年,是物联网元年,2016年,物联网将达到高潮!为了迎接该潮流,我们工作室(F403科技创意室:http://f40 ...

  6. OpenWRT开发之——对C++的支持(解决库依赖问题)【转】

    转自:https://my.oschina.net/hevakelcj/blog/411944 摘要: 本文尝试用C++来开发一个cpp-demo包 遇到打包库依赖的问题,分析打包过程并解决了这个问题 ...

  7. openwrt开发笔记一:源码下载与编译

    1.1 环境要求 编译系统:Linux发行版(本文使用Ubuntu) 编译一个可以安装的OpenWrt固件镜像文件(大约8MB大小的),你需要: 一个纯净的OpenWrt编译系统大约需要200MB的空 ...

  8. 【OpenWRT】【RT5350】【一】OpenWrt开发环境搭建

    [宿主机构建] 本人电脑配置如下: CPU: intel 酷睿 i3 3.3G 双核 内存: 金士顿 8G 硬盘:希捷 1TB 7200rpm 操作系统:win7  旗舰版 64位 OpenWrt是在 ...

  9. 【玩转开源】BananaPi R2 —— 第三篇 基于Openwrt开发一个简单的路由器

    上一篇讲解了R2的网口配置,这一篇我们以BananaPi R2为例子来实现一个简单的路由器:那么一个简单的路由器应该具备什么样的功能呢?最简单的说就是wan+lan+ap这三个功能. 首先wan+la ...

随机推荐

  1. article元素设计网络新闻展示

    article元素用来表示文档.页面中独立的.完整的.可以独自被外部引用的内容.它可以是一篇文章博客或者报刊中的文章.一篇论坛帖子.一段用户评论或独立的插件等.除了内容部分,一个article元素通常 ...

  2. someExperience

    // 面试题1 var name = 'World'; (function () { if (typeof name==='undefined') { var name = 'jack'; conso ...

  3. 三栏布局的n种实现

    本文主要讨论左右边栏固定宽度,中间栏填满其余空间的布局.至于其他类型,基本上也就是半斤和八两.每一种布局都会有个Demo,个人依然认为文章里帖代码并没有Demo来的直接.所以正文负责解释,源码参见De ...

  4. 两种js监听滚轮事件的方式

    前段时间在写前端的时候,需要监听浏览器的滚轮事件 网上查了一下,找到两种监听滚轮事件的方法: 一.原生js通过window.onscroll监听 //window.onscroll = functio ...

  5. [.Net MVC] 使用 log4net 日志框架

    项目:后台管理平台 意义:项目开发中提出增加日志功能,对关键的操作.程序运行中的错误信息进行记录,这对程序部署后的调试有很大意义. 注:本文只是对网上搜集的信息进行了整合,以备今后查询. 关键字:.N ...

  6. 用PL0语言求Fibonacci数列前m个中偶数位的数

    程序说明:求Fibonacci数列前m个中偶数位的数: 这是编译原理作业,本打算写 求Fibonacci数列前m个数:写了半天,不会写,就放弃了: 程序代码如下: var n1,n2,m,i; pro ...

  7. const char*、char*、char* const、char[]、string的区别

    1.const char* p: p is a pointer to const char(char const* p 一样)   意思就是不能通过p指针来修改p指向的内容(但是内容可以修改). 2. ...

  8. ssh连接失败解决方法

    执行如下命令: ssh-keygen -t dsa -P '' -f /etc/ssh/ssh_host_dsa_key ssh-keygen -t rsa -P '' -f /etc/ssh/ssh ...

  9. VPN client on linux debian

    Install the pptp-linux and pptp-linux-client: sudo apt-get install pptp-linux pptp-linux-client Crea ...

  10. Linux内核Radix Tree(三):API介绍

    1.     单值查找radix_tree_lookup 函数radix_tree_lookup执行查找操作,查找方法是:从叶子到树顶,通过数组索引键值值查看数组元素的方法,一层层地查找slot.其列 ...