openwrt使用sdk编译应用程序

来源:互联网 发布:淘宝怎么用erp转订单 编辑:程序博客网 时间:2024/05/17 06:21

openwrt 版本:15.05(CC)

在openwrt源码目录下make menuconfig时,选择  [*] Build the OpenWrt SDK ,之后就会在bin/ar71xx/下生产SDK文件OpenWrt-SDK-ar71xx-generic_gcc-4.8-linaro_uClibc-0.9.33.2.Linux-i686.tar.bz2


解压之后就是OpenWrt-SDK-ar71xx-generic_gcc-4.8-linaro_uClibc-0.9.33.2.Linux-i686,此目录结构跟openwrt的目录结构基本一致

 



概述:源码在SDK/pakage目录,生成文件在SDK/bin/ar71xx/packages目录。如果没有SDK/bin/ar71xx/packages目录也可手动创建。SDK/bin/packages目录没用到。
1.在OpenWrt-SDK-ar71xx-generic_gcc-4.8-linaro_uClibc-0.9.33.2.Linux-i686/package目录下新建my_ipk目录
[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. mint@mint ~/openwrt/bin/ar71xx/OpenWrt-SDK-ar71xx-generic_gcc-4.8-linaro_uClibc-0.9.33.2.Linux-i686 $ ls package/
    Makefile  my_ipk
  2.   

2.在my_ipk目录下新建Makefile文件和src目录
[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. mint@mint ~/openwrt/bin/ar71xx/OpenWrt-SDK-ar71xx-generic_gcc-4.8-linaro_uClibc-0.9.33.2.Linux-i686/package/my_ipk $ ls
    Makefile  src
  2.   
此Makefile内容
##############################################
# OpenWrtMakefile for helloworld program
#
#
# Most ofthe variables used here are defined in
# theinclude directives below. We just need to
# specifya basic description of the package,
# whereto build our program, where to find
# thesource files, and where to install the
#compiled program on the router.
#
# Be verycareful of spacing in this file.
# Indentsshould be tabs, not spaces, and
# thereshould be no trailing whitespace in
# linesthat are not commented.
#
##############################################
include $(TOPDIR)/rules.mk
# Nameand release number of this package
PKG_NAME:=my_ipk
PKG_RELEASE:=1
 
 
# Thisspecifies the directory where we're going to build the program.
# Theroot 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
 
 
 
# Specifypackage information for this program.
# Thevariables defined here should be self explanatory.
# If youare running Kamikaze, delete the DESCRIPTION
#variable below and uncomment the Kamikaze define
# directivefor the description below
define Package/my_ipk
SECTION:=utils
CATEGORY:=Utilities
TITLE:=Helloworld-- prints a snarky message
endef
 
 
 
# Specifywhat needs to be done to prepare for building the package.
# In ourcase, we need to copy the source files to the build directory.
# This isNOT the default.  The default uses thePKG_SOURCE_URL and the
#PKG_SOURCE which is not defined here to download the source from the web.
# Inorder to just build a simple program that we have just written, it is
# mucheasier to do it this way.
define Build/Prepare
mkdir -p $(PKG_BUILD_DIR)
$(CP) ./src/* $(PKG_BUILD_DIR)/
endef
 
 
# We donot need to define Build/Configure or Build/Compile directives
# Thedefaults are appropriate for compiling a simple program such as this one
 
 
# Specifywhere and how to install the program. Since we only have one file,
# thehelloworld executable, install it by copying it to the /bin directory on
# therouter. 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
# commandto copy the binary file from its current location (in our case the build
#directory) to the install directory.
define Package/my_ipk/install
$(INSTALL_DIR)  $(1)/bin
$(INSTALL_BIN)  $(PKG_BUILD_DIR)/my_ipk $(1)/bin/
endef
 
 
# Thisline executes the necessary commands to compile our program.
# Theabove define directives specify all the information needed, but this
# linecalls BuildPackage which in turn actually uses this information to
# build apackage.
$(eval $(call BuildPackage,my_ipk))
3.在src目录下新建my_ipk.c文件和Makefile文件,分别如下
[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #include<stdio.h>  
  2. int main(void)  
  3. {  
  4.     printf("fuck\n");  
  5.     return 0;  
  6. }  

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. # build my_ipk executable when user executes "make"  
      
    my_ipk: my_ipk.o  
    $(CC) $(LDFLAGS) my_ipk.o -o my_ipk  
      
    my_ipk.o: my_ipk.c  
    $(CC) $(CFLAGS) -c my_ipk.c  
      
    # remove object files and executable when user executes "make clean"  
    clean:  
    rm *.o my_ipk  

4.在OpenWrt-SDK-ar71xx-generic_gcc-4.8-linaro_uClibc-0.9.33.2.Linux-i686目录下执行编译

 make  -j1 V=s

此步骤可能会出错,大部分原因是Makefile的格式问题,大家可参考官网的格式 https://wiki.openwrt.org/zh-cn/doc/devel/packages


5.成功之后会在mint@mint ~/openwrt/bin/ar71xx/OpenWrt-SDK-ar71xx-generic_gcc-4.8-linaro_uClibc-0.9.33.2.Linux-i686/bin/ar71xx/packages/base 下生产my_ipk_1_ar71xx.ipk



6.拷贝ipk到板子执行

如果你的板子网络还没有适配好,无法拷贝文件。可以把此文件一起编译进文件系统里。方法如下

cp my_ipk_1_ar71xx.ipk ~/openwrt/package/base-files/files/etc/

files下面就是你的 文件系统的根文件目录,你可以选择放入任意目录下面,我把它放进etc下面。然后跳到openwrt的根目录下,重新编译openwrt

make V=99

编译完毕后将编译出来的内核和文件系统烧写进开发板即可。

启动开发板


找到了 my_ipk_1_ar71xx.ipk

opkg install my_ipk_1_ar71xx.ipk

会生成/bin/my_ipk

通过opkg list可查看


0 0
原创粉丝点击