Openwrt 编译添加模块 Package

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
int main(void)
{
	printf("Helloworld\n");
	return 0;
}



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

helloworld:helloworld.o
	$(CC) $(LDFLAGS) helloworld.o -o helloworld
helloworld.o:helloworld.c
	$(CC) $(CFLAGS) -c helloworld.c
clean:
	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
	mkdir -p $(PKG_BUILD_DIR)
	$(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
	$(INSTALL_DIR) $(1)/bin
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/helloworld $(1)/bin/
endef

$(eval$(call BuildPackage,helloworld))



3.4 helloworld模块

在trunk目录下,敲make menuconfig                 

选择Utilities

 Openwrt 编译添加模块 Package_第1张图片


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

Openwrt 编译添加模块 Package_第2张图片

 

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

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

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


你可能感兴趣的:(OpenWrt)