文章出處:http://blog.csdn.net/kingvenll/article/details/27545221linux
此次講講openwrt的結構.web
1. 代碼上來看有幾個重要目錄package, target, build_root, bin, dl....vim
---build_dir/host目錄是創建工具鏈時的臨時目錄網絡
---build_dir/toolchain-<arch>*是對應硬件的工具鏈的目錄app
---staging_dir/toolchain-<arch>* 則是工具鏈的安裝位置工具
---target/linux/<platform>目錄裏面是各個平臺(arch)的相關代碼ui
---target/linux/<platform>/config-3.10文件就是配置文件了this
---dl目錄是'download'的縮寫, 在編譯前期,須要從網絡下載的數據包都會放在這個目錄下,這些軟件包的一個特色就是,會自動安裝在所編譯的固件中,也就是咱們make menuconfig的時候,爲固件配置的一些軟件包。若是咱們須要更改這些源碼包,只須要將更改好的源碼包打包成相同的名字放在這個目錄下,而後開始編譯便可。編譯時,會將軟件包解壓到build_dir目錄下。spa
---而在build_dir/目錄下進行解壓,編譯和打補丁等。.net
---package目錄裏面包含了咱們在配置文件裏設定的全部編譯好的軟件包。默認狀況下,會有默認選擇的軟件包。在openwrt中ipk就是一切, 咱們可使用
$ ./scripts/feeds update來對軟件包進行更新.
$ ./scripts/feeds search nmap 查找軟件包'nmap'
Search results in feed ’packages’:
nmap Network exploration and/or security auditing utility
$ ./scripts/feeds install nmap 安裝'nmap'這個軟件
$ make package/symlinks //估計意思是更新軟件源之類的
---bin目錄下生成了不少bin文件,根據不一樣的平臺來區分。另外bin/<platform>/package目錄,裏面有不少ipk後綴的文件,都是package目錄下的源碼在build_dir目錄下編譯後的生成的結果。
2. 新建本身的packages
對於本身新建的package,而這個package又不須要隨固件一塊兒安裝,換句話說,就是能夠當作一個可選軟件包的話。咱們能夠利用咱們的SDK環境來單獨編譯,編譯後會生成一個ipk的文件包。而後利用 opkg install xxx.ipk 來安裝這個軟件。
下面具體說下,如何編譯一個helloword的軟件包。
(1)首先,編寫helloworld程序
編寫helloworld.c
/****************
* Helloworld.c
* The most simplistic C program ever written.
* An epileptic monkey on crack could write this code.
*****************/
#include <stdio.h>
#include <unistd.h>
int main(void)
{
printf("Hell! O' world, why won't my code compile?\n\n");
return 0;
}
編寫Makefile文件
# build helloworld executable when user executes "make"
helloworld: helloworld.o
$(CC) $(LDFLAGS) helloworld.o -o helloworld
helloworld.o: helloworld.c
$(CC) $(CFLAGS) -c helloworld.c
# remove object files and executable when user executes "make clean"
clean:
rm *.o helloworld
在這兩個文件的目錄下,執行make 應該能夠生成helloworld的可執行文件。執行helloworld後,可以打印出「Hell!O' world, why won't my code compile?」。 這一步,主要保證咱們的源程序是能夠正常編譯的。
下面咱們將其移植到OpenWRT上。
(2)將OpenWrt-SDK-brcm47xx-for-Linux-x86_64-gcc-4.3.3+cs_uClibc-0.9.30.1.tar.bz2解壓
tar –xvf OpenWrt-SDK-brcm47xx-for-Linux-x86_64-gcc-4.3.3+cs_uClibc-0.9.30.1.tar.bz2
(3)進入SDK
cd OpenWrt-SDK-brcm47xx-for-Linux-x86_64-gcc-4.3.3+cs_uClibc-0.9.30.1
能夠看到裏面的目錄結構跟咱們以前source的目錄結構基本相同,所須要編譯的軟件包,須要放置在package目錄下
(4)在package目錄下建立helloworld目錄
cd package
mkdir helloworld
cd helloworld
(5)建立src目錄,拷貝 helloworld文件
mkdir src
cp /home/wrt/test/helloworld.c src
cp /home/wrt/test/Makefile src
(6)在helloworld目錄下建立Makefile文件
這個Makefile文件是給OpenWRT讀的,而以前寫的那個Makefile文件是針對helloworld給編譯其讀的。兩個Makefile不在同一層目錄下。
touch Makefile
vim Makefile
Makefile文件模板內容以下:############################################### OpenWrt Makefile for helloworld program### Most of the variables used here are defined in# the include directives below. We just need to# specify a basic description of the package,# where to build our program, where to find# the source files, and where to install the# compiled program on the router.## Be very 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# Name and release number of this packagePKG_NAME:=helloworldPKG_RELEASE:=1# This specifies the directory where we're going to build the program. # The root build directory, $(BUILD_DIR), is by default the build_mipsel# directory in your OpenWrt SDK directoryPKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME)include $(INCLUDE_DIR)/package.mk # Specify package information for this program.# The variables defined here should be self explanatory.# If you are running Kamikaze, delete the DESCRIPTION# variable below and uncomment the Kamikaze define# directive for the description belowdefine Package/helloworld SECTION:=utils CATEGORY:=Utilities TITLE:=Helloworld -- prints a snarky messageendef# Uncomment portion below for Kamikaze and delete DESCRIPTION variable abovedefine Package/helloworld/description If you can't figure out what this program does, you're probably brain-dead and need immediate medical attention.endef # Specify what needs to be done to prepare for building the package.# In our case, we need to copy the source files to the build directory.# This is 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.# In order to just build a simple program that we have just written, it is# much easier to do it this way.define Build/Prepare mkdir -p $(PKG_BUILD_DIR) $(CP) ./src/* $(PKG_BUILD_DIR)/endef# We do not need to define Build/Configure or Build/Compile directives# The defaults 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,# the helloworld executable, install it by copying it to the /bin directory on# the router. 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.define Package/helloworld/install $(INSTALL_DIR) $(1)/bin $(INSTALL_BIN) $(PKG_BUILD_DIR)/helloworld $(1)/bin/endef# This line executes the necessary commands to compile our program.# The above define directives specify all the information needed, but this# line calls BuildPackage which in turn actually uses this information to# build a package.$(eval $(call BuildPackage,helloworld))(7)返回到SDK的根目錄執行make進行編譯編譯過程會在build_dir目錄下完成編譯結果會放在 bin/[yourtarget]/package目錄下helloworld_1_bcm47xx.ipk(8)上傳helloworld_1_bcm47xx.ipk上傳helloworld_1_bcm47xx.ipk至路由器執行# opkg install helloworld_1_bcm47xx.ipk而後輸入hello而後按Tab鍵,發現openwrt中已經有helloworld可執行命令。執行 helloworld命令來查看程序的效果。Hell! O' world, why won't my code compile?