码迷,mamicode.com
首页 > 其他好文 > 详细

Openwrt 编译添加模块 Package

时间:2015-01-17 18:08:54      阅读:396      评论:0      收藏:0      [点我收藏+]

标签:openwrt

3.  添加模块

一般我们需要建立自己的模块(package),在编译固件时可以选择是否将自己的模块编译到固件中去。

3.1. 建立package

最终helloword文件目录结构为:

helloword/
├──Makefile
└── src
   ├── helloworld.c
   └── Makefile


在./openwrt/trunk/package/utils/目录下新建helloword文件夹。

然后在helloword文件夹下新建src文件夹。

3.2. 在src目录下编写helloworld程序

在src目录下,编写helloworld.c

#include<stdio.h>
int main(void)
{
<span style="white-space:pre">	</span>printf("Helloworld\n");
<span style="white-space:pre">	</span>return 0;
}



在src目录下编写Makefile文件,注意Makefile文件格式,尤其是Tab制表符缩进

helloworld:helloworld.o
<span style="white-space:pre">	</span>$(CC) $(LDFLAGS) helloworld.o -o helloworld
helloworld.o:helloworld.c
<span style="white-space:pre">	</span>$(CC) $(CFLAGS) -c helloworld.c
clean:
<span style="white-space:pre">	</span>rm *.o helloworld


3.3. 在helloworld目录下创建Makefile文件

       这个Makefile文件是给OpenWRT读的,而之前写的那个Makefile文件是针对helloworld给编译其读的。两个Makefile不在同一层目录下。注意Makefile文件格式,尤其是Tab制表符缩进

##############################################
#OpenWrt Makefile for helloworld program
#
#
# Mostof the variables used here are defined in
# theinclude directives below. We just need to
#specify a basic description of the package,
#where to build our program, where to find
# thesource files, and where to install the
#compiled program on the router.
#
# Bevery 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
# Nameand release number of this package
PKG_NAME:=helloworld
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

#Specify package information for this program.
# Thevariables defined here should be self explanatory.
# Ifyou are running Kamikaze, delete the DESCRIPTION
#variable below and uncomment the Kamikaze define
#directive for the description below
definePackage/helloworld
SECTION:=utils
CATEGORY:=Utilities
TITLE:=Helloworld-- prints a snarky message
endef

#Uncomment portion below for Kamikaze and delete DESCRIPTION variable above
definePackage/helloworld/description
If youcan't figure out what this program does, you're probably
brain-deadand need immediate medical attention.
endef

#Specify what needs to be done to prepare for building the package.
# Inour case, we need to copy the source files to the build directory.
# Thisis 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.
# Inorder to just build a simple program that we have just written, it is
# mucheasier to do it this way.
defineBuild/Prepare
<span style="white-space:pre">	</span>mkdir -p $(PKG_BUILD_DIR)
<span style="white-space:pre">	</span>$(CP) ./src/* $(PKG_BUILD_DIR)/
endef

# Wedo not need to define Build/Configure or Build/Compile directives
# Thedefaults 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,
# 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
#command to copy the binary file from its current location (in our case the build
#directory) to the install directory.
definePackage/helloworld/install
<span style="white-space:pre">	</span>$(INSTALL_DIR) $(1)/bin
<span style="white-space:pre">	</span>$(INSTALL_BIN) $(PKG_BUILD_DIR)/helloworld $(1)/bin/
endef

$(eval$(call BuildPackage,helloworld))



3.4 helloworld模块

在trunk目录下,敲make menuconfig                 

选择Utilities

 技术分享


在Utilities中选择就可以看到helloworld,选择该模块就可以将helloword模块编译到固件中去。

技术分享

 

在trunk目录下make重新编译固件后

编译结果会放在 bin/[yourtarget]/package目录下helloworld_1_xx.ipk,

也可能在bin/[yourtarget]/package的子目录下。可以通过find . –name hello*命令来查找编译好的模块helloworld。


Openwrt 编译添加模块 Package

标签:openwrt

原文地址:http://blog.csdn.net/lichao_ustc/article/details/42805631

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!