Makefile新手入门:How to write Makefile
2013-05-07 14:42
330 查看
I'vealwaysthoughtthiswaseasiertolearnwithadetailedexample,sohere'showIthinkofmakefiles.Foreachsectionyouhaveonelinethat'snotindentedanditshowsthenameofthesectionfollowedbydependencies.Thedependenciescanbeeitherothersections(whichwillberunbeforethecurrentsection)orfiles(whichifupdateswillcausethecurrentsectiontoberunagain).
Here'saquickexample(keepinmindthatI'musing4spaceswhereIshouldbeusingatab,SOwon'tletmeusetabs):
all:a3driver.o
g++-oa3drivera3driver.o
a3driver.o:a3driver.cpp
g++-ca3driver.cpp
Whenyoutype
Sincethere'sonlyonefile,itcouldevenbereducedto:
a3driver:a3driver.cpp
g++-oa3drivera3driver.cpp
ThereasonIshowedthefirstexampleisthatitshowsthepowerofmakefiles.Ifyouneedtocompileanotherfile,youcanjustaddanothersection.Here'sanexamplewithasecondFile.cpp(whichloadsinaheadernamedsecondFile.h):
all:a3driver.osecondFile.o
g++-oa3drivera3driver.osecondFile.o
a3driver.o:a3driver.cpp
g++-ca3driver.cpp
secondFile.o:secondFile.cppsecondFile.h
g++-csecondFile.cpp
ThiswayifyouchangesomethinginsecondFile.cpporsecondFile.handrecompile,itwillonlyrecompilesecondFile.cpp(nota3driver.cpp).Oralternately,ifyouchangesomethingina3driver.cpp,itwon'trecompilesecondFile.cpp.
######################################################################################################
Dependencyareatreeofrulesthatlooklikethis:
main_target:source1source2etc
commandtobuildmain_targetfromsources
source1:dependentsforsource1
commandtobuildsource1
Theremustbeablanklineafterthecommandsforatarget,andtheremustnotbeablanklinebeforethecommands.Thefirsttargetinthemakefileistheoverallgoal,othertargetsarebuiltonlyifthefirsttargetdependsonthem.
Soyourmakefilewilllooksomethinglikethis.
a3a.exe:a3driver.obj
link/out:a3aa3driver.obj
a3driver.obj:a3driver.cpp
cca3driver.cpp
PS:HarvestfromStackflow'target='_blank'>http://stackoverflow.com/questions/2481269/how-to-make-simple-c-makefile[/code]
Here'saquickexample(keepinmindthatI'musing4spaceswhereIshouldbeusingatab,SOwon'tletmeusetabs):
Whenyoutype
make,itwillchoosethefirstsection(all).alldependsona3driver.o,soitwillgotothatsection.a3driver.odependsona3driver.cpp,soitwillonlyrunifa3driver.cpphaschangedsinceitwaslastrun.Assumingithas(orhasneverbeenrun),itwillcompilea3driver.cpptoa.ofile,thengobacktoallandcompilethatintothefileexecutable.
Sincethere'sonlyonefile,itcouldevenbereducedto:
ThereasonIshowedthefirstexampleisthatitshowsthepowerofmakefiles.Ifyouneedtocompileanotherfile,youcanjustaddanothersection.Here'sanexamplewithasecondFile.cpp(whichloadsinaheadernamedsecondFile.h):
ThiswayifyouchangesomethinginsecondFile.cpporsecondFile.handrecompile,itwillonlyrecompilesecondFile.cpp(nota3driver.cpp).Oralternately,ifyouchangesomethingina3driver.cpp,itwon'trecompilesecondFile.cpp.
######################################################################################################
Dependencyareatreeofrulesthatlooklikethis:
Theremustbeablanklineafterthecommandsforatarget,andtheremustnotbeablanklinebeforethecommands.Thefirsttargetinthemakefileistheoverallgoal,othertargetsarebuiltonlyifthefirsttargetdependsonthem.
Soyourmakefilewilllooksomethinglikethis.
相关文章推荐
- how to write a makefile
- How to write makefile
- Go语言入门系列1:安装,How to Write Go Code
- Go语言入门系列1:安装,How to Write Go Code
- How to write a Makefile
- how to write Makefile
- How to write a Makefile for ARM board
- How to write a Makefile
- How to write a simple Makefile
- how to write a makefile
- How to write a simple Makefile
- how to write a simple makefile
- How to write a Makefile
- How to write the better Makefile for cpputest
- How to write a Makefile
- how to write Makefile
- how to write makefile
- How to write Makefile for kernel modules
- How-to: write own Kafka Partitioner based on requirement