Openwrt之helloworld

来源:互联网 发布:花生壳映射外网80端口 编辑:程序博客网 时间:2024/05/22 16:17
一、制作helloworld软件包

   在制作好的SDK环境OpenWrt-SDK-ramips-for-linux-x86_64-gcc-4.8-linaro_uClibc-0.9.33.2.tar.bz2下创建helloworld包
package/$mkdir helloworld
package/helloworld/$创建Makefile 和 src目录
Makefile  src/

src目录下编辑helloworld.c  Makefile
helloworld.c如下:
[cpp] view plain copy
  1. #include <stdio.h>  
  2.   
  3. int main(void)  
  4. {  
  5.     printf("hello,OpenWrt!\n");  
  6.   
  7.     return 0;  
  8. }  

Makefile如下:
[plain] view plain copy
  1. helloworld:helloworld.o  
  2.         $(CC) $(LDFLAGS) helloworld.o -o helloworld  
  3. hello.o:hello.c  
  4.         $(CC) $(CFLAGS) -c helloworld.c  
  5.   
  6. clean:  
  7.         rm *.o helloworld  

在linux环境进入src目录直接make 就可以生成helloworld helloworld.o了。

注:交叉工具链在SDK staging_dir/toolchain-mipsel_24kec+dsp_gcc-4.8-linaro_uClibc-0.9.33.2/bin/


移植到openwrt环境,关键是package/helloworld/Makefile文件,这个是openwrt编译软件包的规则

package/helloworld/Makefile文件:
[plain] 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.   
  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))  


Makefile语法可参考官网:http://wiki.openwrt.org/doc/devel/packages

二、编译

make V=99编译之后在bin/ramips/packages/base/目录下生成
helloworld_1_ramips_24kec.ipk

三、路由器上测试

scp win@192.168.100.111:/home/win/work/helloWorld_1_ramips_24kec.ipk .

安装:
root@OpenWrt:/# opkg install helloworld_1_ramips_24kec.ipk 
Installing helloworld (1) to root...
Configuring helloworld.

运行:
root@OpenWrt:/# helloworld 

hello,OpenWrt!

第二篇:

我们在trunk/bin/下编译出OpenWrt-SDK-ramips-for-linux-x86_64-gcc-4.8-linaro_uClibc-0.9.33.2.tar.bz2

在此,我们用到它来编译出我们的helloworld.ipk.


解压:

cd /trunk/ramips/bin

tar -xjvf OpenWrt-SDK-ramips-for-linux-x86_64-gcc-4.8-linaro_uClibc-0.9.33.2.tar.bz2

cd OpenWrt-SDK-ramips-for-linux-x86_64-gcc-4.8-linaro_uClibc-0.9.33.2

cd package

在trunk/bin/ramips/OpenWrt-SDK-ramips-for-linux-x86_64-gcc-4.8-linaro_uClibc-0.9.33.2/package这个目录下建立helloworld

mkdir  helloworld

cd helloworld

mkdir src

cd src

vim helloworld.c

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #include <stdio.h>  
  2. #include <unistd.h>  
  3.   
  4. int main(void)  
  5. {  
  6.     printf("Hello world, why won't my code compile?\n\n");  
  7.     return 0;  
  8. }  

vim Makefile  //注意TAB,不能被替换空格,Makefile的编写规则,此Makfile是编译helloworld.c
[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. helloworld:helloworld.o  
  2.     $(CC) $(LDFLAGS) helloworld.o -o helloworld  
  3. helloworld.o:helloworld.c  
  4.     $(CC) $(CFLAGS) -c helloworld.c  
  5. clean:  
  6.     rm *.o helloworld  

回到上一层目录,:trunk/bin/ramips/OpenWrt-SDK-ramips-for-linux-x86_64-gcc-4.8-linaro_uClibc-0.9.33.2/package/helloworld

cd ..

vim Makefile  //此Makefile是给SDK读取,编译src目录的

[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))  
特别提醒:格式务必使用TAB,不然会出现编译问题。特别最后一行:
[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <span style="color:#FF0000;">$(eval $(call BuildPackage,helloworld))</span>  

不需要TAB,当初以为这一行也需要TAB,结果怎么编译bin目录下都没有生成ipk。

回到SDK根目录:trunk/bin/ramips/OpenWrt-SDK-ramips-for-linux-x86_64-gcc-4.8-linaro_uClibc-0.9.33.2

执行:make V=s

最后生成目标文件都在trunk/bin/ramips/OpenWrt-SDK-ramips-for-linux-x86_64-gcc-4.8-linaro_uClibc-0.9.33.2/bin/ramips/packages:

drwxr-xr-x 2 dean dean 4096  7月  5 19:07 ./
drwxr-xr-x 3 dean dean 4096  7月  5 19:07 ../
-rw-r--r-- 1 dean dean 1796  7月  5 19:07 helloworld_1_ramips_24kec.ipk
-rw-r--r-- 1 dean dean  432  7月  5 19:07 Packages
-rw-r--r-- 1 dean dean  331  7月  5 19:07 Packages.gz

这个helloworld_1_ramips_24kec.ipk就是我们编译出来的helloworld


开发板正常运行到系统后,使用wget下载helloworld_1_ramips_24kec.ipk

安装:opkg install helloworld_1_ramips_24kec.ipk 

运行:hello  //打完hello按TAB自动补全,然后运行即可。


0 0