这次讲讲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

转载于:https://www.cnblogs.com/runningdonkey/p/7058136.html

openwrt转载--openwrt框架分析相关推荐

  1. Linux USB驱动框架分析 【转】

    转自:http://blog.chinaunix.net/uid-11848011-id-96188.html 初次接触与OS相关的设备驱动编写,感觉还挺有意思的,为了不至于忘掉看过的东西,笔记跟总结 ...

  2. Prototype 框架分析(一)

    Prototype 框架分析(一) Class 关于javascript的面向对象设计可以参看MSDN上的文章<JavaScript使用面向对象的技术创建高级 Web 应用程序>,这样理解 ...

  3. Android系统--Binder系统具体框架分析(一)补充

    Android系统--Binder系统具体框架分析(一)补充 补充:对Binder驱动分析一的代码补充,添加saygoobye和saygoodbye_to服务 test_server.h #ifnde ...

  4. jquery框架分析-构造jquery对象初步

    这是一些分析jquery框架的文字 针对jquery 1.3.2版本 面向的读者应具备以下要求 1.非常熟悉HTML 2.非常熟悉javascript语法知识 3.熟悉javascript面向对象方面 ...

  5. Linux PCI驱动框架分析:(Peripheral Component Interconnect,外部设备互联)

    <DPDK 20.05 | rte_pci_bus思维导图 | 第一版> <linux系统下:IO端口,内存,PCI总线 的 读写(I/O)操作> <Linux指令:ls ...

  6. (七) UVC框架分析

    title: UVC框架分析 date: 2019/4/23 19:50:00 toc: true --- UVC框架分析 源码的位置在drivers\media\video\uvc,查看下Makef ...

  7. linux服务器安装openwrt,探索openwrt安装宝塔,搭建web网站论坛社区网校

    本帖最后由 离人难拥 于 2021-3-14 12:35 编辑 本人小白新手,linux命令也不熟悉,学习,有老师的话请指教 先认识一下openwrt OpenWrt 可以被描述为一个嵌入式的 Lin ...

  8. skydive前端代码框架分析

    skydive前端代码: https://github.com/skydive-project/skydive/tree/master/statics 前端代码使用框架分析: 以vue-2.1.9框架 ...

  9. OS相关驱动 Linux USB驱动框架分析

    初次接触与OS相关的设备驱动编写,感觉还挺有意思的,为了不至于忘掉看过的东西,笔记跟总结当然不可缺,更何况我决定为嵌入式卖命了.好,言归正传,我说一说这段时间的收获,跟大家分享一下Linux的驱动开发 ...

最新文章

  1. 深入理解printf 之一 问题引出
  2. 浅入深出Vue:发布项目
  3. HDLBits答案(23)_找BUG
  4. HW--漂亮度2(测试通过)
  5. mysql aes encrypt_mysql加密函数aes_encrypt()和aes_decrypt()使用教程
  6. Oracle Hint
  7. python编程(paddle ocr)
  8. 前端综合性文档和教程总结(持续更新)
  9. 搭建Cocos2d-js开发环境(Window)
  10. 潮流计算的matlab程序实现方法
  11. 【AI视野·今日NLP 自然语言处理论文速览 第三十三期】Thu, 21 Apr 2022
  12. Excel 2010 VBA 入门 043 按自定义序列进行排序
  13. unity android录制视频教程,Unity移动端视频录制,Android和IOS都支持
  14. 1050Ti解决csgo打不开、电脑无缘无故蓝屏的终极方法
  15. 户外直播、慢直播、赛事直播等直播行业的未来发展趋势
  16. 经济基础知识(中级)【2】
  17. 毕业论文知网查重之应对办法
  18. BZOJ3838 : [Pa2013]Raper
  19. [生存志] 第114节 韩信围垓下
  20. 大青云不显示服务器,37大青云4月25日关服停止运营公告

热门文章

  1. javascript感叹号1_「翻译」JavaScript的可视化学习之三:作用域(链)
  2. graphpad 折线图横坐标比例_GraphPad Prism绘制剂量反应曲线
  3. php smarty 时间,smarty的时间格式-详细 | 时刻需
  4. OpenKruise 2021 规划曝光:More than workloads
  5. netty tcp服务端主动断开客户端_「Netty核心技术」6-ChannelPipeline源码
  6. picf509c语言程序,樊媛媛c语言程序设计09编译预处理.pptx
  7. c语言是以文件为单位编译,c语言从头开始(三:编译器工作原理) (我们还可以自己编写头文件后缀是xx.h并把它当前代码文件所在目录我们要用就可以直接通过下面代码使用)...
  8. oracle 安全桌面,Oracle Secure Global Desktop组件远程漏洞(CVE-2012-1685)
  9. tcp option 结构体_基于 Kotlin 实现一个简单的 TCP 自定义协议
  10. linux用不用装固态硬盘驱动,固态硬盘要不要安装驱动?总算弄明白了