openwrt使用sdk编译应用程序

来源:互联网 发布:淘宝店铺体检中心 编辑:程序博客网 时间:2024/04/30 00:41
在openwrt源码目录下make menuconfig时,选择  [*] Build the OpenWrt SDK ,之后就会在bin/ar71xx/下生产SDK文件OpenWrt-SDK-ar71xx-for-linux-i686-gcc-4.6-linaro_uClibc-0.9.33.2.tar.bz2

解压之后就是OpenWrt-SDK-ar71xx-for-linux-i686-gcc-4.6-linaro_uClibc-0.9.33.2,此目录结构跟openwrt的目录结构基本一致
[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
  1. song@song-virtual-machine:attitude_adjustment# ls bin/ar71xx/OpenWrt-SDK-ar71xx-for-linux-i686-gcc-4.6-linaro_uClibc-0.9.33.2  
  2. bin        Config.in  docs                include  logs      package   Packages.gz  rules.mk  staging_dir  tmp  
  3. build_dir  dl         feeds.conf.default  LICENSE  Makefile  Packages  README.SDK   scripts   target  
[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
  1. song@song-virtual-machine:attitude_adjustment# ls  
  2. bin          build_dir  dl    feeds.conf.default  LICENSE  Makefile  README    scripts      target  toolchain  
  3. BSDmakefile  Config.in  docs  feeds           include             logs     package   rules.mk  staging_dir  tmp     tools  


概述:源码在SDK/pakage目录,生成文件在SDK/bin/ar71xx/packages目录。如果没有SDK/bin/ar71xx/packages目录也可手动创建。SDK/bin/packages目录没用到。
1.在OpenWrt-SDK-ar71xx-for-linux-i686-gcc-4.6-linaro_uClibc-0.9.33.2/package目录下新建helloworld目录
[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
  1. song@song-virtual-machine:package$ ls  
  2. helloworld  Makefile  rules.mk  

2.在helloworld目录下新建Makefile文件和src目录
[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
  1. song@song-virtual-machine:helloworld$ ls  
  2. Makefile  src  
此Makefile内容
[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
  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.   
  19. include $(TOPDIR)/rules.mk  
  20.   
  21. # Name and release number of this package  
  22. PKG_NAME:=helloworld  
  23. PKG_RELEASE:=1  
  24.   
  25.   
  26. # This specifies the directory where we're going to build the program.   
  27. # The root build directory, $(BUILD_DIR), is by default the build_mipsel  
  28. # directory in your OpenWrt SDK directory  
  29. PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME)  
  30.   
  31.   
  32. include $(INCLUDE_DIR)/package.mk  
  33.   
  34.    
  35.   
  36. # Specify package information for this program.  
  37. # The variables defined here should be self explanatory.  
  38. # If you are running Kamikaze, delete the DESCRIPTION  
  39. # variable below and uncomment the Kamikaze define  
  40. # directive for the description below  
  41. define Package/helloworld  
  42.     SECTION:=utils  
  43.     CATEGORY:=Utilities  
  44.     TITLE:=Helloworld -- prints a snarky message  
  45. endef  
  46.   
  47.   
  48. # Uncomment portion below for Kamikaze and delete DESCRIPTION variable above  
  49. define Package/helloworld/description  
  50.         If you can't figure out what this program does, you're probably  
  51.         brain-dead and need immediate medical attention.  
  52. endef  
  53.   
  54.    
  55.   
  56. # Specify what needs to be done to prepare for building the package.  
  57. # In our case, we need to copy the source files to the build directory.  
  58. # This is NOT the default.  The default uses the PKG_SOURCE_URL and the  
  59. # PKG_SOURCE which is not defined here to download the source from the web.  
  60. # In order to just build a simple program that we have just written, it is  
  61. # much easier to do it this way.  
  62. define Build/Prepare  
  63.     mkdir -p $(PKG_BUILD_DIR)  
  64.     $(CP) ./src/* $(PKG_BUILD_DIR)/  
  65. endef  
  66.   
  67.   
  68. # We do not need to define Build/Configure or Build/Compile directives  
  69. # The defaults are appropriate for compiling a simple program such as this one  
  70.   
  71.   
  72. # Specify where and how to install the program. Since we only have one file,  
  73. # the helloworld executable, install it by copying it to the /bin directory on  
  74. # the router. The $(1) variable represents the root directory on the router running  
  75. # OpenWrt. The $(INSTALL_DIR) variable contains a command to prepare the install  
  76. # directory if it does not already exist.  Likewise $(INSTALL_BIN) contains the  
  77. # command to copy the binary file from its current location (in our case the build  
  78. # directory) to the install directory.  
  79. define Package/helloworld/install  
  80.     $(INSTALL_DIR) $(1)/bin  
  81.     $(INSTALL_BIN) $(PKG_BUILD_DIR)/helloworld $(1)/bin/  
  82. endef  
  83.   
  84.   
  85. # This line executes the necessary commands to compile our program.  
  86. # The above define directives specify all the information needed, but this  
  87. # line calls BuildPackage which in turn actually uses this information to  
  88. # build a package.  
  89. $(eval $(call BuildPackage,helloworld))  

3.在src目录下新建helloworld.c文件和Makefile文件,分别如下
[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
  1. #include<stdio.h>  
  2. int main(void)  
  3. {  
  4.     printf("yyyy\n");  
  5.     return 0;  
  6. }  

[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
  1. # build helloworld executable when user executes "make"  
  2.   
  3. helloworld: helloworld.o  
  4.     $(CC) $(LDFLAGS) helloworld.o -o helloworld  
  5.   
  6. helloworld.o: helloworld.c  
  7.     $(CC) $(CFLAGS) -c helloworld.c  
  8.   
  9. # remove object files and executable when user executes "make clean"  
  10. clean:  
  11.     rm *.o helloworld  

4.在OpenWrt-SDK-ar71xx-for-linux-i686-gcc-4.6-linaro_uClibc-0.9.33.2目录下执行编译
[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
  1. song@song-virtual-machine:OpenWrt-SDK-ar71xx-for-linux-i686-gcc-4.6-linaro_uClibc-0.9.33.2$ make V=s  

5.成功之后会在OpenWrt-SDK-ar71xx-for-linux-i686-gcc-4.6-linaro_uClibc-0.9.33.2/bin/ar71xx/packages/ 下生产helloworld_1_ar71xx.ipk
[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
  1. song@song-virtual-machine:OpenWrt-SDK-ar71xx-for-linux-i686-gcc-4.6-linaro_uClibc-0.9.33.2$ ls bin/ar71xx/packages/  
  2. helloworld_1_ar71xx.ipk  Packages  Packages.gz  

6.拷贝ipk到板子执行
[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
  1. opkg install helloworld_1_ar71xx.ipk  
会生成/bin/helloworld

通过opkg list可查看
0 0