Writing and Compiling A Simple Program For OpenWrt.docx
- 文档编号:10270705
- 上传时间:2023-02-09
- 格式:DOCX
- 页数:42
- 大小:32.25KB
Writing and Compiling A Simple Program For OpenWrt.docx
《Writing and Compiling A Simple Program For OpenWrt.docx》由会员分享,可在线阅读,更多相关《Writing and Compiling A Simple Program For OpenWrt.docx(42页珍藏版)》请在冰豆网上搜索。
WritingandCompilingASimpleProgramForOpenWrt
WritingandCompilingASimpleProgramForOpenWrt
WrittenbyEricBishop
PartI:
ASimpleProgramInCadded8/23/2007
PartII:
C++andtheStandardTemplateLibrary(STL)added10/10/2007
PartIII:
BuildingandUsingtheKamikazeSDKadded12/9/2007
Introduction
I'mwritingthisdocumentbecauseIfoundthedocumentationavailableontheOpenWrtwiki(http:
//wiki.openwrt.org)difficulttofollow.IfoundmyselfinthepositionofwantingtocompileaverysimpleprogramthatIcouldrunonmyOpenWrtrouter.Iendedupspendinghourswadingthroughthefrustratinglyincompletedocumentationonthewiki,goingthroughdozensofforumposts,andconductingextensivetrial-and-errorbeforemycodewouldcompile.Iespeciallyappreciatedtheexamplesonthewikithatcontainthewarning:
"NotethisMakefileisprovidedasanexampleonly;itwillnotcompile."Ifsomethingdoesn'twork,itisn'taverygoodexample,isit?
Here,then,isa(hopefully)morestraightforwardguidetobuildingaprogramforOpenWrt.Ifoundthatexistingdocumentationfocusesmoreonportingexisting,complicatedprogramstoOpenWrt.Myintentionistofocusongettingasmall,verysimple,home-grownapplicationrunningonOpenWrt.Mygoalistoexplainthisinassimpleandcompleteamanneraspossible,explainingeachandeverystepnecessarytowriteandcompileaprogramthatwillrunonOpenWrt.Theprocessisactuallyverysimpleandstraightforward--providedyouknowwhatyou'redoing.ForthepurposesofthistutorialI'mgoingtoassumeyouhaveadevelopmentboxrunninglinuxandarouterrunningOpenWrt.IwillalsoassumeyouareatleastsomewhatfamiliarwithC/C++andstandardUnixMakefiles.
Thecodefortheexamplesinthistutorialcanbedownloadedfromhere.Theexamplefromthefirstpartofthetutorialisintheopenwrt-programming-examples/cdirectoryandtheexamplefortheWhiteRussianportionofthesecondpartisintheopenwrt-programming-examples/c++directory.ThemodifiedKamikazeSDKMakefilereferredtoinpartIIIisalsoincludedinthiszipfile.
PartI:
ASimplePrograminC
First,we'regoingtoneedtowritethecodefortheprogramitselfandgetitcompilingonourlocallinuxmachine.Let'swriteasimple"helloworld"programthatwewanttorunontherouter:
~/helloworld/src/helloworld.c:
/****************
*Helloworld.c
*ThemostsimplisticCprogrameverwritten.
*Anepilepticmonkeyoncrackcouldwritethiscode.
*****************/
#include
intmain(void)
{
printf("Hell!
O'world,whywon'tmycodecompile?
\n\n");
return0;
}
Alright,wehaveourcode.Notethelocationofthisfile.Makeahelloworlddirectoryandthenasrcsubdirectory.Placethecodeinthesrcsubdirectory.Now,let'swriteastandardUnixMakefiletocompilethiscodeforus:
~/helloworld/src/Makefile:
#buildhelloworldexecutablewhenuserexecutes"make"
helloworld:
helloworld.o
$(CC)$(LDFLAGS)helloworld.o-ohelloworld
helloworld.o:
helloworld.c
$(CC)$(CFLAGS)-chelloworld.c
#removeobjectfilesandexecutablewhenuserexecutes"makeclean"
clean:
rm*.ohelloworld
Noticethatinsteadofhard-coding"gcc"inthemakefiletocompiletheprogram,weuseavariablethatholdstheCcompiler,$(CC).Ifyou'recompilingac++programyouwoulduse$(CXX)insteadof$(CC)and$(CXXFLAGS)insteadof$(CFLAGS).Theuseofthecompilervariableisnotnecessarytocompilethecodelocally,butinordertocompilethecodeforOpenWRTitiscriticalbecausewewon'tbeusingthestandardgcccompiler.Placethemakefileinthesamesrcdirectoryourcodeisin.Wecannowgotothesrcdirectory,type"make"andtheprogramshouldcompile.Youcanrunitbytyping"./helloworld"
Besuretorun"makeclean"toremovethecompiledbinaryandobjectfilesafteryouhavesuccessfullytestedtheprogram.We'regoingtobuildthesourcefilesinthisdirectoryfortherouter,andiftheoldobjectfileisn'tdeleted,itwillbeusedinsteadofthenewoneweneedtogeneratethatiscompatiblewithOpenWrt.
mockingbird@linuxbox:
~/helloworld/src$make
cc-chelloworld.c
cchelloworld.o-ohelloworld
mockingbird@linuxbox:
~/helloworld/src$./helloworld
Hell!
O'world,whywon'tmycodecompile?
mockingbird@linuxbox:
~/helloworld/src$makeclean
rm*.ohelloworld
mockingbird@linuxbox:
~/helloworld/src$
SofarthisshouldbeareviewonhowtowritesimpleCprogramsandhowtouseMakefiles.Nowcomesthetrickypart,compilingthecodesothatitwillrunonourrouter.Therouterusesadistinctlydifferentarchitecturethanourlinuxdevelopmentbox.Becausethereisn'tenoughmemory/diskspaceontheroutertoinstallacompilerandcompilethecodenatively,weneedto"cross-compile"thecodeonourdevelopmentboxforuseontherouter.TodothisweneedaspecialcompileranddevelopmentenvironmentcalledtheOpenWRTSDK.YoucandownloadtheSDKfromhttp:
//downloads.openwrt.orgTheSDKvariesdependingonthearchitectureofyourdevelopmentbox,thearchitectureofyourrouterandtheversion/releaseofOpenWrtyourrouterisrunning.Icurrentlyhavewhiterussianv0.9installedonmyLinksysWRT54Grouter,andmydevelopmentboxisani686,sotheSDKIuseisthisone.ExtracttheSDKfilesfromthedownloadedarchive,andentertheSDKdirectory,whichshouldhavethesamenameasthetar.bz2file(inmycaseOpenWrt-SDK-Linux-i686-1).
mockingbird@linuxbox:
~$tarxfjOpenWrt-SDK-Linux-i686-1.tar.bz2
mockingbird@linuxbox:
~$cdOpenWrt-SDK-Linux-i686-1
mockingbird@linuxbox:
~/OpenWrt-SDK-Linux-i686-1$ls
dldocsexamplesincludeMakefilepackageREADME.SDKrules.mkscripts
staging_dir_mipsel
mockingbird@linuxbox:
~/OpenWrt-SDK-Linux-i686-1$
OurgoalistobuildapackageforOpenWrtusingthesourcewealreadyhave.Whenyouexecutethe"make"commandintheSDKdirectory,theSDKwillcompileallproperlyconfiguredpackagesinthepackagesubdirectoryundertheSDKdirectory.Thenextstep(andthetrickiest)istoproperlyconfigureourcodesothattheSDKwillbuildit.First,copythehelloworlddirectorywemadeearlierintothepackagesubdirectoryoftheSDK:
mockingbird@linuxbox:
~/OpenWrt-SDK-Linux-i686-1$cp-r~/helloworldpackage
mockingbird@linuxbox:
~/OpenWrt-SDK-Linux-i686-1$
InordertotelltheOpenWrtSDKhowtobuildourprogramweneedtocreateaspecialMakefileinthehelloworlddirectory,abovethesrcdirectorywhichcontainsourconventionalmakefile.Writingthisfileis90%oftheworkinvolvedincompilingourprogramforOpenWrt.BelowisanOpenWrtmakefileforbuildingthehelloworldprogram.Eachsectionisheavilycommentedsothatitshouldbefairlyclearwhatisgoingon:
~/OpenWrt-SDK-Linux-i686-1/package/helloworld/Makefile:
##############################################
#OpenWrtMakefileforhelloworldprogram
#
#
#Mostofthevariablesusedherearedefinedin
#theincludedirectivesbelow.Wejustneedto
#specifyabasicdescriptionofthepackage,
#wheretobuildourprogram,wheretofind
#thesourcefiles,andwheretoinstallthe
#compiledprogramontherouter.
#
#Beverycarefulofspacinginthisfile.
#Indentsshouldbetabs,notspaces,and
#thereshouldbenotrailingwhitespacein
#linesthatarenotcommented.
#
##############################################
include$(TOPDIR)/rules.mk
#Nameandreleasenumberofthispackage
PKG_NAME:
=helloworld
PKG_RELEASE:
=1
#Thisspecifiesthedirectorywherewe'regoingtobuildtheprogram.
#Therootbuilddirectory,$(BUILD_DIR),isbydefaultthebuild_mipsel
#directoryinyourOpenWrtSDKdirectory
PKG_BUILD_DIR:
=$(BUILD_DIR)/$(PKG_NAME)
include$(INCLUDE_DIR)/package.mk
#Specifypackageinformationforthisprogram.
#Thevariablesdefinedhereshouldbeselfexplanatory.
#IfyouarerunningKamikaze,deletetheDESCRIPTION
#variablebelowanduncommenttheKamikazedefine
#directiveforthedescriptionbelow
definePackage/helloworld
SECTION:
=utils
CATEGORY:
=Utilities
TITLE:
=Helloworld--printsasnarkymessage
DESCRIPTION:
=\
Ifyoucan'tfigureoutwhatthisprogramdoes,\\\
you'reprobablybrain-deadandneedimmediate\\\
medicalattention.
endef
#UncommentportionbelowforKamikazeanddeleteDESCRIPTIONvariableabove
#definePackage/helloworld/description
#Ifyoucan'tfigureoutwhatthisprogramdoes,you'reprobably
#brain-deadandneedimmediatemedicalattention.
#endef
#Specifywhatneedstobedonetoprepareforbuildingthepackage.
#Inourcase,weneedtocopythesourcefilestothebuilddirectory.
#ThisisNOTthedefault.ThedefaultusesthePKG_SOURCE_URLandthe
#PKG_SOURCEwhichisnotdefinedheretodownloadthesourcefromtheweb.
#Inordertojustbuildasimpleprogramthatwehavejustwritten,itis
#mucheasiertodoitthisway.
defineBuild/Prepare
mkdir-p$(PKG_BUILD_DIR)
$(CP)./src/*$(PKG_BUILD_DIR)/
endef
#WedonotneedtodefineBuild/ConfigureorBuild/Compiledirectives
#Thedefaultsareappropriateforcompilingasimpleprogramsuchasthisone
#Specifywhereandhowtoinstalltheprogram.Sinceweonlyhaveonefile,
#thehelloworldexecutable,installitbycopyingittothe/bindirectoryon
#therouter.The$
(1)variablerepresentstherootdirectoryontherouterrunning
#OpenWrt.The$(INSTALL_DIR)variablecontainsacommandtopreparetheinstall
#directoryifitdoesnotalreadyexist.Likewise$(INSTALL_BIN)containsthe
#commandtocopythebinaryfilefromitscurrentlocation(inourcasethebuild
#directory)totheinstalldirectory.
definePackage/helloworld/install
$(INSTALL_DIR)$
(1)/bin
$(INSTA
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Writing and Compiling Simple Program For OpenWrt
![提示](https://static.bdocx.com/images/bang_tan.gif)
链接地址:https://www.bdocx.com/doc/10270705.html