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<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。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章