这次讲讲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文件模板内容如下:

[cpp] view plain copy
  1. ##############################################
  2. # OpenWrt Makefile for HelloWorld program
  3. #
  4. #
  5. # Most of the variables used here are defined in
  6. # the include directives below. We just need to
  7. # specify a basic description of the package,
  8. # where to build our program, where to find
  9. # the source files, and where to install the
  10. # compiled program on the router.
  11. #
  12. # Be very careful of spacing in this file.
  13. # Indents should be tabs, not spaces, and
  14. # there should be no trailing whitespace in
  15. # lines that are not commented.
  16. #
  17. ##############################################
  18. include $(TOPDIR)/rules.mk
  19. # Name and release number of this package
  20. PKG_NAME:=HelloWorld
  21. PKG_RELEASE:=1
  22. # This specifies the directory where we're going to build the program.
  23. # The root build directory, $(BUILD_DIR), is by default the build_mipsel
  24. # directory in your OpenWrt SDK directory
  25. PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME)
  26. include $(INCLUDE_DIR)/package.mk
  27. # Specify package information for this program.
  28. # The variables defined here should be self explanatory.
  29. # If you are running Kamikaze, delete the DESCRIPTION
  30. # variable below and uncomment the Kamikaze define
  31. # directive for the description below
  32. define Package/HelloWorld
  33. SECTION:=utils
  34. CATEGORY:=Utilities
  35. TITLE:=HelloWorld -- prints a snarky message
  36. endef
  37. # Uncomment portion below for Kamikaze and delete DESCRIPTION variable above
  38. define Package/HelloWorld/description
  39. If you can't figure out what this program does, you're probably brain-dead and need immediate medical attention.
  40. endef
  41. # Specify what needs to be done to prepare for building the package.
  42. # In our case, we need to copy the source files to the build directory.
  43. # This is NOT the default.  The default uses the PKG_SOURCE_URL and the
  44. # PKG_SOURCE which is not defined here to download the source from the web.
  45. # In order to just build a simple program that we have just written, it is
  46. # much easier to do it this way.
  47. define Build/Prepare
  48. mkdir -p $(PKG_BUILD_DIR)
  49. $(CP) ./src/* $(PKG_BUILD_DIR)/
  50. endef
  51. # We do not need to define Build/Configure or Build/Compile directives
  52. # The defaults are appropriate for compiling a simple program such as this one
  53. # Specify where and how to install the program. Since we only have one file,
  54. # the HelloWorld executable, install it by copying it to the /bin directory on
  55. # the router. The $(1) variable represents the root directory on the router running
  56. # OpenWrt. The $(INSTALL_DIR) variable contains a command to prepare the install
  57. # directory if it does not already exist.  Likewise $(INSTALL_BIN) contains the
  58. # command to copy the binary file from its current location (in our case the build
  59. # directory) to the install directory.
  60. define Package/HelloWorld/install
  61. $(INSTALL_DIR) $(1)/bin
  62. $(INSTALL_BIN) $(PKG_BUILD_DIR)/HelloWorld $(1)/bin/
  63. endef
  64. # This line executes the necessary commands to compile our program.
  65. # The above define directives specify all the information needed, but this
  66. # line calls BuildPackage which in turn actually uses this information to
  67. # build a package.
  68. $(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?

总结:本人也是新手, 所以很多都不懂, 望高人指点.

openwrt编译helloworld相关推荐

  1. 最新版OpenWrt编译教程,解决依赖问题

    最新版OpenWrt编译教程,解决依赖问题 参考文章: (1)最新版OpenWrt编译教程,解决依赖问题 (2)https://www.cnblogs.com/jzssuanfa/p/7400840. ...

  2. 【物联网】OpenWrt编译和修改基础--预科

    该文章讲解一下在AR9331上使用Openwrt的编译修改方法,前面先介绍一下硬件平台特点,为后面的代码修改做铺垫,然后描述一下Openwrt的编译烧写流程,最后再重点讲述编译配置.代码修改细节. 1 ...

  3. OpenWrt 编译及batman-adv组件选择(for Netgear WNDR3800)

    OpenWrt 编译(ubuntu)及batman-adv配置(for Netgear WNDR3800) 一.编译openwrt固件 1.下载源码,如15.05(Chaos Calmer)这个版本 ...

  4. OpenWRT编译 -- 出现‘...net/mac80211/mac80211.ko‘ is missing的错误

    1.背景   1.在 ubuntu 环境下搭建好 OpenWRT 的开发环境使用良久之后(具体的搭建可以查阅 OpenWRT 编译 – 搭建属于自己的 OpenWRT 的开发环境),心血来潮想要换一下 ...

  5. openwrt编译固件流程

    openwrt编译固件分为环境配置以及固件编译两个部分,下面将按步骤介绍编译固件的方法流程 1.环境配置 编译固件需要先配置环境,自己所需的环境在官网中可以找到.选择适合自己的分支,默认的分支为mas ...

  6. OpenWRT编译失败问题解决(一)

    最近在做OpenWRT相关的项目,按照步骤在编译时报错,提示增加编译选项 -j1 V=s 查看详细报错信息. make -j1 V=s 之后看到打印信息,gcc-linaro-4.8-2014.04. ...

  7. openwrt编译kcptun报错proxy.golang.org 无法下载

    openwrt编译kcptun报错proxy.golang.org 无法下载. 错误提示: Get https://proxy.golang.org/github.com/kardianos/gove ...

  8. 【速记】openwrt - 编译、刷固件(资料整理)

    固件 官方 https://downloads.openwrt.org/releases/19.07.4/targets/x86/64/ 整合 https://op.dllkids.xyz/op/fi ...

  9. Openwrt编译:root.squashfs-64k is too big (Max=6488064 , Current=xxxx)

    搞了一段时间openwrt编译,由于之前没有路由器的经验,很蛋疼,各种问题. Question 折腾好几天,终于把环境配好了,包括软件源.依赖库.版本,以及make menuconfig选择打到固件里 ...

  10. openwrt编译环境搭建

    openwrt编译环境搭建 1.虚拟机安装 请参考网络上的资料进行安装. 2.ubuntu安装 请参考网络上的资料进行安装. 3.ubuntu下安装相关的编译环境(若是编译环境没有准备好,在后来的操作 ...

最新文章

  1. js/jquery中实现图片轮播
  2. android 连接 asp.net webservice 简单记录
  3. 国外开源的PACS服务器
  4. Java中Controller层和Service层具体怎么区分
  5. 通讯录小程序android,通讯录小程序,找回青春的回忆
  6. IDEA使用@Data注解,类调用get、set方法标红的解决办法
  7. django 轮播图上传_拼多多规则更新:关于【商品轮播图】你所不知道的秘密!...
  8. android自定义手势解锁View
  9. ruby array 额
  10. vofuria的开发(4)更换目标图片(target)
  11. 安卓手机管理_安卓手机用户大福利,谷歌将改善手机内存管理,运行更流畅
  12. 电信无线路由器服务器网站,电信拨号上网连无线路由器的方法
  13. 如何使用dosbox运行程序——步骤详解
  14. ​「5G消息」的最新消息
  15. 第6章 访问权限控制
  16. 央行征信与互联网征信技术接口区别(征信架构篇)
  17. IText构造PDF文件
  18. python拼图_利用python制作拼图小游戏的全过程
  19. [armv9]-PAC:Pointer authentication和BTI:Branch target instructions介绍
  20. 如何改变图片的尺寸大小得到一张缩小后的图片

热门文章

  1. RK3399pro 使用TNN日记 2(Linux系统)
  2. 告诉你怎样学Java才是硬道理(转自chinaitlab)
  3. ubuntu下怎样安装星际译王stardict和下载本地词典
  4. 如何修改apk文件,改之理简单使用教程
  5. 电脑计算机未输出任何信号 键盘没亮,戴尔计算机不显示信号,为什么计算机屏幕不显示...
  6. 5款cpu温度检测工具,让你时刻关注mac的工作情况!
  7. 将Imagenet2012比赛数据解析为图像
  8. 教你三秒钟将电脑速度提高三倍
  9. Android常用的 adb shell命令
  10. 基于springboot的医院管理系统