openWrt軟件開發教程1(交叉編譯和ipk包生成)

一、交叉編譯

1.      建立交叉編譯環境

在使用buildroot對openwrt進行編譯之後,在buildroot目錄下會有一個名叫staging_dir的目錄,針對當前平臺的toolchain都在這個目錄下。

1.1增加toolchain的目錄到PATH目錄中

Vim ~/.bash_profile

添加代碼:

# add openWrt cross-compile path

PATH=$PATH:/home/jason/openWrt/trunk/staging_dir/toolchain-i386_gcc-4.6-linaro_uClibc-0.9.33.2/bin/

1.2   增加staging_dir的目錄到toolchain PATH

Vim ~/.bash_profile

添加代碼:

STAGING_DIR=/home/jason/openWrt/trunk/staging_dir/

exportSTAGING_DIR

1.3 保存退出

2.      編譯

2.1 configure

 ./configure--target=i486-openwrt-linux-uclibc

2.2 make

make CC=i486-openwrt-linux-uclibc-gcc LD=i486-openwrt-linux-uclibc-ld

二、編譯ipk包

1.      編譯SDK

在buildroot目錄下make menuconfig,然後選中SDK進行編譯


選中後,進行make編譯。

編譯完成後,對應生成的SDK會出現類似這樣的目錄:“openWrt/trunk/bin/x86”,進入SDK後,打印當前工作路徑如下:

“/openWrt/trunk/bin/x86/OpenWrt-SDK-x86-for-redhat-x86_64-gcc-4.6-linaro_uClibc-0.9.33.2”

2.      創建工程

在sdk的package目錄下創建我們的工程“helloworld”:

新建目錄中包含src目錄,這個目錄就是我們的源代碼所在地,另外一個非常重要的文件Makefile,這個Makefile的組成與GNU的有所不同,有點類似於製作rpm包時的spec文件。到後面會有更詳細的介紹。以下是具體文件的位置:


其中helloworld.c的內容如下:


Src目錄下的Makefile文件內容如下:

最後是helloworld目錄下的Makefile的內容:

##############################################

# OpenWrtMakefile for helloworld program

#

#

# Most ofthe variables used here are defined in

# theinclude directives below. We just need to

# specifya basic description of the package,

# whereto build our program, where to find

# thesource files, and where to install the

#compiled program on the router.

#

# Be verycareful of spacing in this file.

# Indentsshould be tabs, not spaces, and

# thereshould be no trailing whitespace in

# linesthat 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

 

 

 

# Specifypackage information for this program.

# Thevariables defined here should be self explanatory.

# If youare running Kamikaze, delete the DESCRIPTION

#variable below and uncomment the Kamikaze define

# directivefor the description below

definePackage/helloworld

SECTION:=utils

CATEGORY:=Utilities

TITLE:=Helloworld-- prints a snarky message

endef

 

 

 

# Specifywhat needs to be done to prepare for building the package.

# In ourcase, we need to copy the source files to the build directory.

# This isNOT the default.  The default uses thePKG_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

 

 

# We donot need to define Build/Configure or Build/Compile directives

# Thedefaults are appropriate for compiling a simple program such as this one

 

 

# Specifywhere 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

# commandto 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

 

 

# Thisline executes the necessary commands to compile our program.

# Theabove define directives specify all the information needed, but this

# linecalls BuildPackage which in turn actually uses this information to

# build apackage.

$(eval $(call BuildPackage,helloworld))

這個Makefile的語法規則還是參考官網吧:

http://wiki.openwrt.org/doc/devel/packages

3.      編譯

將當前目錄返回到SDK:

執行make進行編譯。如果一切順利,最後的結果會保存在SDK/bin/x86/packages目錄下,名稱爲helloword_1_x86.ipk。

4.      安裝最新編譯的包

通過scp將該包拷貝到目的機器上,通過opkg包管理工具進行安裝:

Opkg install helloworld_1_x86.ipk

一切順利,包安裝成功,在當前路徑下執行helloworld查看程序執行結果。

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