Openwrt 編譯新增模組 Package
阿新 • • 發佈:2019-01-22
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。