这次讲讲openwrt的结构.

1. 代码上来看有几个重要目录package, target, build_root, bin, dl....

---build_dir/host目录是建立工具链时的临时目录

---build_dir/toolchain-<arch>*是对应硬件的工具链的目录

---staging_dir/toolchain-<arch>* 则是工具链的安装位置

---target/Linux/<platform>目录里面是各个平台(arch)的相关代码

---target/linux/<platform>/config-3.10文件就是配置文件了

---dl目录是'download'的缩写, 在编译前期,需要从网络下载的数据包都会放在这个目录下,这些软件包的一个特点就是,会自动安装在所编译的固件中,也就是我们make

menuconfig的时候,为固件配置的一些软件包。如果我们需要更改这些源码包,只需要将更改好的源码包打包成相同的名字放在这个目录下,然后开始编译即可。编译时,会将软件包解压到build_dir目录下。

---而在build_dir/目录下进行解压,编译和打补丁等。

---package目录里面包含了我们在配置文件里设定的所有编译好的软件包。默认情况下,会有默认选择的软件包。在openwrt中ipk就是一切, 我们可以使用

$ ./scripts/feeds update来对软件包进行更新.

$ ./scripts/feeds search nmap 查找软件包'nmap'

Search results in feed ’packages’: 
 nmap       Network exploration and/or security auditing utility

$ ./scripts/feeds install nmap 安装'nmap'这个软件

$ make package/symlinks  //估计意思是更新软件源之类的

---bin目录下生成了很多bin文件,根据不同的平台来区分。另外bin/<platform>/package目录,里面有很多ipk后缀的文件,都是package目录下的源码在build_dir目录下编译后的生成的结果。

2. 新建自己的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
上传helloworld_1_bcm47xx.ipk至路由器
执行# opkg install helloworld_1_bcm47xx.ipk
然后输入hello然后按Tab键,发现openwrt中已经有helloworld可执行命令。
执行 helloworld命令来查看程序的效果。
Hell! O' world, why won't my code compile?

转自:http://blog.csdn.net/kingvenll/article/details/27545221

openwrt<转载--openwrt框架分析 >的更多相关文章

  1. openwrt: Makefile 框架分析

    openwrt: Makefile 框架分析 原文链接:blog.chinaunix.net/uid-26675482-id-4704952.html 本篇的主要目的是想通过分析Makefile,了解 ...

  2. VS2010/MFC编程入门之四(MFC应用程序框架分析)

    VS2010/MFC编程入门之四(MFC应用程序框架分析)-软件开发-鸡啄米 http://www.jizhuomi.com/software/145.html   上一讲鸡啄米讲的是VS2010应用 ...

  3. SSH框架总结(环境搭建+框架分析+实例源码下载)

    一.SSH框架简介 SSH是struts+spring+hibernate集成的web应用程序开源框架. Struts:用来控制的,核心控制器是Controller. Spring:对Struts和H ...

  4. Android/Linux下CGroup框架分析及其使用

    1 cgroup介绍 CGroup是control group的简称,它为Linux kernel提供一种任务聚集和划分的机制,可以限制.记录.隔离进程组(process groups)所使用的资源( ...

  5. 几款开源的hybird移动app框架分析

    几款开源的Hybrid移动app框架分析 Ionic Onsen UI 与 ionic 相比 jQuery Mobile Mobile Angular UI 结论 很多移动开发者喜欢使用原生代码开发, ...

  6. 深入浅出 - Android系统移植与平台开发(十一) - Sensor HAL框架分析之一

    作者:唐老师,华清远见嵌入式学院讲师. 1. Sensor的概念 Sensor即传感器,在当前智能手机上大量存在:G-Sensor.LightsSensor. ProximitySensor.Temp ...

  7. 深入浅出 - Android系统移植与平台开发(八)- HAL Stub框架分析

    作者:唐老师,华清远见嵌入式学院讲师. 1. HAL Stub框架分析 HAL stub的框架比较简单,三个结构体.两个常量.一个函数,简称321架构,它的定义在:@hardware/libhardw ...

  8. Android 核心分析 之六 IPC框架分析 Binder,Service,Service manager

    IPC框架分析 Binder,Service,Service manager 我首先从宏观的角度观察Binder,Service,Service Manager,并阐述各自的概念.从Linux的概念空 ...

  9. Yii PHP 框架分析(二)

    Yii PHP 框架分析(二)作者:wdy http://hi.baidu.com/delphiss/blog/item/54597af595085ad3f3d38552.html Yii是基于组件( ...

随机推荐

  1. No.101_第二次团队会议

    时间的敲定 在这一次的会议中,明确了任务目标,将任务进行合理分配,并且规划了整个任务的时间节点,这对团队来说非常重要. 一.最终项目 在上一节课的时候,我们最终没有拿到学霸开发项目,最后爬虫也被选走了 ...

  2. [buaa-SE-2017]个人作业-回顾

    个人作业-回顾 提问题的博客:[buaa-SE-2017]个人作业-Week1 Part1: 问题的解答和分析 1.1 问题:根据书中"除了前20的学校之外,计科和软工没有区别"所 ...

  3. 06慕课网《进击Node.js基础(一)》作用域和上下文

    作用域 function(){}大括号中的内容是一个作用域; function 和 var 的声明会被提到作用域的最上面 function f(){ a = 2; var b = g(); //此处可 ...

  4. static和final

    是静态修饰符,什么叫静态修饰符呢?大家都知道,在程序中任何变量或者代码都是在编译时由系统自动分配内存来存储的,而所谓静态就是指在编译后所分配的内存会一直存在,直到程序退出内存才会释放这个空间,也就是只 ...

  5. C#和.net框架

    第一章C#和.net框架 c#只是.net的一部分,.net不只包含C#.C#是一种程序语言,.net是一个框架/平台 C#和.NET框架 在.NET之前 20世纪90年代,微软平台多数程序员使用VB ...

  6. User survey(用户调研)

    郑文武——小学二年级学生 姓名 郑文武  性别.年龄 男.9岁                      职业 学生 收入 父母给的零花钱 知识层次和能力 会使用手机 生活/工作情况 努力学习但数学成 ...

  7. LCA(最近公共祖先)算法

    参考博客:https://blog.csdn.net/my_sunshine26/article/details/72717112 首先看一下定义,来自于百度百科 LCA(Lowest Common ...

  8. 关于Keil C51中“ERROR L107: ADDRESS SPACE OVERFLOW ”的总

    最近写一个关于单片机播放音乐的程序,出现如下错误: *** ERROR L107: ADDRESS SPACE OVERFLOW ... ... Program Size: data=167.6 xd ...

  9. 项目复审—Alpha阶段

    项目复审-Alpha阶段 小组的名字和链接 优 点 缺 点 排名 [别看了你没救队]http://www.cnblogs.com/liaoyujun233/p/9016362.html 此队优点很多, ...

  10. TP5 多入口文件配置的坑

    闲话不多说,TP5(5.0.20) 在配置多入口文件的时候你是否遇到过一下的问题呢? 开发设计的需求吧网站拆分为前台.后台.API 3 个模块,对应的也需要3个入口文件,后台和API入口文件是用PAT ...