Makefile语法

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

IntroductionGNUmakeconformstosection6.2ofIEEEStandard1003.2-1992(POSIX.2).(POSIX.2).Asimplemakefileconsistsofruleswiththefollowingshape:target...:prerequisites...command......Note:weneedtoputatabatthebeginningofeverycommandline!Amakefilemaycontainothertextbesidesrules,butasimplemakefileneedonlycontainrules.Rulesmaylooksomewhatmorecomplicatedthanshowninthistemplate,butallfitthepatternmoreorless.AsimpleexampleInthisexample,alltheCfilesincludedefs.h,butonlythosedefiningeditingcommandsincludecommand.h,andonlylowlevelfilesthatchangetheeditorbufferincludebuffer.h.edit:main.okbd.ocommand.odisplay.o\insert.osearch.ofiles.outils.occ-oeditmain.okbd.ocommand.odisplay.o\insert.osearch.ofiles.outils.omain.o:main.cdefs.hcc-cmain.ckbd.o:kbd.cdefs.hcommand.hcc-ckbd.ccommand.o:command.cdefs.hcommand.hcc-ccommand.cdisplay.o:display.cdefs.hbuffer.hcc-cdisplay.cinsert.o:insert.cdefs.hbuffer.hcc-cinsert.csearch.o:search.cdefs.hbuffer.hcc-csearch.cfiles.o:files.cdefs.hbuffer.hcommand.hcc-cfiles.cutils.o:utils.cdefs.hcc-cutils.cclean:rmedit*.oTousethismakefiletocreatetheexecutablefilecallededit,type:makeTousethismakefiletodeletetheexecutablefileandalltheobjectfilesfromthedirectory,type:makecleanUsingvariablesWecansimplifythismakefilebyusingvariables:objects=main.okbd.ocommand.odisplay.o\insert.osearch.ofiles.outils.oedit:$(objects)cc-oedit$(objects)main.o:main.cdefs.hcc-cmain.ckbd.o:kbd.cdefs.hcommand.hcc-ckbd.ccommand.o:command.cdefs.hcommand.hcc-ccommand.cdisplay.o:display.cdefs.hbuffer.hcc-cdisplay.cinsert.o:insert.cdefs.hbuffer.hcc-cinsert.csearch.o:search.cdefs.hbuffer.hcc-csearch.cfiles.o:files.cdefs.hbuffer.hcommand.hcc-cfiles.cutils.o:utils.cdefs.hcc-cutils.cclean:rmedit$(objects)Usingc-oimplicitruleItisnotnecessarytospelloutthecommandsforcompilingtheindividualCsourcefiles,becausemakecanfigurethemout:ithasanimplicitruleforupdatinga.ofilefromacorrespondinglynamed.cfileusingacc-ccommand.Forexample,itwillusethecommandcc-cmain.c-omain.otocompilemain.cintomain.o.Wecanthereforeomitthecommandsfromtherulesfortheobjectfiles.Whena.cfileisusedautomaticallyinthisway,itisalsoautomaticallyaddedtothelistofprerequisites.Wecanthereforeomitthe.cfilesfromtheprerequisites,providedweomitthecommands.Hereisanexample:objects=main.okbd.ocommand.odisplay.o\insert.osearch.ofiles.outils.oedit:$(objects)cc-oedit$(objects)main.o:defs.hkbd.o:defs.hcommand.hcommand.o:defs.hcommand.hdisplay.o:defs.hbuffer.hinsert.o:defs.hbuffer.hsearch.o:defs.hbuffer.hfiles.o:defs.hbuffer.hcommand.hutils.o:defs.h.PHONY:cleanclean:rmedit$(objects)Whentheobjectsofamakefilearecreatedonlybyimplicitrules,analternativestyleofmakefileispossible.Inthisstyleofmakefile,wegroupentriesbytheirprerequisitesinsteadofbytheirtargets:objects=main.okbd.ocommand.odisplay.o\insert.osearch.ofiles.outils.oedit:$(objects)cc-oedit$(objects)$(objects):defs.hkbd.ocommand.ofiles.o:command.hdisplay.oinsert.osearch.ofiles.o:buffer.hHeredefs.hisgivenasaprerequisiteofalltheobjectfiles;command.handbuffer.hareprerequisitesofthespecificobjectfileslistedforthem.ThefilenameBydefault,whenmakelooksforthemakefile,ittriesthefollowingnames,inorder:GNUmakefile,makefileandMakefile.NormallyweshouldcallthemakefileeithermakefileorMakefile.Makefileisrecommandedbecauseitappearsprominentlynearthebeginningofadirectorylisting,rightnearotherimportantfilessuchasREADME.GNUmakefileisnotrecommendedformostmakefiles.WeshouldusethisnameifwehaveamakefilethatisspecifictoGNUmake,andwillnotbeunderstoodbyotherversionsofmake.OthermakeprogramslookforMakefileandmakefile,butnotGNUmakefile.Ifmakefindsnoneofthesenames,itdoesnotuseanymakefile.Thenwemustspecifyagoalwithacommandargument,andmakewillattempttofigureouthowtoremakeitusingonlyitsbuilt-inimplicitrules.Ifwewanttouseanonstandardnameforourmakefile,wecanspecifythemakefilenamewiththe-for--fileoption.Thearguments-fnameor--file=nametellmaketoreadthefilenameasthemakefile.Ifweusemorethanone-for--fileoption,wecanspecifyseveralmakefiles.Allthemakefilesareeffectivelyconcatenatedintheorderspecified.ThedefaultmakefilenamesGNUmakefile,makefileandMakefilearenotcheckedautomaticallyifwespecify-for--file.IncludingothermakefilesincludeTheincludedirectivetellsmaketosuspendreadingthecurrentmakefileandreadoneormoreothermakefilesbeforecontinuing.Thedirectiveisalineinthemakefilethatlookslikethis:includefilenames...Extraspacesareallowedandignoredatthebeginningoftheline,butatabisnotallowed.Whitespaceisrequiredbetweenincludeandthefilenames,andbetweenfilenames;extrawhitespaceisignoredthereandattheendofthedirective.Acommentstartingwith‘#’isallowedattheendoftheline.Ifthefilenamescontainanyvariableorfunctionreferences,theyareexpanded.Forexample,ifwehavethree.mkfiles,a.mk,b.mk,and‘c.mk’,and$(bar)expandstobishbash,thenincludefoo*.mk$(bar)isequivalenttoincludefooa.mkb.mkc.mkbishbashWhenmakeprocessesanincludedirective,itsuspendsreadingofthecontainingmakefileandreadsfromeachlistedfileinturn.Whenthatisfinished,makeresumesreadingthemakefileinwhichthedirectiveappears.Ifthespecifiednameisnotanabsolutepath,thefileissearche

1 / 62
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功