ºìÁªLinuxÃÅ»§
Linux°ïÖú

¸úÎÒÒ»Æðд Makefile

·¢²¼Ê±¼ä:2006-06-11 11:29:39À´Ô´:ºìÁª×÷Õß:ºãÉÆϲ
ʲôÊÇmakefile£¿»òÐíºÜ¶àWinodwsµÄ³ÌÐòÔ±¶¼²»ÖªµÀÕâ¸ö¶«Î÷£¬ÒòΪÄÇЩWindowsµÄIDE¶¼
ΪÄã×öÁËÕâ¸ö¹¤×÷£¬µ«ÎÒ¾õµÃÒª×÷Ò»¸öºÃµÄºÍprofessionalµÄ³ÌÐòÔ±£¬makefile»¹ÊÇÒª¶®¡£
Õâ¾ÍºÃÏñÏÖÔÚÓÐÕâô¶àµÄHTMLµÄ±à¼­Æ÷£¬µ«Èç¹ûÄãÏë³ÉΪһ¸öרҵÈËÊ¿£¬Ä㻹ÊÇÒªÁ˽âHTM
LµÄ±êʶµÄº¬Òå¡£ÌرðÔÚUnixϵÄÈí¼þ±àÒ룬Äã¾Í²»Äܲ»×Ô¼ºÐ´makefileÁË£¬»á²»»áдmakef
ile£¬´ÓÒ»¸ö²àÃæ˵Ã÷ÁËÒ»¸öÈËÊÇ·ñ¾ß±¸Íê³É´óÐ͹¤³ÌµÄÄÜÁ¦¡£
ÒòΪ£¬makefile¹Øϵµ½ÁËÕû¸ö¹¤³ÌµÄ±àÒë¹æÔò¡£Ò»¸ö¹¤³ÌÖеÄÔ´Îļþ²»¼ÆÊý£¬Æä°´ÀàÐÍ¡¢¹¦
ÄÜ¡¢Ä£¿é·Ö±ð·ÅÔÚÈô¸É¸öĿ¼ÖУ¬makefile¶¨ÒåÁËһϵÁеĹæÔòÀ´Ö¸¶¨£¬ÄÄЩÎļþÐèÒªÏȱà
Ò룬ÄÄЩÎļþÐèÒªºó±àÒ룬ÄÄЩÎļþÐèÒªÖØбàÒ룬ÉõÖÁÓÚ½øÐиü¸´ÔӵŦÄܲÙ×÷£¬ÒòΪma
kefile¾ÍÏñÒ»¸öShell½Å±¾Ò»Ñù£¬ÆäÖÐÒ²¿ÉÒÔÖ´ÐвÙ×÷ϵͳµÄÃüÁî¡£
makefile´øÀ´µÄºÃ´¦¾ÍÊÇ----¡°×Ô¶¯»¯±àÒ롱£¬Ò»µ©Ð´ºÃ£¬Ö»ÐèÒªÒ»¸ömakeÃüÁÕû¸ö¹¤³Ì
ÍêÈ«×Ô¶¯±àÒ룬¼«´óµÄÌá¸ßÁËÈí¼þ¿ª·¢µÄЧÂÊ¡£makeÊÇÒ»¸öÃüÁ¾ß£¬ÊÇÒ»¸ö½âÊÍmakefil
eÖÐÖ¸ÁîµÄÃüÁ¾ß£¬Ò»°ãÀ´Ëµ£¬´ó¶àÊýµÄIDE¶¼ÓÐÕâ¸öÃüÁ±ÈÈ磺DelphiµÄmake£¬Visual
C++µÄnmake£¬LinuxÏÂGNUµÄmake¡£¿É¼û£¬makefile¶¼³ÉΪÁËÒ»ÖÖÔÚ¹¤³Ì·½ÃæµÄ±àÒë·½·¨¡£
ÏÖÔÚ½²ÊöÈçºÎдmakefileµÄÎÄÕ±ȽÏÉÙ£¬ÕâÊÇÎÒÏëдÕâƪÎÄÕµÄÔ­Òò¡£µ±È»£¬²»Í¬²úÉ̵Äma
ke¸÷²»Ïàͬ£¬Ò²Óв»Í¬µÄÓï·¨£¬µ«Æä±¾Öʶ¼ÊÇÔÚ¡°ÎļþÒÀÀµÐÔ¡±ÉÏ×öÎÄÕ£¬ÕâÀÎÒ½ö¶ÔGN
UµÄmake½øÐн²Êö£¬ÎҵĻ·¾³ÊÇRedHat Linux 8.0£¬makeµÄ°æ±¾ÊÇ3.80¡£±Ø¾¹£¬Õâ¸ömakeÊÇ
Ó¦ÓÃ×îΪ¹ã·ºµÄ£¬Ò²ÊÇÓõÃ×î¶àµÄ¡£¶øÇÒÆ仹ÊÇ×î×ñÑ­ÓÚIEEE 1003.2-1992 ±ê×¼µÄ£¨POSIX
.2£©¡£
ÔÚÕâƪÎĵµÖУ¬½«ÒÔC/C++µÄÔ´Âë×÷ΪÎÒÃÇ»ù´¡£¬ËùÒÔ±ØÈ»É漰һЩ¹ØÓÚC/C++µÄ±àÒëµÄ֪ʶ
£¬Ïà¹ØÓÚÕâ·½ÃæµÄÄÚÈÝ£¬»¹Çë¸÷λ²é¿´Ïà¹ØµÄ±àÒëÆ÷µÄÎĵµ¡£ÕâÀïËùĬÈϵıàÒëÆ÷ÊÇUNIXÏÂ
µÄGCCºÍCC¡£
¹ØÓÚ³ÌÐòµÄ±àÒëºÍÁ´½Ó
--------------------
ÔÚ´Ë£¬ÎÒÏë¶à˵¹ØÓÚ³ÌÐò±àÒëµÄһЩ¹æ·¶ºÍ·½·¨£¬Ò»°ãÀ´Ëµ£¬ÎÞÂÛÊÇC¡¢C++¡¢»¹ÊÇpas£¬Ê×
ÏÈÒª°ÑÔ´Îļþ±àÒë³ÉÖмä´úÂëÎļþ£¬ÔÚWindowsÏÂÒ²¾ÍÊÇ .obj Îļþ£¬UNIXÏÂÊÇ .o Îļþ£¬
¼´ Object File£¬Õâ¸ö¶¯×÷½Ð×ö±àÒ루compile£©¡£È»ºóÔÙ°Ñ´óÁ¿µÄObject FileºÏ³ÉÖ´ÐÐÎÄ
¼þ£¬Õâ¸ö¶¯×÷½Ð×÷Á´½Ó£¨link£©¡£
±àÒëʱ£¬±àÒëÆ÷ÐèÒªµÄÊÇÓï·¨µÄÕýÈ·£¬º¯ÊýÓë±äÁ¿µÄÉùÃ÷µÄÕýÈ·¡£¶ÔÓÚºóÕߣ¬Í¨³£ÊÇÄãÐèÒª
¸æËß±àÒëÆ÷Í·ÎļþµÄËùÔÚλÖã¨Í·ÎļþÖÐÓ¦¸ÃÖ»ÊÇÉùÃ÷£¬¶ø¶¨ÒåÓ¦¸Ã·ÅÔÚC/C++ÎļþÖУ©£¬
Ö»ÒªËùÓеÄÓï·¨ÕýÈ·£¬±àÒëÆ÷¾Í¿ÉÒÔ±àÒë³öÖмäÄ¿±êÎļþ¡£Ò»°ãÀ´Ëµ£¬Ã¿¸öÔ´Îļþ¶¼Ó¦¸Ã¶Ô
Ó¦ÓÚÒ»¸öÖмäÄ¿±êÎļþ£¨OÎļþ»òÊÇOBJÎļþ£©¡£
Á´½Óʱ£¬Ö÷ÒªÊÇÁ´½Óº¯ÊýºÍÈ«¾Ö±äÁ¿£¬ËùÒÔ£¬ÎÒÃÇ¿ÉÒÔʹÓÃÕâЩÖмäÄ¿±êÎļþ£¨OÎļþ»òÊÇO
BJÎļþ£©À´Á´½ÓÎÒÃǵÄÓ¦ÓóÌÐò¡£Á´½ÓÆ÷²¢²»¹Üº¯ÊýËùÔÚµÄÔ´Îļþ£¬Ö»¹Üº¯ÊýµÄÖмäÄ¿±êÎÄ
¼þ£¨Object File£©£¬ÔÚ´ó¶àÊýʱºò£¬ÓÉÓÚÔ´ÎļþÌ«¶à£¬±àÒëÉú³ÉµÄÖмäÄ¿±êÎļþÌ«¶à£¬¶ø
ÔÚÁ´½ÓʱÐèÒªÃ÷ÏÔµØÖ¸³öÖмäÄ¿±êÎļþÃû£¬Õâ¶ÔÓÚ±àÒëºÜ²»·½±ã£¬ËùÒÔ£¬ÎÒÃÇÒª¸øÖмäÄ¿±ê
Îļþ´ò¸ö°ü£¬ÔÚWindowsÏÂÕâÖÖ°ü½Ð¡°¿âÎļþ¡±£¨Library File)£¬Ò²¾ÍÊÇ .lib Îļþ£¬ÔÚUN
IXÏ£¬ÊÇArchive File£¬Ò²¾ÍÊÇ .a Îļþ¡£
×ܽáһϣ¬Ô´ÎļþÊ×ÏÈ»áÉú³ÉÖмäÄ¿±êÎļþ£¬ÔÙÓÉÖмäÄ¿±êÎļþÉú³ÉÖ´ÐÐÎļþ¡£ÔÚ±àÒëʱ£¬
±àÒëÆ÷Ö»¼ì²â³ÌÐòÓï·¨£¬ºÍº¯Êý¡¢±äÁ¿ÊÇ·ñ±»ÉùÃ÷¡£Èç¹ûº¯Êýδ±»ÉùÃ÷£¬±àÒëÆ÷»á¸ø³öÒ»¸ö
¾¯¸æ£¬µ«¿ÉÒÔÉú³ÉObject File¡£¶øÔÚÁ´½Ó³ÌÐòʱ£¬Á´½ÓÆ÷»áÔÚËùÓеÄObject FileÖÐÕÒÑ°º¯
ÊýµÄʵÏÖ£¬Èç¹ûÕÒ²»µ½£¬Äǵ½¾Í»á±¨Á´½Ó´íÎóÂ루Linker Error£©£¬ÔÚVCÏ£¬ÕâÖÖ´íÎóÒ»°ã
ÊÇ£ºLink 2001´íÎó£¬Òâ˼˵ÊÇ˵£¬Á´½ÓÆ÷δÄÜÕÒµ½º¯ÊýµÄʵÏÖ¡£ÄãÐèÒªÖ¸¶¨º¯ÊýµÄObject
File.
ºÃ£¬ÑÔ¹éÕý´«£¬GNUµÄmakeÓÐÐí¶àµÄÄÚÈÝ£¬ÏÐÑÔÉÙÐ𣬻¹ÊÇÈÃÎÒÃÇ¿ªÊ¼°É¡£

Makefile ½éÉÜ
--------------
makeÃüÁîÖ´ÐÐʱ£¬ÐèÒªÒ»¸ö Makefile Îļþ£¬ÒÔ¸æËßmakeÃüÁîÐèÒªÔõôÑùµÄÈ¥±àÒëºÍÁ´½Ó³Ì
Ðò¡£
Ê×ÏÈ£¬ÎÒÃÇÓÃÒ»¸öʾÀýÀ´ËµÃ÷MakefileµÄÊéд¹æÔò¡£ÒÔ±ã¸ø´ó¼ÒÒ»¸ö¸ÐÐËÈÏʶ¡£Õâ¸öʾÀýÀ´
Ô´ÓÚGNUµÄmakeʹÓÃÊֲᣬÔÚÕâ¸öʾÀýÖУ¬ÎÒÃǵŤ³ÌÓÐ8¸öCÎļþ£¬ºÍ3¸öÍ·Îļþ£¬ÎÒÃÇҪд
Ò»¸öMakefileÀ´¸æËßmakeÃüÁîÈçºÎ±àÒëºÍÁ´½ÓÕ⼸¸öÎļþ¡£ÎÒÃǵĹæÔòÊÇ£º
1£©Èç¹ûÕâ¸ö¹¤³ÌûÓбàÒë¹ý£¬ÄÇôÎÒÃǵÄËùÓÐCÎļþ¶¼Òª±àÒë²¢±»Á´½Ó¡£
2£©Èç¹ûÕâ¸ö¹¤³ÌµÄij¼¸¸öCÎļþ±»Ð޸ģ¬ÄÇôÎÒÃÇÖ»±àÒë±»Ð޸ĵÄCÎļþ£¬²¢Á´½ÓÄ¿±ê
³ÌÐò¡£
3£©Èç¹ûÕâ¸ö¹¤³ÌµÄÍ·Îļþ±»¸Ä±äÁË£¬ÄÇôÎÒÃÇÐèÒª±àÒëÒýÓÃÁËÕ⼸¸öÍ·ÎļþµÄCÎļþ£¬
²¢Á´½ÓÄ¿±ê³ÌÐò¡£
Ö»ÒªÎÒÃǵÄMakefileдµÃ¹»ºÃ£¬ËùÓеÄÕâÒ»ÇУ¬ÎÒÃÇÖ»ÓÃÒ»¸ömakeÃüÁî¾Í¿ÉÒÔÍê³É£¬makeÃü
Áî»á×Ô¶¯ÖÇÄܵظù¾Ýµ±Ç°µÄÎļþÐ޸ĵÄÇé¿öÀ´È·¶¨ÄÄЩÎļþÐèÒªÖرàÒ룬´Ó¶ø×Ô¼º±àÒëËùÐè
ÒªµÄÎļþºÍÁ´½ÓÄ¿±ê³ÌÐò¡£

Ò»¡¢MakefileµÄ¹æÔò
ÔÚ½²ÊöÕâ¸öMakefile֮ǰ£¬»¹ÊÇÈÃÎÒÃÇÏÈÀ´´ÖÂԵؿ´Ò»¿´MakefileµÄ¹æÔò¡£
target ... : prerequisites ...
command
...
...
targetÒ²¾ÍÊÇÒ»¸öÄ¿±êÎļþ£¬¿ÉÒÔÊÇObject File£¬Ò²¿ÉÒÔÊÇÖ´ÐÐÎļþ¡£»¹¿ÉÒÔÊÇÒ»¸ö
±êÇ©£¨Label£©£¬¶ÔÓÚ±êÇ©ÕâÖÖÌØÐÔ£¬ÔÚºóÐøµÄ¡°Î±Ä¿±ê¡±Õ½ÚÖлáÓÐÐðÊö¡£
prerequisites¾ÍÊÇ£¬ÒªÉú³ÉÄǸötargetËùÐèÒªµÄÎļþ»òÊÇÄ¿±ê¡£
commandÒ²¾ÍÊÇmakeÐèÒªÖ´ÐеÄÃüÁî¡££¨ÈÎÒâµÄShellÃüÁ
ÕâÊÇÒ»¸öÎļþµÄÒÀÀµ¹Øϵ£¬Ò²¾ÍÊÇ˵£¬targetÕâÒ»¸ö»ò¶à¸öµÄÄ¿±êÎļþÒÀÀµÓÚprerequisite
sÖеÄÎļþ£¬ÆäÉú³É¹æÔò¶¨ÒåÔÚcommandÖС£Ëµ°×Ò»µã¾ÍÊÇ˵£¬prerequisitesÖÐÈç¹ûÓÐÒ»¸ö
ÒÔÉϵÄÎļþ±ÈtargetÎļþҪеĻ°£¬commandËù¶¨ÒåµÄÃüÁî¾Í»á±»Ö´ÐС£Õâ¾ÍÊÇMakefileµÄ
¹æÔò¡£Ò²¾ÍÊÇMakefileÖÐ×îºËÐĵÄÄÚÈÝ¡£
˵µ½µ×£¬MakefileµÄ¶«Î÷¾ÍÊÇÕâÑùÒ»µã£¬ºÃÏñÎÒµÄÕâƪÎĵµÒ²¸Ã½áÊøÁË¡£ºÇºÇ¡£»¹²»¾¡È»£¬
ÕâÊÇMakefileµÄÖ÷ÏߺͺËÐÄ£¬µ«ÒªÐ´ºÃÒ»¸öMakefile»¹²»¹»£¬ÎÒ»áÒÔºóÃæÒ»µãÒ»µãµØ½áºÏÎÒ
µÄ¹¤×÷¾­Ñé¸øÄãÂýÂýµ½À´¡£ÄÚÈÝ»¹¶à×ÅÄØ¡££º£©

¶þ¡¢Ò»¸öʾÀý
ÕýÈçÇ°ÃæËù˵µÄ£¬Èç¹ûÒ»¸ö¹¤³ÌÓÐ3¸öÍ·Îļþ£¬ºÍ8¸öCÎļþ£¬ÎÒÃÇΪÁËÍê³ÉÇ°ÃæËùÊöµÄÄÇÈý
¸ö¹æÔò£¬ÎÒÃǵÄMakefileÓ¦¸ÃÊÇÏÂÃæµÄÕâ¸öÑù×ӵġ£
edit : main.o kbd.o command.o display.o \
insert.o search.o files.o utils.o
cc -o edit main.o kbd.o command.o display.o \
insert.o search.o files.o utils.o
main.o : main.c defs.h
cc -c main.c
kbd.o : kbd.c defs.h command.h
cc -c kbd.c
command.o : command.c defs.h command.h
cc -c command.c
display.o : display.c defs.h buffer.h
cc -c display.c
insert.o : insert.c defs.h buffer.h
cc -c insert.c
search.o : search.c defs.h buffer.h
cc -c search.c
files.o : files.c defs.h buffer.h command.h
cc -c files.c
utils.o : utils.c defs.h
cc -c utils.c
clean :
rm edit main.o kbd.o command.o display.o \
insert.o search.o files.o utils.o
·´Ð±¸Ü£¨\£©ÊÇ»»ÐзûµÄÒâ˼¡£ÕâÑù±È½Ï±ãÓÚMakefileµÄÒ׶Á¡£ÎÒÃÇ¿ÉÒÔ°ÑÕâ¸öÄÚÈݱ£´æÔÚ
ÎļþΪ¡°Makefile¡±»ò¡°makefile¡±µÄÎļþÖУ¬È»ºóÔÚ¸ÃĿ¼ÏÂÖ±½ÓÊäÈëÃüÁî¡°make¡±¾Í¿É
ÒÔÉú³ÉÖ´ÐÐÎļþedit¡£Èç¹ûҪɾ³ýÖ´ÐÐÎļþºÍËùÓеÄÖмäÄ¿±êÎļþ£¬ÄÇô£¬Ö»Òª¼òµ¥µØÖ´ÐÐ
һϡ°make clean¡±¾Í¿ÉÒÔÁË¡£
ÔÚÕâ¸ömakefileÖУ¬Ä¿±êÎļþ£¨target£©°üº¬£ºÖ´ÐÐÎļþeditºÍÖмäÄ¿±êÎļþ£¨*.o£©£¬ÒÀ
ÀµÎļþ£¨prerequisites£©¾ÍÊÇðºÅºóÃæµÄÄÇЩ .c ÎļþºÍ .hÎļþ¡£Ã¿Ò»¸ö .o Îļþ¶¼ÓÐÒ»
×éÒÀÀµÎļþ£¬¶øÕâЩ .o ÎļþÓÖÊÇÖ´ÐÐÎļþ edit µÄÒÀÀµÎļþ¡£ÒÀÀµ¹ØϵµÄʵÖÊÉϾÍÊÇ˵Ã÷
ÁËÄ¿±êÎļþÊÇÓÉÄÄЩÎļþÉú³ÉµÄ£¬»»ÑÔÖ®£¬Ä¿±êÎļþÊÇÄÄЩÎļþ¸üеġ£
ÔÚ¶¨ÒåºÃÒÀÀµ¹Øϵºó£¬ºóÐøµÄÄÇÒ»Ðж¨ÒåÁËÈçºÎÉú³ÉÄ¿±êÎļþµÄ²Ù×÷ϵͳÃüÁһ¶¨ÒªÒÔÒ»
¸öTab¼ü×÷Ϊ¿ªÍ·¡£¼Çס£¬make²¢²»¹ÜÃüÁîÊÇÔõô¹¤×÷µÄ£¬ËûÖ»¹ÜÖ´ÐÐËù¶¨ÒåµÄÃüÁî¡£make
»á±È½ÏtargetsÎļþºÍprerequisitesÎļþµÄÐÞ¸ÄÈÕÆÚ£¬Èç¹ûprerequisitesÎļþµÄÈÕÆÚÒª±Èt
argetsÎļþµÄÈÕÆÚҪУ¬»òÕßtarget²»´æÔڵĻ°£¬ÄÇô£¬make¾Í»áÖ´ÐкóÐø¶¨ÒåµÄÃüÁî¡£
ÕâÀïҪ˵Ã÷Ò»µãµÄÊÇ£¬clean²»ÊÇÒ»¸öÎļþ£¬ËüÖ»²»¹ýÊÇÒ»¸ö¶¯×÷Ãû×Ö£¬ÓеãÏñCÓïÑÔÖеÄla
bleÒ»Ñù£¬ÆäðºÅºóʲôҲûÓУ¬ÄÇô£¬make¾Í²»»á×Ô¶¯È¥ÕÒÎļþµÄÒÀÀµÐÔ£¬Ò²¾Í²»»á×Ô¶¯
Ö´ÐÐÆäºóËù¶¨ÒåµÄÃüÁî¡£ÒªÖ´ÐÐÆäºóµÄÃüÁ¾ÍÒªÔÚmakeÃüÁîºóÃ÷ÏÔµÃÖ¸³öÕâ¸ölableµÄÃû
×Ö¡£ÕâÑùµÄ·½·¨·Ç³£ÓÐÓã¬ÎÒÃÇ¿ÉÒÔÔÚÒ»¸ömakefileÖж¨Òå²»ÓõıàÒë»òÊǺͱàÒëÎ޹صÄÃü
Á±ÈÈç³ÌÐòµÄ´ò°ü£¬³ÌÐòµÄ±¸·Ý£¬µÈµÈ¡£

¶þ¡¢
Èý¡¢makeÊÇÈçºÎ¹¤×÷µÄ
ÔÚĬÈϵķ½Ê½Ï£¬Ò²¾ÍÊÇÎÒÃÇÖ»ÊäÈëmakeÃüÁî¡£ÄÇô£¬
1¡¢make»áÔÚµ±Ç°Ä¿Â¼ÏÂÕÒÃû×ֽС°Makefile¡±»ò¡°makefile¡±µÄÎļþ¡£
2¡¢Èç¹ûÕÒµ½£¬Ëü»áÕÒÎļþÖеĵÚÒ»¸öÄ¿±êÎļþ£¨target£©£¬ÔÚÉÏÃæµÄÀý×ÓÖУ¬Ëû»áÕÒ
µ½¡°edit¡±Õâ¸öÎļþ£¬²¢°ÑÕâ¸öÎļþ×÷Ϊ×îÖÕµÄÄ¿±êÎļþ¡£
3¡¢Èç¹ûeditÎļþ²»´æÔÚ£¬»òÊÇeditËùÒÀÀµµÄºóÃæµÄ .o ÎļþµÄÎļþÐÞ¸Äʱ¼äÒª±Èedit
Õâ¸öÎļþУ¬ÄÇô£¬Ëû¾Í»áÖ´ÐкóÃæËù¶¨ÒåµÄÃüÁîÀ´Éú³ÉeditÕâ¸öÎļþ¡£
4¡¢Èç¹ûeditËùÒÀÀµµÄ.oÎļþÒ²´æÔÚ£¬ÄÇômake»áÔÚµ±Ç°ÎļþÖÐÕÒÄ¿±êΪ.oÎļþµÄÒÀÀµ
ÐÔ£¬Èç¹ûÕÒµ½ÔòÔÙ¸ù¾ÝÄÇÒ»¸ö¹æÔòÉú³É.oÎļþ¡££¨ÕâÓеãÏñÒ»¸ö¶ÑÕ»µÄ¹ý³Ì£©
5¡¢µ±È»£¬ÄãµÄCÎļþºÍHÎļþÊÇ´æÔÚµÄÀ²£¬ÓÚÊÇmake»áÉú³É .o Îļþ£¬È»ºóÔÙÓà .o ÎÄ
¼þÉúÃümakeµÄÖÕ¼«ÈÎÎñ£¬Ò²¾ÍÊÇÖ´ÐÐÎļþeditÁË¡£
Õâ¾ÍÊÇÕû¸ömakeµÄÒÀÀµÐÔ£¬make»áÒ»²ãÓÖÒ»²ãµØÈ¥ÕÒÎļþµÄÒÀÀµ¹Øϵ£¬Ö±µ½×îÖÕ±àÒë³öµÚÒ»
¸öÄ¿±êÎļþ¡£ÔÚÕÒÑ°µÄ¹ý³ÌÖУ¬Èç¹û³öÏÖ´íÎ󣬱ÈÈç×îºó±»ÒÀÀµµÄÎļþÕÒ²»µ½£¬ÄÇômake¾Í
»áÖ±½ÓÍ˳ö£¬²¢±¨´í£¬¶ø¶ÔÓÚËù¶¨ÒåµÄÃüÁîµÄ´íÎ󣬻òÊDZàÒë²»³É¹¦£¬make¸ù±¾²»Àí¡£mak
eÖ»¹ÜÎļþµÄÒÀÀµÐÔ£¬¼´£¬Èç¹ûÔÚÎÒÕÒÁËÒÀÀµ¹Øϵ֮ºó£¬Ã°ºÅºóÃæµÄÎļþ»¹ÊDz»ÔÚ£¬ÄÇô¶Ô
²»Æð£¬ÎҾͲ»¹¤×÷À²¡£
ͨ¹ýÉÏÊö·ÖÎö£¬ÎÒÃÇÖªµÀ£¬ÏñcleanÕâÖÖ£¬Ã»Óб»µÚÒ»¸öÄ¿±êÎļþÖ±½Ó»ò¼ä½Ó¹ØÁª£¬ÄÇôËü
ºóÃæËù¶¨ÒåµÄÃüÁ²»»á±»×Ô¶¯Ö´ÐУ¬²»¹ý£¬ÎÒÃÇ¿ÉÒÔÏÔʾҪmakeÖ´ÐС£¼´ÃüÁî----¡°make
clean¡±£¬ÒÔ´ËÀ´Çå³ýËùÓеÄÄ¿±êÎļþ£¬ÒÔ±ãÖرàÒë¡£
ÓÚÊÇÔÚÎÒÃDZà³ÌÖУ¬Èç¹ûÕâ¸ö¹¤³ÌÒѱ»±àÒë¹ýÁË£¬µ±ÎÒÃÇÐÞ¸ÄÁËÆäÖÐÒ»¸öÔ´Îļþ£¬±ÈÈçfile
.c£¬ÄÇô¸ù¾ÝÎÒÃǵÄÒÀÀµÐÔ£¬ÎÒÃǵÄÄ¿±êfile.o»á±»ÖرàÒ루Ҳ¾ÍÊÇÔÚÕâ¸öÒÀÐÔ¹ØϵºóÃæËù
¶¨ÒåµÄÃüÁ£¬ÓÚÊÇfile.oµÄÎļþÒ²ÊÇ×îеÄÀ²£¬ÓÚÊÇfile.oµÄÎļþÐÞ¸Äʱ¼äÒª±ÈeditÒªÐÂ
£¬ËùÒÔeditÒ²»á±»ÖØÐÂÁ´½ÓÁË£¨Ïê¼ûeditÄ¿±êÎļþºó¶¨ÒåµÄÃüÁ¡£
¶øÈç¹ûÎÒÃǸıäÁË¡°command.h¡±£¬ÄÇô£¬kdb.o¡¢command.oºÍfiles.o¶¼»á±»ÖرàÒ룬²¢ÇÒ
£¬edit»á±»ÖØÁ´½Ó¡£

ËÄ¡¢makefileÖÐʹÓñäÁ¿
ÔÚÉÏÃæµÄÀý×ÓÖУ¬ÏÈÈÃÎÒÃÇ¿´¿´editµÄ¹æÔò£º
edit : main.o kbd.o command.o display.o \
insert.o search.o files.o utils.o
cc -o edit main.o kbd.o command.o display.o \
insert.o search.o files.o utils.o
ÎÒÃÇ¿ÉÒÔ¿´µ½[.o]ÎļþµÄ×Ö·û´®±»Öظ´ÁËÁ½´Î£¬Èç¹ûÎÒÃǵŤ³ÌÐèÒª¼ÓÈëÒ»¸öеÄ[.o]Îļþ
£¬ÄÇôÎÒÃÇÐèÒªÔÚÁ½¸öµØ·½¼Ó£¨Ó¦¸ÃÊÇÈý¸öµØ·½£¬»¹ÓÐÒ»¸öµØ·½ÔÚcleanÖУ©¡£µ±È»£¬ÎÒÃÇ
µÄmakefile²¢²»¸´ÔÓ£¬ËùÒÔÔÚÁ½¸öµØ·½¼ÓÒ²²»ÀÛ£¬µ«Èç¹ûmakefile±äµÃ¸´ÔÓ£¬ÄÇôÎÒÃǾÍÓÐ
¿ÉÄÜ»áÍüµôÒ»¸öÐèÒª¼ÓÈëµÄµØ·½£¬¶øµ¼Ö±àÒëʧ°Ü¡£ËùÒÔ£¬ÎªÁËmakefileµÄÒ×ά»¤£¬ÔÚmake
fileÖÐÎÒÃÇ¿ÉÒÔʹÓñäÁ¿¡£makefileµÄ±äÁ¿Ò²¾ÍÊÇÒ»¸ö×Ö·û´®£¬Àí½â³ÉCÓïÑÔÖеĺê¿ÉÄÜ»á
¸üºÃ¡£
±ÈÈ磬ÎÒÃÇÉùÃ÷Ò»¸ö±äÁ¿£¬½Ðobjects, OBJECTS, objs, OBJS, obj, »òÊÇ OBJ£¬·´Õý²»¹Ü
ʲôÀ²£¬Ö»ÒªÄܹ»±íʾobjÎļþ¾ÍÐÐÁË¡£ÎÒÃÇÔÚmakefileÒ»¿ªÊ¼¾ÍÕâÑù¶¨Ò壺
objects = main.o kbd.o command.o display.o \
insert.o search.o files.o utils.o
ÓÚÊÇ£¬ÎÒÃǾͿÉÒԺܷ½±ãµØÔÚÎÒÃǵÄmakefileÖÐÒÔ¡°$(objects)¡±µÄ·½Ê½À´Ê¹ÓÃÕâ¸ö±äÁ¿ÁË
£¬ÓÚÊÇÎÒÃǵĸÄÁ¼°æmakefile¾Í±ä³ÉÏÂÃæÕâ¸öÑù×Ó£º
objects = main.o kbd.o command.o display.o \
insert.o search.o files.o utils.o
edit : $(objects)
cc -o edit $(objects)
main.o : main.c defs.h
cc -c main.c
kbd.o : kbd.c defs.h command.h
cc -c kbd.c
command.o : command.c defs.h command.h
cc -c command.c
display.o : display.c defs.h buffer.h
cc -c display.c
insert.o : insert.c defs.h buffer.h
cc -c insert.c
search.o : search.c defs.h buffer.h
cc -c search.c
files.o : files.c defs.h buffer.h command.h
cc -c files.c
utils.o : utils.c defs.h
cc -c utils.c
clean :
rm edit $(objects)

ÓÚÊÇÈç¹ûÓÐÐ嵀 .o Îļþ¼ÓÈ룬ÎÒÃÇÖ»Ðè¼òµ¥µØÐÞ¸ÄһϠobjects ±äÁ¿¾Í¿ÉÒÔÁË¡£
¹ØÓÚ±äÁ¿¸ü¶àµÄ»°Ì⣬ÎÒ»áÔÚºóÐø¸øÄãÒ»Ò»µÀÀ´¡£

Îå¡¢ÈÃmake×Ô¶¯ÍƵ¼
GNUµÄmakeºÜÇ¿´ó£¬Ëü¿ÉÒÔ×Ô¶¯ÍƵ¼ÎļþÒÔ¼°ÎļþÒÀÀµ¹ØϵºóÃæµÄÃüÁÓÚÊÇÎÒÃǾÍû±ØÒª
È¥ÔÚÿһ¸ö[.o]Îļþºó¶¼Ð´ÉÏÀàËƵÄÃüÁÒòΪ£¬ÎÒÃǵÄmake»á×Ô¶¯Ê¶±ð£¬²¢×Ô¼ºÍƵ¼ÃüÁî
¡£
Ö»Òªmake¿´µ½Ò»¸ö[.o]Îļþ£¬Ëü¾Í»á×Ô¶¯µÄ°Ñ[.c]Îļþ¼ÓÔÚÒÀÀµ¹ØϵÖУ¬Èç¹ûmakeÕÒµ½Ò»¸ö
whatever.o£¬ÄÇôwhatever.c£¬¾Í»áÊÇwhatever.oµÄÒÀÀµÎļþ¡£²¢ÇÒ cc -c whatever.c Ò²
»á±»ÍƵ¼³öÀ´£¬ÓÚÊÇ£¬ÎÒÃǵÄmakefileÔÙÒ²²»ÓÃдµÃÕâô¸´ÔÓ¡£ÎÒÃǵÄÊÇеÄmakefileÓÖ³ö
¯ÁË¡£

objects = main.o kbd.o command.o display.o \
insert.o search.o files.o utils.o
edit : $(objects)
cc -o edit $(objects)
main.o : defs.h
kbd.o : defs.h command.h
command.o : defs.h command.h
display.o : defs.h buffer.h
insert.o : defs.h buffer.h
search.o : defs.h buffer.h
files.o : defs.h buffer.h command.h
utils.o : defs.h
.PHONY : clean
clean :
rm edit $(objects)
ÕâÖÖ·½·¨£¬Ò²¾ÍÊÇmakeµÄ¡°Òþ»Þ¹æÔò¡±¡£ÉÏÃæÎļþÄÚÈÝÖУ¬¡°.PHONY¡±±íʾ£¬cleanÊǸöα
Ä¿±êÎļþ¡£
¹ØÓÚ¸üΪÏêϸµÄ¡°Òþ»Þ¹æÔò¡±ºÍ¡°Î±Ä¿±êÎļþ¡±£¬ÎÒ»áÔÚºóÐø¸øÄãÒ»Ò»µÀÀ´¡£

Áù¡¢ÁíÀà·ç¸ñµÄmakefile
¼´È»ÎÒÃǵÄmake¿ÉÒÔ×Ô¶¯ÍƵ¼ÃüÁÄÇôÎÒ¿´µ½ÄǶÑ[.o]ºÍ[.h]µÄÒÀÀµ¾ÍÓе㲻ˬ£¬ÄÇô¶à
µÄÖظ´µÄ[.h]£¬Äܲ»ÄÜ°ÑÆäÊÕ£ÆðÀ´£¬ºÃ°É£¬Ã»ÓÐÎÊÌ⣬Õâ¸ö¶ÔÓÚmakeÀ´ËµºÜÈÝÒ×£¬Ë­½ÐËü
ÌṩÁË×Ô¶¯ÍƵ¼ÃüÁîºÍÎļþµÄ¹¦ÄÜÄØ£¿À´¿´¿´×îзç¸ñµÄmakefile°É¡£
objects = main.o kbd.o command.o display.o \
insert.o search.o files.o utils.o
edit : $(objects)
cc -o edit $(objects)
$(objects) : defs.h
kbd.o command.o files.o : command.h
display.o insert.o search.o files.o : buffer.h
.PHONY : clean
clean :
rm edit $(objects)
ÕâÖÖ·ç¸ñ£¬ÈÃÎÒÃǵÄmakefile±äµÃºÜ¼òµ¥£¬µ«ÎÒÃǵÄÎļþÒÀÀµ¹Øϵ¾ÍÏÔµÃÓеãÁèÂÒÁË¡£ÓãºÍ
ÐÜÕƲ»¿É¼æµÃ¡£»¹¿´ÄãµÄϲºÃÁË¡£ÎÒÊDz»Ï²»¶ÕâÖÖ·ç¸ñµÄ£¬Ò»ÊÇÎļþµÄÒÀÀµ¹Øϵ¿´²»Çå³þ£¬
¶þÊÇÈç¹ûÎļþÒ»¶à£¬Òª¼ÓÈ뼸¸öеÄ.oÎļþ£¬ÄǾÍÀí²»Çå³þÁË¡£

Æß¡¢Çå¿ÕÄ¿±êÎļþµÄ¹æÔò
ÿ¸öMakefileÖж¼Ó¦¸Ãдһ¸öÇå¿ÕÄ¿±êÎļþ£¨.oºÍÖ´ÐÐÎļþ£©µÄ¹æÔò£¬Õâ²»½ö±ãÓÚÖرàÒ룬
Ò²ºÜÀûÓÚ±£³ÖÎļþµÄÇå½à¡£ÕâÊÇÒ»¸ö¡°ÐÞÑø¡±£¨ºÇºÇ£¬»¹¼ÇµÃÎҵġ¶±à³ÌÐÞÑø¡·Â𣩡£Ò»°ã
µÄ·ç¸ñ¶¼ÊÇ£º
clean:
rm edit $(objects)
¸üΪÎȽ¡µÄ×ö·¨ÊÇ£º
.PHONY : clean
clean :
-rm edit $(objects)
Ç°Ãæ˵¹ý£¬.PHONYÒâ˼±íʾcleanÊÇÒ»¸ö¡°Î±Ä¿±ê¡±£¬¡£¶øÔÚrmÃüÁîÇ°Ãæ¼ÓÁËÒ»¸öС¼õºÅµÄ
Òâ˼¾ÍÊÇ£¬Ò²ÐíijЩÎļþ³öÏÖÎÊÌ⣬µ«²»Òª¹Ü£¬¼ÌÐø×öºóÃæµÄÊ¡£µ±È»£¬cleanµÄ¹æÔò²»Òª
·ÅÔÚÎļþµÄ¿ªÍ·£¬²»È»£¬Õâ¾Í»á±ä³ÉmakeµÄĬÈÏÄ¿±ê£¬ÏàÐÅË­Ò²²»Ô¸ÒâÕâÑù¡£²»³ÉÎĵĹæ¾Ø
ÊÇ----¡°clean´ÓÀ´¶¼ÊÇ·ÅÔÚÎļþµÄ×îºó¡±¡£

ÉÏÃæ¾ÍÊÇÒ»¸ömakefileµÄ¸Åò£¬Ò²ÊÇmakefileµÄ»ù´¡£¬ÏÂÃ滹ÓкܶàmakefileµÄÏà¹Øϸ½Ú£¬
×¼±¸ºÃÁËÂð£¿×¼±¸ºÃÁ˾ÍÀ´¡£


Èý¡¢Makefile ×ÜÊö
--------------
Ò»¡¢MakefileÀïÓÐʲô£¿
MakefileÀïÖ÷Òª°üº¬ÁËÎå¸ö¶«Î÷£ºÏÔʽ¹æÔò¡¢Òþ»Þ¹æÔò¡¢±äÁ¿¶¨Òå¡¢ÎļþָʾºÍ×¢ÊÍ¡£
1¡¢ÏÔʽ¹æÔò¡£ÏÔʽ¹æÔò˵Ã÷ÁË£¬ÈçºÎÉú³ÉÒ»¸ö»ò¶àµÄµÄÄ¿±êÎļþ¡£ÕâÊÇÓÉMakefileµÄÊéд
ÕßÃ÷ÏÔÖ¸³ö£¬ÒªÉú³ÉµÄÎļþ£¬ÎļþµÄÒÀÀµÎļþ£¬Éú³ÉµÄÃüÁî¡£
2¡¢Òþ»Þ¹æÔò¡£ÓÉÓÚÎÒÃǵÄmakeÓÐ×Ô¶¯ÍƵ¼µÄ¹¦ÄÜ£¬ËùÒÔÒþ»ÞµÄ¹æÔò¿ÉÒÔÈÃÎÒÃDZȽϴֲڵØ
¼òÂÔµØÊéдMakefile£¬ÕâÊÇÓÉmakeËùÖ§³ÖµÄ¡£
3¡¢±äÁ¿µÄ¶¨Òå¡£ÔÚMakefileÖÐÎÒÃÇÒª¶¨ÒåһϵÁеıäÁ¿£¬±äÁ¿Ò»°ã¶¼ÊÇ×Ö·û´®£¬Õâ¸öÓеã
ÄãCÓïÑÔÖеĺ꣬µ±Makefile±»Ö´ÐÐʱ£¬ÆäÖеıäÁ¿¶¼»á±»À©Õ¹µ½ÏàÓ¦µÄÒýÓÃλÖÃÉÏ¡£
4¡¢Îļþָʾ¡£Æä°üÀ¨ÁËÈý¸ö²¿·Ö£¬Ò»¸öÊÇÔÚÒ»¸öMakefileÖÐÒýÓÃÁíÒ»¸öMakefile£¬¾ÍÏñCÓï
ÑÔÖеÄincludeÒ»Ñù£»ÁíÒ»¸öÊÇÖ¸¸ù¾ÝijЩÇé¿öÖ¸¶¨MakefileÖеÄÓÐЧ²¿·Ö£¬¾ÍÏñCÓïÑÔÖеÄ
Ô¤±àÒë#ifÒ»Ñù£»»¹ÓоÍÊǶ¨ÒåÒ»¸ö¶àÐеÄÃüÁî¡£ÓйØÕâÒ»²¿·ÖµÄÄÚÈÝ£¬ÎÒ»áÔÚºóÐøµÄ²¿·Ö
Öн²Êö¡£
5¡¢×¢ÊÍ¡£MakefileÖÐÖ»ÓÐÐÐ×¢ÊÍ£¬ºÍUNIXµÄShell½Å±¾Ò»Ñù£¬Æä×¢ÊÍÊÇÓá°#¡±×Ö·û£¬Õâ¸ö
¾ÍÏñC/C++Öеġ°//¡±Ò»Ñù¡£Èç¹ûÄãÒªÔÚÄãµÄMakefileÖÐʹÓá°#¡±×Ö·û£¬¿ÉÒÔÓ÷´Ð±¿ò½øÐÐ
תÒ壬È磺¡°\#¡±¡£
×îºó£¬»¹ÖµµÃÒ»ÌáµÄÊÇ£¬ÔÚMakefileÖеÄÃüÁ±ØÐëÒªÒÔ[Tab]¼ü¿ªÊ¼¡£

¶þ¡¢MakefileµÄÎļþÃû
ĬÈϵÄÇé¿öÏ£¬makeÃüÁî»áÔÚµ±Ç°Ä¿Â¼Ï°´Ë³ÐòÕÒÑ°ÎļþÃûΪ¡°GNUmakefile¡±¡¢¡°makefil
e¡±¡¢¡°Makefile¡±µÄÎļþ£¬ÕÒµ½Á˽âÊÍÕâ¸öÎļþ¡£ÔÚÕâÈý¸öÎļþÃûÖУ¬×îºÃʹÓá°Makefil
e¡±Õâ¸öÎļþÃû£¬ÒòΪ£¬Õâ¸öÎļþÃûµÚÒ»¸ö×Ö·ûΪ´óд£¬ÕâÑùÓÐÒ»ÖÖÏÔÄ¿µÄ¸Ð¾õ¡£×îºÃ²»Òª
Óá°GNUmakefile¡±£¬Õâ¸öÎļþÊÇGNUµÄmakeʶ±ðµÄ¡£ÓÐÁíÍâһЩmakeÖ»¶ÔȫСдµÄ¡°makefi
le¡±ÎļþÃûÃô¸Ð£¬µ«ÊÇ»ù±¾ÉÏÀ´Ëµ£¬´ó¶àÊýµÄmake¶¼Ö§³Ö¡°makefile¡±ºÍ¡°Makefile¡±ÕâÁ½
ÖÖĬÈÏÎļþÃû¡£
µ±È»£¬Äã¿ÉÒÔʹÓñðµÄÎļþÃûÀ´ÊéдMakefile£¬±ÈÈ磺¡°Make.Linux¡±£¬¡°Make.Solaris¡±
£¬¡°Make.AIX¡±µÈ£¬Èç¹ûÒªÖ¸¶¨Ìض¨µÄMakefile£¬Äã¿ÉÒÔʹÓÃmakeµÄ¡°-f¡±ºÍ¡°--file¡±²Î
Êý£¬È磺make -f Make.Linux»òmake --file Make.AIX¡£

Èý¡¢ÒýÓÃÆäËüµÄMakefile
ÔÚMakefileʹÓÃinclude¹Ø¼ü×Ö¿ÉÒ԰ѱðµÄMakefile°üº¬½øÀ´£¬ÕâºÜÏñCÓïÑÔµÄ#include£¬±»
°üº¬µÄÎļþ»áÔ­Ä£Ô­ÑùµÄ·ÅÔÚµ±Ç°ÎļþµÄ°üº¬Î»Öá£includeµÄÓï·¨ÊÇ£º
include
filename¿ÉÒÔÊǵ±Ç°²Ù×÷ϵͳShellµÄÎļþģʽ£¨¿ÉÒÔ±£º¬Â·¾¶ºÍͨÅä·û£©
ÔÚincludeÇ°Ãæ¿ÉÒÔÓÐһЩ¿Õ×Ö·û£¬µ«ÊǾø²»ÄÜÊÇ[Tab]¼ü¿ªÊ¼¡£includeºÍ¿ÉÒÔ
ÓÃÒ»¸ö»ò¶à¸ö¿Õ¸ñ¸ô¿ª¡£¾Ù¸öÀý×Ó£¬ÄãÓÐÕâÑù¼¸¸öMakefile£ºa.mk¡¢b.mk¡¢c.mk£¬»¹ÓÐÒ»¸ö
Îļþ½Ðfoo.make£¬ÒÔ¼°Ò»¸ö±äÁ¿$(bar)£¬Æä°üº¬ÁËe.mkºÍf.mk£¬ÄÇô£¬ÏÂÃæµÄÓï¾ä£º
include foo.make *.mk $(bar)
µÈ¼ÛÓÚ£º
include foo.make a.mk b.mk c.mk e.mk f.mk
makeÃüÁʼʱ£¬»á°ÑÕÒÑ°includeËùÖ¸³öµÄÆäËüMakefile£¬²¢°ÑÆäÄÚÈÝ°²ÖÃÔÚµ±Ç°µÄλÖÃ
¡£¾ÍºÃÏñC/C++µÄ#includeÖ¸ÁîÒ»Ñù¡£Èç¹ûÎļþ¶¼Ã»ÓÐÖ¸¶¨¾ø¶Ô·¾¶»òÊÇÏà¶Ô·¾¶µÄ»°£¬mak
e»áÔÚµ±Ç°Ä¿Â¼ÏÂÊ×ÏÈÑ°ÕÒ£¬Èç¹ûµ±Ç°Ä¿Â¼ÏÂûÓÐÕÒµ½£¬ÄÇô£¬make»¹»áÔÚÏÂÃæµÄ¼¸¸öĿ¼
ÏÂÕÒ£º
1¡¢Èç¹ûmakeÖ´ÐÐʱ£¬ÓС°-I¡±»ò¡°--include-dir¡±²ÎÊý£¬ÄÇômake¾Í»áÔÚÕâ¸ö²ÎÊýËù
Ö¸¶¨µÄĿ¼ÏÂȥѰÕÒ¡£
2¡¢Èç¹ûĿ¼/include£¨Ò»°ãÊÇ£º/usr/local/bin»ò/usr/include£©´æÔڵĻ°
£¬makeÒ²»áÈ¥ÕÒ¡£
Èç¹ûÓÐÎļþûÓÐÕÒµ½µÄ»°£¬make»áÉú³ÉÒ»Ìõ¾¯¸æÐÅÏ¢£¬µ«²»»áÂíÉϳöÏÖÖÂÃü´íÎó¡£Ëü»á¼ÌÐø
ÔØÈëÆäËüµÄÎļþ£¬Ò»µ©Íê³ÉmakefileµÄ¶ÁÈ¡£¬make»áÔÙÖØÊÔÕâЩûÓÐÕÒµ½£¬»òÊDz»ÄܶÁÈ¡µÄ
Îļþ£¬Èç¹û»¹ÊDz»ÐУ¬make²Å»á³öÏÖÒ»ÌõÖÂÃüÐÅÏ¢¡£Èç¹ûÄãÏëÈÃmake²»ÀíÄÇЩÎÞ·¨¶ÁÈ¡µÄÎÄ
¼þ£¬¶ø¼ÌÐøÖ´ÐУ¬Äã¿ÉÒÔÔÚincludeÇ°¼ÓÒ»¸ö¼õºÅ¡°-¡±¡£È磺
-include
Æä±íʾ£¬ÎÞÂÛinclude¹ý³ÌÖгöÏÖʲô´íÎ󣬶¼²»Òª±¨´í¼ÌÐøÖ´ÐС£ºÍÆäËü°æ±¾make¼æ
ÈݵÄÏà¹ØÃüÁîÊÇsinclude£¬Æä×÷ÓúÍÕâÒ»¸öÊÇÒ»ÑùµÄ¡£

ËÄ¡¢»·¾³±äÁ¿ MAKEFILES
Èç¹ûÄãµÄµ±Ç°»·¾³Öж¨ÒåÁË»·¾³±äÁ¿MAKEFILES£¬ÄÇô£¬make»á°ÑÕâ¸ö±äÁ¿ÖеÄÖµ×öÒ»¸öÀà
ËÆÓÚincludeµÄ¶¯×÷¡£Õâ¸ö±äÁ¿ÖеÄÖµÊÇÆäËüµÄMakefile£¬Óÿոñ·Ö¸ô¡£Ö»ÊÇ£¬ËüºÍinclud
e²»Í¬µÄÊÇ£¬´ÓÕâ¸ö»·¾³±äÖÐÒýÈëµÄMakefileµÄ¡°Ä¿±ê¡±²»»áÆð×÷Óã¬Èç¹û»·¾³±äÁ¿Öж¨Òå
µÄÎļþ·¢ÏÖ´íÎó£¬makeÒ²»á²»Àí¡£
µ«ÊÇÔÚÕâÀïÎÒ»¹Êǽ¨Ò鲻ҪʹÓÃÕâ¸ö»·¾³±äÁ¿£¬ÒòΪֻҪÕâ¸ö±äÁ¿Ò»±»¶¨Ò壬ÄÇôµ±ÄãʹÓÃ
makeʱ£¬ËùÓеÄMakefile¶¼»áÊܵ½ËüµÄÓ°Ï죬Õâ¾ø²»ÊÇÄãÏë¿´µ½µÄ¡£ÔÚÕâÀïÌáÕâ¸öÊ£¬Ö»ÊÇ
ΪÁ˸æËß´ó¼Ò£¬Ò²ÐíÓÐʱºòÄãµÄMakefile³öÏÖÁ˹ÖÊ£¬ÄÇôÄã¿ÉÒÔ¿´¿´µ±Ç°»·¾³ÖÐÓÐûÓж¨
ÒåÕâ¸ö±äÁ¿¡£

Îå¡¢makeµÄ¹¤×÷·½Ê½
GNUµÄmake¹¤×÷ʱµÄÖ´Ðв½ÖèÈëÏ£º£¨ÏëÀ´ÆäËüµÄmakeÒ²ÊÇÀàËÆ£©
1¡¢¶ÁÈëËùÓеÄMakefile¡£
2¡¢¶ÁÈë±»includeµÄÆäËüMakefile¡£
3¡¢³õʼ»¯ÎļþÖеıäÁ¿¡£
4¡¢ÍƵ¼Òþ»Þ¹æÔò£¬²¢·ÖÎöËùÓйæÔò¡£
5¡¢ÎªËùÓеÄÄ¿±êÎļþ´´½¨ÒÀÀµ¹ØϵÁ´¡£
6¡¢¸ù¾ÝÒÀÀµ¹Øϵ£¬¾ö¶¨ÄÄЩĿ±êÒªÖØÐÂÉú³É¡£
7¡¢Ö´ÐÐÉú³ÉÃüÁî¡£
1-5²½ÎªµÚÒ»¸ö½×¶Î£¬6-7ΪµÚ¶þ¸ö½×¶Î¡£µÚÒ»¸ö½×¶ÎÖУ¬Èç¹û¶¨ÒåµÄ±äÁ¿±»Ê¹ÓÃÁË£¬ÄÇô£¬
make»á°ÑÆäÕ¹¿ªÔÚʹÓõÄλÖᣵ«make²¢²»»áÍêÈ«ÂíÉÏÕ¹¿ª£¬makeʹÓõÄÊÇÍÏÑÓÕ½Êõ£¬Èç¹û
±äÁ¿³öÏÖÔÚÒÀÀµ¹ØϵµÄ¹æÔòÖУ¬ÄÇô½öµ±ÕâÌõÒÀÀµ±»¾ö¶¨ÒªÊ¹ÓÃÁË£¬±äÁ¿²Å»áÔÚÆäÄÚ²¿Õ¹¿ª
¡£
µ±È»£¬Õâ¸ö¹¤×÷·½Ê½Äã²»Ò»¶¨ÒªÇå³þ£¬µ«ÊÇÖªµÀÕâ¸ö·½Ê½ÄãÒ²»á¶Ômake¸üΪÊìϤ¡£ÓÐÁËÕâ¸ö
»ù´¡£¬ºóÐø²¿·ÖÒ²¾ÍÈÝÒ׿´¶®ÁË¡£



ËÄ¡¢Êéд¹æÔò
--------
¹æÔò°üº¬Á½¸ö²¿·Ö£¬Ò»¸öÊÇÒÀÀµ¹Øϵ£¬Ò»¸öÊÇÉú³ÉÄ¿±êµÄ·½·¨¡£
ÔÚMakefileÖУ¬¹æÔòµÄ˳ÐòÊǺÜÖØÒªµÄ£¬ÒòΪ£¬MakefileÖÐÖ»Ó¦¸ÃÓÐÒ»¸ö×îÖÕÄ¿±ê£¬ÆäËüµÄ
Ä¿±ê¶¼ÊDZ»Õâ¸öÄ¿±êËùÁ¬´ø³öÀ´µÄ£¬ËùÒÔÒ»¶¨ÒªÈÃmakeÖªµÀÄãµÄ×îÖÕÄ¿±êÊÇʲô¡£Ò»°ãÀ´Ëµ
£¬¶¨ÒåÔÚMakefileÖеÄÄ¿±ê¿ÉÄÜ»áÓкܶ࣬µ«ÊǵÚÒ»Ìõ¹æÔòÖеÄÄ¿±ê½«±»È·Á¢Îª×îÖÕµÄÄ¿±ê
¡£Èç¹ûµÚÒ»Ìõ¹æÔòÖеÄÄ¿±êÓкܶà¸ö£¬ÄÇô£¬µÚÒ»¸öÄ¿±ê»á³ÉΪ×îÖÕµÄÄ¿±ê¡£makeËùÍê³ÉµÄ
Ò²¾ÍÊÇÕâ¸öÄ¿±ê¡£
ºÃÁË£¬»¹ÊÇÈÃÎÒÃÇÀ´¿´Ò»¿´ÈçºÎÊéд¹æÔò¡£

Ò»¡¢¹æÔò¾ÙÀý
foo.o : foo.c defs.h # fooÄ£¿é
cc -c -g foo.c
¿´µ½Õâ¸öÀý×Ó£¬¸÷λӦ¸Ã²»ÊǺÜÄ°ÉúÁË£¬Ç°ÃæÒ²ÒÑ˵¹ý£¬foo.oÊÇÎÒÃǵÄÄ¿±ê£¬foo.cºÍdefs
.hÊÇÄ¿±êËùÒÀÀµµÄÔ´Îļþ£¬¶øÖ»ÓÐÒ»¸öÃüÁî¡°cc -c -g foo.c¡±£¨ÒÔTab¼ü¿ªÍ·£©¡£Õâ¸ö¹æ
Ôò¸æËßÎÒÃÇÁ½¼þÊ£º
1¡¢ÎļþµÄÒÀÀµ¹Øϵ£¬foo.oÒÀÀµÓÚfoo.cºÍdefs.hµÄÎļþ£¬Èç¹ûfoo.cºÍdefs.hµÄÎļþÈÕ
ÆÚÒª±Èfoo.oÎļþÈÕÆÚҪУ¬»òÊÇfoo.o²»´æÔÚ£¬ÄÇôÒÀÀµ¹Øϵ·¢Éú¡£
2¡¢Èç¹ûÉú³É£¨»ò¸üУ©foo.oÎļþ¡£Ò²¾ÍÊÇÄǸöccÃüÁÆä˵Ã÷ÁË£¬ÈçºÎÉú³Éfoo.oÕâ
¸öÎļþ¡££¨µ±È»foo.cÎļþincludeÁËdefs.hÎļþ£©

¶þ¡¢¹æÔòµÄÓï·¨
targets : prerequisites
command
...
»òÊÇÕâÑù£º
targets : prerequisites ; command
command
...
targetsÊÇÎļþÃû£¬ÒÔ¿Õ¸ñ·Ö¿ª£¬¿ÉÒÔʹÓÃͨÅä·û¡£Ò»°ãÀ´Ëµ£¬ÎÒÃǵÄÄ¿±ê»ù±¾ÉÏÊÇÒ»¸öÎÄ
¼þ£¬µ«Ò²ÓпÉÄÜÊǶà¸öÎļþ¡£
commandÊÇÃüÁîÐУ¬Èç¹ûÆä²»Óë¡°target:prerequisites¡±ÔÚÒ»ÐУ¬ÄÇô£¬±ØÐëÒÔ[Tab¼ü]¿ª
Í·£¬Èç¹ûºÍprerequisitesÔÚÒ»ÐУ¬ÄÇô¿ÉÒÔÓ÷ֺÅ×öΪ·Ö¸ô¡££¨¼ûÉÏ£©
prerequisitesÒ²¾ÍÊÇÄ¿±êËùÒÀÀµµÄÎļþ£¨»òÒÀÀµÄ¿±ê£©¡£Èç¹ûÆäÖеÄij¸öÎļþÒª±ÈÄ¿±êÎÄ
¼þҪУ¬ÄÇô£¬Ä¿±ê¾Í±»ÈÏΪÊÇ¡°¹ýʱµÄ¡±£¬±»ÈÏΪÊÇÐèÒªÖØÉú³ÉµÄ¡£Õâ¸öÔÚÇ°ÃæÒѾ­½²¹ý
ÁË¡£
Èç¹ûÃüÁîÌ«³¤£¬Äã¿ÉÒÔʹÓ÷´Ð±¿ò£¨¡®\¡¯£©×÷Ϊ»»Ðзû¡£make¶ÔÒ»ÐÐÉÏÓжàÉÙ¸ö×Ö·ûûÓÐ
ÏÞÖÆ¡£¹æÔò¸æËßmakeÁ½¼þÊ£¬ÎļþµÄÒÀÀµ¹ØϵºÍÈçºÎ³É³ÉÄ¿±êÎļþ¡£
Ò»°ãÀ´Ëµ£¬make»áÒÔUNIXµÄ±ê×¼Shell£¬Ò²¾ÍÊÇ/bin/shÀ´Ö´ÐÐÃüÁî¡£

Èý¡¢ÔÚ¹æÔòÖÐʹÓÃͨÅä·û
Èç¹ûÎÒÃÇÏ붨ÒåһϵÁбȽÏÀàËƵÄÎļþ£¬ÎÒÃǺÜ×ÔÈ»µØ¾ÍÏëÆðʹÓÃͨÅä·û¡£makeÖ§³ÖÈý¸÷ͨ
Åä·û£º¡°*¡±£¬¡°?¡±ºÍ¡°[...]¡±¡£ÕâÊǺÍUnixµÄB-ShellÊÇÏàͬµÄ¡£
²¨À˺ţ¨¡°~¡±£©×Ö·ûÔÚÎļþÃûÖÐÒ²ÓбȽÏÌØÊâµÄÓÃ;¡£Èç¹ûÊÇ¡°~/test¡±£¬Õâ¾Í±íʾµ±Ç°
Óû§µÄ$HOMEĿ¼ÏµÄtestĿ¼¡£¶ø¡°~hchen/test¡±Ôò±íʾÓû§hchenµÄËÞÖ÷Ŀ¼ÏµÄtest
Ŀ¼¡££¨ÕâЩ¶¼ÊÇUnixϵÄС֪ʶÁË£¬makeÒ²Ö§³Ö£©¶øÔÚWindows»òÊÇMS-DOSÏ£¬Óû§Ã»ÓÐ
ËÞÖ÷Ŀ¼£¬ÄÇô²¨À˺ÅËùÖ¸µÄĿ¼Ôò¸ù¾Ý»·¾³±äÁ¿¡°HOME¡±¶ø¶¨¡£
ͨÅä·û´úÌæÁËÄãһϵÁеÄÎļþ£¬Èç¡°*.c¡±±íʾËùÒÔºó׺ΪcµÄÎļþ¡£Ò»¸öÐèÒªÎÒÃÇ×¢ÒâµÄÊÇ
£¬Èç¹ûÎÒÃǵÄÎļþÃûÖÐÓÐͨÅä·û£¬È磺¡°*¡±£¬ÄÇô¿ÉÒÔÓÃתÒå×Ö·û¡°\¡±£¬Èç¡°\*¡±À´±íʾ
ÕæʵµÄ¡°*¡±×Ö·û£¬¶ø²»ÊÇÈÎÒⳤ¶ÈµÄ×Ö·û´®¡£
ºÃ°É£¬»¹ÊÇÏÈÀ´¿´¼¸¸öÀý×Ó°É£º
clean:
rm -f *.o
ÉÏÃæÕâ¸öÀý×ÓÎÒ²»²»¶à˵ÁË£¬ÕâÊDzÙ×÷ϵͳShellËùÖ§³ÖµÄͨÅä·û¡£ÕâÊÇÔÚÃüÁîÖеÄͨ
Åä·û¡£
print: *.c
lpr -p $?
touch print
ÉÏÃæÕâ¸öÀý×Ó˵Ã÷ÁËͨÅä·ûÒ²¿ÉÒÔÔÚÎÒÃǵĹæÔòÖУ¬Ä¿±êprintÒÀÀµÓÚËùÓеÄ[.c]Îļþ
¡£ÆäÖеġ°$?¡±ÊÇÒ»¸ö×Ô¶¯»¯±äÁ¿£¬ÎÒ»áÔÚºóÃæ¸øÄã½²Êö¡£
objects = *.o
ÉÏÃæÕâ¸öÀý×Ó£¬±íʾÁË£¬Í¨·ûͬÑù¿ÉÒÔÓÃÔÚ±äÁ¿ÖС£²¢²»ÊÇ˵[*.o]»áÕ¹¿ª£¬²»£¡objec
tsµÄÖµ¾ÍÊÇ¡°*.o¡±¡£MakefileÖеıäÁ¿Æäʵ¾ÍÊÇC/C++ÖеĺꡣÈç¹ûÄãÒªÈÃͨÅä·ûÔÚ±äÁ¿ÖÐ
Õ¹¿ª£¬Ò²¾ÍÊÇÈÃobjectsµÄÖµÊÇËùÓÐ[.o]µÄÎļþÃûµÄ¼¯ºÏ£¬ÄÇô£¬Äã¿ÉÒÔÕâÑù£º
objects := $(wildcard *.o)
ÕâÖÖÓ÷¨Óɹؼü×Ö¡°wildcard¡±Ö¸³ö£¬¹ØÓÚMakefileµÄ¹Ø¼ü×Ö£¬ÎÒÃǽ«ÔÚºóÃæÌÖÂÛ¡£

ËÄ¡¢ÎļþËÑÑ°
ÔÚһЩ´óµÄ¹¤³ÌÖУ¬ÓдóÁ¿µÄÔ´Îļþ£¬ÎÒÃÇͨ³£µÄ×ö·¨ÊÇ°ÑÕâÐí¶àµÄÔ´Îļþ·ÖÀ࣬²¢´æ·ÅÔÚ
²»Í¬µÄĿ¼ÖС£ËùÒÔ£¬µ±makeÐèҪȥÕÒÑ°ÎļþµÄÒÀÀµ¹Øϵʱ£¬Äã¿ÉÒÔÔÚÎļþÇ°¼ÓÉÏ·¾¶£¬µ«
×îºÃµÄ·½·¨ÊÇ°ÑÒ»¸ö·¾¶¸æËßmake£¬ÈÃmakeÔÚ×Ô¶¯È¥ÕÒ¡£
MakefileÎļþÖеÄÌØÊâ±äÁ¿¡°VPATH¡±¾ÍÊÇÍê³ÉÕâ¸ö¹¦Äܵģ¬Èç¹ûûÓÐÖ¸Ã÷Õâ¸ö±äÁ¿£¬make
Ö»»áÔÚµ±Ç°µÄĿ¼ÖÐÈ¥ÕÒÑ°ÒÀÀµÎļþºÍÄ¿±êÎļþ¡£Èç¹û¶¨ÒåÁËÕâ¸ö±äÁ¿£¬ÄÇô£¬make¾Í»áÔÚ
µ±µ±Ç°Ä¿Â¼ÕÒ²»µ½µÄÇé¿öÏ£¬µ½ËùÖ¸¶¨µÄĿ¼ÖÐÈ¥ÕÒÑ°ÎļþÁË¡£
VPATH = src:../headers
ÉÏÃæµÄµÄ¶¨ÒåÖ¸¶¨Á½¸öĿ¼£¬¡°src¡±ºÍ¡°../headers¡±£¬make»á°´ÕÕÕâ¸ö˳Ðò½øÐÐËÑË÷¡£
Ŀ¼ÓÉ¡°Ã°ºÅ¡±·Ö¸ô¡££¨µ±È»£¬µ±Ç°Ä¿Â¼ÓÀÔ¶ÊÇ×î¸ßÓÅÏÈËÑË÷µÄµØ·½£©
ÁíÒ»¸öÉèÖÃÎļþËÑË÷·¾¶µÄ·½·¨ÊÇʹÓÃmakeµÄ¡°vpath¡±¹Ø¼ü×Ö£¨×¢Ò⣬ËüÊÇȫСдµÄ£©£¬
Õâ²»ÊDZäÁ¿£¬ÕâÊÇÒ»¸ömakeµÄ¹Ø¼ü×Ö£¬ÕâºÍÉÏÃæÌáµ½µÄÄǸöVPATH±äÁ¿ºÜÀàËÆ£¬µ«ÊÇËü¸üΪ
Áé»î¡£Ëü¿ÉÒÔÖ¸¶¨²»Í¬µÄÎļþÔÚ²»Í¬µÄËÑË÷Ŀ¼ÖС£ÕâÊÇÒ»¸öºÜÁé»îµÄ¹¦ÄÜ¡£ËüµÄʹÓ÷½·¨
ÓÐÈýÖÖ£º
1¡¢vpath
Ϊ·ûºÏģʽµÄÎļþÖ¸¶¨ËÑË÷Ŀ¼¡£
2¡¢vpath
Çå³ý·ûºÏģʽµÄÎļþµÄËÑË÷Ŀ¼¡£
3¡¢vpath
Çå³ýËùÓÐÒѱ»ÉèÖúÃÁ˵ÄÎļþËÑË÷Ŀ¼¡£
vapthʹÓ÷½·¨ÖеÄÐèÒª°üº¬¡°%¡±×Ö·û¡£¡°%¡±µÄÒâ˼ÊÇÆ¥ÅäÁã»òÈô¸É×Ö·û£¬ÀýÈç
£¬¡°%.h¡±±íʾËùÓÐÒÔ¡°.h¡±½áβµÄÎļþ¡£Ö¸¶¨ÁËÒªËÑË÷µÄÎļþ¼¯£¬¶øes>ÔòÖ¸¶¨Á˵ÄÎļþ¼¯µÄËÑË÷µÄĿ¼¡£ÀýÈ磺
vpath %.h ../headers
¸ÃÓï¾ä±íʾ£¬ÒªÇómakeÔÚ¡°../headers¡±Ä¿Â¼ÏÂËÑË÷ËùÓÐÒÔ¡°.h¡±½áβµÄÎļþ¡££¨Èç¹ûijÎÄ
¼þÔÚµ±Ç°Ä¿Â¼Ã»ÓÐÕÒµ½µÄ»°£©
ÎÒÃÇ¿ÉÒÔÁ¬ÐøµØʹÓÃvpathÓï¾ä£¬ÒÔÖ¸¶¨²»Í¬ËÑË÷²ßÂÔ¡£Èç¹ûÁ¬ÐøµÄvpathÓï¾äÖгöÏÖÁËÏàͬ
µÄ£¬»òÊDZ»Öظ´Á˵ģ¬ÄÇô£¬make»á°´ÕÕvpathÓï¾äµÄÏȺó˳ÐòÀ´Ö´ÐÐ
ËÑË÷¡£È磺
vpath %.c foo
vpath % blish
vpath %.c bar
Æä±íʾ¡°.c¡±½áβµÄÎļþ£¬ÏÈÔÚ¡°foo¡±Ä¿Â¼£¬È»ºóÊÇ¡°blish¡±£¬×îºóÊÇ¡°bar¡±Ä¿Â¼¡£
vpath %.c foo:bar
vpath % blish
¶øÉÏÃæµÄÓï¾äÔò±íʾ¡°.c¡±½áβµÄÎļþ£¬ÏÈÔÚ¡°foo¡±Ä¿Â¼£¬È»ºóÊÇ¡°bar¡±Ä¿Â¼£¬×îºó²ÅÊÇ
¡°blish¡±Ä¿Â¼¡£

Î塢αĿ±ê
×îÔçÏȵÄÒ»¸öÀý×ÓÖУ¬ÎÒÃÇÌáµ½¹ýÒ»¸ö¡°clean¡±µÄÄ¿±ê£¬ÕâÊÇÒ»¸ö¡°Î±Ä¿±ê¡±£¬
clean:
rm *.o temp
ÕýÏñÎÒÃÇÇ°ÃæÀý×ÓÖеġ°clean¡±Ò»Ñù£¬¼´È»ÎÒÃÇÉú³ÉÁËÐí¶àÎļþ±àÒëÎļþ£¬ÎÒÃÇÒ²Ó¦¸ÃÌá
¹©Ò»¸öÇå³ýËüÃǵġ°Ä¿±ê¡±ÒÔ±¸ÍêÕûµØÖرàÒë¶øÓᣠ£¨ÒÔ¡°make clean¡±À´Ê¹ÓøÃÄ¿±ê£©
ÒòΪ£¬ÎÒÃDz¢²»Éú³É¡°clean¡±Õâ¸öÎļþ¡£¡°Î±Ä¿±ê¡±²¢²»ÊÇÒ»¸öÎļþ£¬Ö»ÊÇÒ»¸ö±êÇ©£¬ÓÉ
ÓÚ¡°Î±Ä¿±ê¡±²»ÊÇÎļþ£¬ËùÒÔmakeÎÞ·¨Éú³ÉËüµÄÒÀÀµ¹ØϵºÍ¾ö¶¨ËüÊÇ·ñÒªÖ´ÐС£ÎÒÃÇÖ»ÓÐͨ
¹ýÏÔʾµØÖ¸Ã÷Õâ¸ö¡°Ä¿±ê¡±²ÅÄÜÈÃÆäÉúЧ¡£µ±È»£¬¡°Î±Ä¿±ê¡±µÄÈ¡Ãû²»ÄܺÍÎļþÃûÖØÃû£¬²»
È»Æä¾ÍʧȥÁË¡°Î±Ä¿±ê¡±µÄÒâÒåÁË¡£
µ±È»£¬ÎªÁ˱ÜÃâºÍÎļþÖØÃûµÄÕâÖÖÇé¿ö£¬ÎÒÃÇ¿ÉÒÔʹÓÃÒ»¸öÌØÊâµÄ±ê¼Ç¡°.PHONY¡±À´ÏÔʾµØ
Ö¸Ã÷Ò»¸öÄ¿±êÊÇ¡°Î±Ä¿±ê¡±£¬Ïòmake˵Ã÷£¬²»¹ÜÊÇ·ñÓÐÕâ¸öÎļþ£¬Õâ¸öÄ¿±ê¾ÍÊÇ¡°Î±Ä¿±ê¡±
¡£
.PHONY : clean
Ö»ÒªÓÐÕâ¸öÉùÃ÷£¬²»¹ÜÊÇ·ñÓС°clean¡±Îļþ£¬ÒªÔËÐС°clean¡±Õâ¸öÄ¿±ê£¬Ö»ÓС°make cle
an¡±ÕâÑù¡£ÓÚÊÇÕû¸ö¹ý³Ì¿ÉÒÔÕâÑùд£º
.PHONY: clean
clean:
rm *.o temp
αĿ±êÒ»°ãûÓÐÒÀÀµµÄÎļþ¡£µ«ÊÇ£¬ÎÒÃÇÒ²¿ÉÒÔΪαĿ±êÖ¸¶¨ËùÒÀÀµµÄÎļþ¡£Î±Ä¿±êͬÑù¿É
ÒÔ×÷Ϊ¡°Ä¬ÈÏÄ¿±ê¡±£¬Ö»Òª½«Æä·ÅÔÚµÚÒ»¸ö¡£Ò»¸öʾÀý¾ÍÊÇ£¬Èç¹ûÄãµÄMakefileÐèÒªÒ»¿ÚÆø
Éú³ÉÈô¸É¸ö¿ÉÖ´ÐÐÎļþ£¬µ«ÄãÖ»Ïë¼òµ¥µØÇÃÒ»¸ömakeÍêÊ£¬²¢ÇÒ£¬ËùÓеÄÄ¿±êÎļþ¶¼Ð´ÔÚÒ»
¸öMakefileÖУ¬ÄÇôÄã¿ÉÒÔʹÓá°Î±Ä¿±ê¡±Õâ¸öÌØÐÔ£º
all : prog1 prog2 prog3
.PHONY : all
prog1 : prog1.o utils.o
cc -o prog1 prog1.o utils.o
prog2 : prog2.o
cc -o prog2 prog2.o
prog3 : prog3.o sort.o utils.o
cc -o prog3 prog3.o sort.o utils.o
ÎÒÃÇÖªµÀ£¬MakefileÖеĵÚÒ»¸öÄ¿±ê»á±»×÷ΪÆäĬÈÏÄ¿±ê¡£ÎÒÃÇÉùÃ÷ÁËÒ»¸ö¡°all¡±µÄαĿ
±ê£¬ÆäÒÀÀµÓÚÆäËüÈý¸öÄ¿±ê¡£ÓÉÓÚαĿ±êµÄÌØÐÔÊÇ£¬×ÜÊDZ»Ö´Ðеģ¬ËùÒÔÆäÒÀÀµµÄÄÇÈý¸öÄ¿
±ê¾Í×ÜÊDz»Èç¡°all¡±Õâ¸öÄ¿±êС£ËùÒÔ£¬ÆäËüÈý¸öÄ¿±êµÄ¹æÔò×ÜÊǻᱻ¾öÒé¡£Ò²¾Í´ïµ½ÁË
ÎÒÃÇÒ»¿ÚÆøÉú³É¶à¸öÄ¿±êµÄÄ¿µÄ¡£¡°.PHONY : all¡±ÉùÃ÷ÁË¡°all¡±Õâ¸öÄ¿±êΪ¡°Î±Ä¿±ê¡±
¡£
Ëæ±ãÌáÒ»¾ä£¬´ÓÉÏÃæµÄÀý×ÓÎÒÃÇ¿ÉÒÔ¿´³ö£¬Ä¿±êÒ²¿ÉÒÔ³ÉΪÒÀÀµ¡£ËùÒÔ£¬Î±Ä¿±êͬÑùÒ²¿É³É
ΪÒÀÀµ¡£¿´ÏÂÃæµÄÀý×Ó£º
.PHONY: cleanall cleanobj cleandiff
cleanall : cleanobj cleandiff
rm program
cleanobj :
rm *.o
cleandiff :
rm *.diff
¡°make clean¡±½«Çå³ýËùÓÐÒª±»Çå³ýµÄÎļþ¡£¡°cleanobj¡±ºÍ¡°cleandiff¡±ÕâÁ½¸öαĿ±ê
ÓеãÏñ¡°×Ó³ÌÐò¡±µÄÒâ˼¡£ÎÒÃÇ¿ÉÒÔÊäÈë¡°make cleanall¡±ºÍ¡°make cleanobj¡±ºÍ¡°make
cleandiff¡±ÃüÁîÀ´´ïµ½Çå³ý²»Í¬ÖÖÀàÎļþµÄÄ¿µÄ¡£


Îå¡¢
Áù¡¢¶àÄ¿±ê
MakefileµÄ¹æÔòÖеÄÄ¿±ê¿ÉÒÔ²»Ö¹Ò»¸ö£¬ÆäÖ§³Ö¶àÄ¿±ê£¬ÓпÉÄÜÎÒÃǵĶà¸öÄ¿±êͬʱÒÀÀµÓÚ
Ò»¸öÎļþ£¬²¢ÇÒÆäÉú³ÉµÄÃüÁî´óÌåÀàËÆ¡£ÓÚÊÇÎÒÃǾÍÄÜ°ÑÆäºÏ²¢ÆðÀ´¡£µ±È»£¬¶à¸öÄ¿±êµÄÉú
³É¹æÔòµÄÖ´ÐÐÃüÁîÊÇͬһ¸ö£¬Õâ¿ÉÄÜ»á¿ÉÎÒÃÇ´øÀ´Âé·³£¬²»¹ýºÃÔÚÎÒÃǵĿÉÒÔʹÓÃÒ»¸ö×Ô¶¯
»¯±äÁ¿¡°$@¡±£¨¹ØÓÚ×Ô¶¯»¯±äÁ¿£¬½«ÔÚºóÃæ½²Êö£©£¬Õâ¸ö±äÁ¿±íʾ×ÅÄ¿Ç°¹æÔòÖÐËùÓеÄÄ¿±ê
µÄ¼¯ºÏ£¬ÕâÑù˵¿ÉÄܺܳéÏ󣬻¹ÊÇ¿´Ò»¸öÀý×Ó°É¡£
bigoutput littleoutput : text.g
generate text.g -$(subst output,,$@) > $@
ÉÏÊö¹æÔòµÈ¼ÛÓÚ£º
bigoutput : text.g
generate text.g -big > bigoutput
littleoutput : text.g
generate text.g -little > littleoutput
ÆäÖУ¬-$(subst output,,$@)Öеġ°$¡±±íʾִÐÐÒ»¸öMakefileµÄº¯Êý£¬º¯ÊýÃûΪsubs
t£¬ºóÃæµÄΪ²ÎÊý¡£¹ØÓÚº¯Êý£¬½«ÔÚºóÃæ½²Êö¡£ÕâÀïµÄÕâ¸öº¯ÊýÊǽØÈ¡×Ö·û´®µÄÒâ˼£¬¡°$@
¡±±íʾĿ±êµÄ¼¯ºÏ£¬¾ÍÏñÒ»¸öÊý×飬¡°$@¡±ÒÀ´ÎÈ¡³öÄ¿±ê£¬²¢Ö´ÓÚÃüÁî¡£

Æß¡¢¾²Ì¬Ä£Ê½
¾²Ì¬Ä£Ê½¿ÉÒÔ¸ü¼ÓÈÝÒ׵ض¨Òå¶àÄ¿±êµÄ¹æÔò£¬¿ÉÒÔÈÃÎÒÃǵĹæÔò±äµÃ¸ü¼ÓµÄÓе¯ÐÔºÍÁé»î¡£
ÎÒÃÇ»¹ÊÇÏÈÀ´¿´Ò»ÏÂÓï·¨£º
: :

...

targets¶¨ÒåÁËһϵÁеÄÄ¿±êÎļþ£¬¿ÉÒÔÓÐͨÅä·û¡£ÊÇÄ¿±êµÄÒ»¸ö¼¯ºÏ¡£
target-parrternÊÇÖ¸Ã÷ÁËtargetsµÄģʽ£¬Ò²¾ÍÊǵÄÄ¿±ê¼¯Ä£Ê½¡£
prereq-parrternsÊÇÄ¿±êµÄÒÀÀµÄ£Ê½£¬Ëü¶Ôtarget-parrternÐγɵÄģʽÔÙ½øÐÐÒ»´ÎÒÀ
ÀµÄ¿±êµÄ¶¨Òå¡£
ÕâÑùÃèÊöÕâÈý¸ö¶«Î÷£¬¿ÉÄÜ»¹ÊÇûÓÐ˵Çå³þ£¬»¹ÊǾٸöÀý×ÓÀ´ËµÃ÷һϰɡ£Èç¹ûÎÒÃǵÄget-parrtern>¶¨Òå³É¡°%.o¡±£¬Òâ˼ÊÇÎÒÃǵļ¯ºÏÖж¼ÊÇÒÔ¡°.o¡±½áβµÄ£¬¶øÈç¹û
ÎÒÃǵĶ¨Òå³É¡°%.c¡±£¬Òâ˼ÊǶÔËùÐγɵÄÄ¿±ê¼¯½ø
Ðжþ´Î¶¨Ò壬Æä¼ÆËã·½·¨ÊÇ£¬È¡Ä£Ê½Öеġ°%¡±£¨Ò²¾ÍÊÇÈ¥µôÁË[.o]Õâ¸ö
½á⣩£¬²¢ÎªÆä¼ÓÉÏ[.c]Õâ¸ö½á⣬ÐγɵÄм¯ºÏ¡£
ËùÒÔ£¬ÎÒÃǵġ°Ä¿±êģʽ¡±»òÊÇ¡°ÒÀÀµÄ£Ê½¡±Öж¼Ó¦¸ÃÓС°%¡±Õâ¸ö×Ö·û£¬Èç¹ûÄãµÄÎļþÃû
ÖÐÓС°%¡±ÄÇôÄã¿ÉÒÔʹÓ÷´Ð±¸Ü¡°\¡±½øÐÐתÒ壬À´±êÃ÷ÕæʵµÄ¡°%¡±×Ö·û¡£
¿´Ò»¸öÀý×Ó£º
objects = foo.o bar.o
all: $(objects)
$(objects): %.o: %.c
$(CC) -c $(CFLAGS) $< -o $@

ÉÏÃæµÄÀý×ÓÖУ¬Ö¸Ã÷ÁËÎÒÃǵÄÄ¿±ê´Ó$objectÖлñÈ¡£¬¡°%.o¡±±íÃ÷ÒªËùÓÐÒÔ¡°.o¡±½áβµÄÄ¿
±ê£¬Ò²¾ÍÊÇ¡°foo.o bar.o¡±£¬Ò²¾ÍÊDZäÁ¿$object¼¯ºÏµÄģʽ£¬¶øÒÀÀµÄ£Ê½¡°%.c¡±ÔòÈ¡Ä£
ʽ¡°%.o¡±µÄ¡°%¡±£¬Ò²¾ÍÊÇ¡°foo bar¡±£¬²¢ÎªÆä¼ÓÏ¡°.c¡±µÄºó׺£¬ÓÚÊÇ£¬ÎÒÃǵÄÒÀÀµÄ¿
±ê¾ÍÊÇ¡°foo.c bar.c¡±¡£¶øÃüÁîÖеġ°$<¡±ºÍ¡°$@¡±ÔòÊÇ×Ô¶¯»¯±äÁ¿£¬¡°$<¡±±íʾËùÓеÄ
ÒÀÀµÄ¿±ê¼¯£¨Ò²¾ÍÊÇ¡°foo.c bar.c¡±£©£¬¡°$@¡±±íʾĿ±ê¼¯£¨Ò²¾ÍÊÇ¡°foo.o bar.o¡±£©¡£
ÓÚÊÇ£¬ÉÏÃæµÄ¹æÔòÕ¹¿ªºóµÈ¼ÛÓÚÏÂÃæµÄ¹æÔò£º
foo.o : foo.c
$(CC) -c $(CFLAGS) foo.c -o foo.o
bar.o : bar.c
$(CC) -c $(CFLAGS) bar.c -o bar.o
ÊÔÏ룬Èç¹ûÎÒÃǵġ°%.o¡±Óм¸°Ù¸ö£¬ÄÇÖÖÎÒÃÇÖ»ÒªÓÃÕâÖֺܼòµ¥µÄ¡°¾²Ì¬Ä£Ê½¹æÔò¡±¾Í¿É
ÒÔдÍêÒ»¶Ñ¹æÔò£¬ÊµÔÚÊÇÌ«ÓÐЧÂÊÁË¡£¡°¾²Ì¬Ä£Ê½¹æÔò¡±µÄÓ÷¨ºÜÁé»î£¬Èç¹ûÓõúã¬ÄÇ»á
Ò»¸öºÜÇ¿´óµÄ¹¦ÄÜ¡£ÔÙ¿´Ò»¸öÀý×Ó£º

files = foo.elc bar.o lose.o
$(filter %.o,$(files)): %.o: %.c
$(CC) -c $(CFLAGS) $< -o $@
$(filter %.elc,$(files)): %.elc: %.el
emacs -f batch-byte-compile $<

$(filter %.o,$(files))±íʾµ÷ÓÃMakefileµÄfilterº¯Êý£¬¹ýÂË¡°$filter¡±¼¯£¬Ö»ÒªÆäÖÐ
ģʽΪ¡°%.o¡±µÄÄÚÈÝ¡£ÆäµÄËüÄÚÈÝ£¬ÎҾͲ»Óöà˵ÁË°É¡£Õâ¸öÀý×ÖչʾÁËMakefileÖиü´ó
µÄµ¯ÐÔ¡£

°Ë¡¢×Ô¶¯Éú³ÉÒÀÀµÐÔ
ÔÚMakefileÖУ¬ÎÒÃǵÄÒÀÀµ¹Øϵ¿ÉÄÜ»áÐèÒª°üº¬Ò»ÏµÁеÄÍ·Îļþ£¬±ÈÈ磬Èç¹ûÎÒÃǵÄmain.
cÖÐÓÐÒ»¾ä¡°#include "defs.h"¡±£¬ÄÇôÎÒÃǵÄÒÀÀµ¹ØϵӦ¸ÃÊÇ£º
main.o : main.c defs.h
µ«ÊÇ£¬Èç¹ûÊÇÒ»¸ö±È½Ï´óÐ͵Ť³Ì£¬Äã±ØÐèÇå³þÄÄЩCÎļþ°üº¬ÁËÄÄЩͷÎļþ£¬²¢ÇÒ£¬ÄãÔÚ
¼ÓÈë»òɾ³ýÍ·Îļþʱ£¬Ò²ÐèҪСÐĵØÐÞ¸ÄMakefile£¬ÕâÊÇÒ»¸öºÜûÓÐά»¤ÐԵŤ×÷¡£ÎªÁ˱Ü
ÃâÕâÖÖ·±ÖضøÓÖÈÝÒ׳ö´íµÄÊÂÇ飬ÎÒÃÇ¿ÉÒÔʹÓÃC/C++±àÒëµÄÒ»¸ö¹¦ÄÜ¡£´ó¶àÊýµÄC/C++±àÒë
Æ÷¶¼Ö§³ÖÒ»¸ö¡°-M¡±µÄÑ¡Ï¼´×Ô¶¯ÕÒÑ°Ô´ÎļþÖаüº¬µÄÍ·Îļþ£¬²¢Éú³ÉÒ»¸öÒÀÀµ¹Øϵ¡£Àý
È磬Èç¹ûÎÒÃÇÖ´ÐÐÏÂÃæµÄÃüÁ
cc -M main.c
ÆäÊä³öÊÇ£º
main.o : main.c defs.h
ÓÚÊÇÓɱàÒëÆ÷×Ô¶¯Éú³ÉµÄÒÀÀµ¹Øϵ£¬ÕâÑùÒ»À´£¬Äã¾Í²»±ØÔÙÊÖ¶¯ÊéдÈô¸ÉÎļþµÄÒÀÀµ¹Øϵ£¬
¶øÓɱàÒëÆ÷×Ô¶¯Éú³ÉÁË¡£ÐèÒªÌáÐÑÒ»¾äµÄÊÇ£¬Èç¹ûÄãʹÓÃGNUµÄC/C++±àÒëÆ÷£¬ÄãµÃÓá°-MM
¡±²ÎÊý£¬²»È»£¬¡°-M¡±²ÎÊý»á°ÑһЩ±ê×¼¿âµÄÍ·ÎļþÒ²°üº¬½øÀ´¡£
gcc -M main.cµÄÊä³öÊÇ£º
main.o: main.c defs.h /usr/include/stdio.h /usr/include/features.h \
/usr/include/sys/cdefs.h /usr/include/gnu/stubs.h \
/usr/lib/gcc-lib/i486-suse-linux/2.95.3/include/stddef.h \
/usr/include/bits/types.h /usr/include/bits/pthreadtypes.h \
/usr/include/bits/sched.h /usr/include/libio.h \
/usr/include/_G_config.h /usr/include/wchar.h \
/usr/include/bits/wchar.h /usr/include/gconv.h \
/usr/lib/gcc-lib/i486-suse-linux/2.95.3/include/stdarg.h \
/usr/include/bits/stdio_lim.h

gcc -MM main.cµÄÊä³öÔòÊÇ£º
main.o: main.c defs.h
ÄÇô£¬±àÒëÆ÷µÄÕâ¸ö¹¦ÄÜÈçºÎÓëÎÒÃǵÄMakefileÁªÏµÔÚÒ»ÆðÄØ¡£ÒòΪÕâÑùÒ»À´£¬ÎÒÃǵÄMake
fileÒ²Òª¸ù¾ÝÕâЩԴÎļþÖØÐÂÉú³É£¬ÈÃMakefile×ÔÒÑÒÀÀµÓÚÔ´Îļþ£¿Õâ¸ö¹¦Äܲ¢²»ÏÖʵ£¬²»
¹ýÎÒÃÇ¿ÉÒÔÓÐÆäËüÊÖ¶ÎÀ´ÓػصØʵÏÖÕâÒ»¹¦ÄÜ¡£GNU×éÖ¯½¨Òé°Ñ±àÒëÆ÷Ϊÿһ¸öÔ´ÎļþµÄ×Ô
¶¯Éú³ÉµÄÒÀÀµ¹Øϵ·Åµ½Ò»¸öÎļþÖУ¬ÎªÃ¿Ò»¸ö¡°name.c¡±µÄÎļþ¶¼Éú³ÉÒ»¸ö¡°name.d¡±µÄMa
kefileÎļþ£¬[.d]ÎļþÖоʹæ·Å¶ÔÓ¦[.c]ÎļþµÄÒÀÀµ¹Øϵ¡£
ÓÚÊÇ£¬ÎÒÃÇ¿ÉÒÔд³ö[.c]ÎļþºÍ[.d]ÎļþµÄÒÀÀµ¹Øϵ£¬²¢ÈÃmake×Ô¶¯¸üлò×Ô³É[.d]Îļþ£¬
²¢°ÑÆä°üº¬ÔÚÎÒÃǵÄÖ÷MakefileÖУ¬ÕâÑù£¬ÎÒÃǾͿÉÒÔ×Ô¶¯»¯µØÉú³Éÿ¸öÎļþµÄÒÀÀµ¹ØϵÁË
¡£
ÕâÀÎÒÃǸø³öÁËÒ»¸öģʽ¹æÔòÀ´²úÉú[.d]Îļþ£º
%.d: %.c
@set -e; rm -f $@; \
$(CC) -M $(CPPFLAGS) $< > $@.$$$$; \
sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
rm -f $@.$$$$

Õâ¸ö¹æÔòµÄÒâ˼ÊÇ£¬ËùÓеÄ[.d]ÎļþÒÀÀµÓÚ[.c]Îļþ£¬¡°rm -f $@¡±µÄÒâ˼ÊÇɾ³ýËùÓеÄÄ¿
±ê£¬Ò²¾ÍÊÇ[.d]Îļþ£¬µÚ¶þÐеÄÒâ˼ÊÇ£¬ÎªÃ¿¸öÒÀÀµÎļþ¡°$<¡±£¬Ò²¾ÍÊÇ[.c]ÎļþÉú³ÉÒÀÀµ
Îļþ£¬¡°$@¡±±íʾģʽ¡°%.d¡±Îļþ£¬Èç¹ûÓÐÒ»¸öCÎļþÊÇname.c£¬ÄÇô¡°%¡±¾ÍÊÇ¡°name¡±
£¬¡°$$$$¡±ÒâΪһ¸öËæ»ú±àºÅ£¬µÚ¶þÐÐÉú³ÉµÄÎļþÓпÉÄÜÊÇ¡°name.d.12345¡±£¬µÚÈýÐÐʹÓÃ
sedÃüÁî×öÁËÒ»¸öÌæ»»£¬¹ØÓÚsedÃüÁîµÄÓ÷¨Çë²Î¿´Ïà¹ØµÄʹÓÃÎĵµ¡£µÚËÄÐоÍÊÇɾ³ýÁÙʱÎÄ
¼þ¡£
×ܶøÑÔÖ®£¬Õâ¸öģʽҪ×öµÄʾÍÊÇÔÚ±àÒëÆ÷Éú³ÉµÄÒÀÀµ¹ØϵÖмÓÈë[.d]ÎļþµÄÒÀÀµ£¬¼´°ÑÒÀ
Àµ¹Øϵ£º
main.o : main.c defs.h
ת³É£º
main.o main.d : main.c defs.h
ÓÚÊÇ£¬ÎÒÃǵÄ[.d]ÎļþÒ²»á×Ô¶¯¸üÐÂÁË£¬²¢»á×Ô¶¯Éú³ÉÁË£¬µ±È»£¬Ä㻹¿ÉÒÔÔÚÕâ¸ö[.d]Îļþ
ÖмÓÈëµÄ²»Ö»ÊÇÒÀÀµ¹Øϵ£¬°üÀ¨Éú³ÉµÄÃüÁîÒ²¿ÉÒ»²¢¼ÓÈ룬ÈÃÿ¸ö[.d]Îļþ¶¼°üº¬Ò»¸öÍêÀµ
µÄ¹æÔò¡£Ò»µ©ÎÒÃÇÍê³ÉÕâ¸ö¹¤×÷£¬½ÓÏÂÀ´£¬ÎÒÃǾÍÒª°ÑÕâЩ×Ô¶¯Éú³ÉµÄ¹æÔò·Å½øÎÒÃǵÄÖ÷Ma
kefileÖС£ÎÒÃÇ¿ÉÒÔʹÓÃMakefileµÄ¡°include¡±ÃüÁÀ´ÒýÈë±ðµÄMakefileÎļþ£¨Ç°Ãæ½²
¹ý£©£¬ÀýÈ磺
sources = foo.c bar.c
include $(sources:.c=.d)
ÉÏÊöÓï¾äÖеġ°$(sources:.c=.d)¡±Öеġ°.c=.d¡±µÄÒâ˼ÊÇ×öÒ»¸öÌæ»»£¬°Ñ±äÁ¿$(sources
)ËùÓÐ[.c]µÄ×Ö´®¶¼Ìæ»»³É[.d]£¬¹ØÓÚÕâ¸ö¡°Ìæ»»¡±µÄÄÚÈÝ£¬ÔÚºóÃæÎÒ»áÓиüΪÏêϸµÄ½²Êö
¡£µ±È»£¬ÄãµÃ×¢Òâ´ÎÐò£¬ÒòΪincludeÊÇ°´´ÎÀ´ÔØÈëÎļþ£¬×îÏÈÔØÈëµÄ[.d]ÎļþÖеÄÄ¿±ê»á
³ÉΪĬÈÏÄ¿±ê


ÁùÊéдÃüÁî
--------
ÿÌõ¹æÔòÖеÄÃüÁîºÍ²Ù×÷ϵͳShellµÄÃüÁîÐÐÊÇÒ»Öµġ£make»áÒ»°´Ë³ÐòÒ»ÌõÒ»ÌõµÄÖ´ÐÐÃü
ÁÿÌõÃüÁîµÄ¿ªÍ·±ØÐëÒÔ[Tab]¼ü¿ªÍ·£¬³ý·Ç£¬ÃüÁîÊǽô¸úÔÚÒÀÀµ¹æÔòºóÃæµÄ·ÖºÅºóµÄ¡£
ÔÚÃüÁîÐÐÖ®¼äÖеĿոñ»òÊÇ¿ÕÐлᱻºöÂÔ£¬µ«ÊÇÈç¹û¸Ã¿Õ¸ñ»ò¿ÕÐÐÊÇÒÔTab¼ü¿ªÍ·µÄ£¬ÄÇôm
ake»áÈÏΪÆäÊÇÒ»¸ö¿ÕÃüÁî¡£
ÎÒÃÇÔÚUNIXÏ¿ÉÄÜ»áʹÓò»Í¬µÄShell£¬µ«ÊÇmakeµÄÃüÁîĬÈÏÊDZ»¡°/bin/sh¡±----UNIXµÄ±ê
×¼Shell½âÊÍÖ´Ðеġ£³ý·ÇÄãÌرðÖ¸¶¨Ò»¸öÆäËüµÄShell¡£MakefileÖУ¬¡°#¡±ÊÇ×¢ÊÍ·û£¬ºÜ
ÏñC/C++Öеġ°//¡±£¬ÆäºóµÄ±¾ÐÐ×Ö·û¶¼±»×¢ÊÍ¡£
Ò»¡¢ÏÔʾÃüÁî
ͨ³££¬make»á°ÑÆäÒªÖ´ÐеÄÃüÁîÐÐÔÚÃüÁîÖ´ÐÐÇ°Êä³öµ½ÆÁÄ»ÉÏ¡£µ±ÎÒÃÇÓá°@¡±×Ö·ûÔÚÃüÁî
ÐÐÇ°£¬ÄÇô£¬Õâ¸öÃüÁ²»±»makeÏÔʾ³öÀ´£¬×î¾ß´ú±íÐÔµÄÀý×ÓÊÇ£¬ÎÒÃÇÓÃÕâ¸ö¹¦ÄÜÀ´ÏñÆÁ
Ä»ÏÔʾһЩÐÅÏ¢¡£È磺
@echo ÕýÔÚ±àÒëXXXÄ£¿é......
µ±makeÖ´ÐÐʱ£¬»áÊä³ö¡°ÕýÔÚ±àÒëXXXÄ£¿é......¡±×Ö´®£¬µ«²»»áÊä³öÃüÁÈç¹ûûÓС°@¡±
£¬ÄÇô£¬make½«Êä³ö£º
echo ÕýÔÚ±àÒëXXXÄ£¿é......
ÕýÔÚ±àÒëXXXÄ£¿é......
Èç¹ûmakeÖ´ÐÐʱ£¬´øÈëmake²ÎÊý¡°-n¡±»ò¡°--just-print¡±£¬ÄÇôÆäÖ»ÊÇÏÔʾÃüÁµ«²»»á
Ö´ÐÐÃüÁÕâ¸ö¹¦ÄܺÜÓÐÀûÓÚÎÒÃǵ÷ÊÔÎÒÃǵÄMakefile£¬¿´¿´ÎÒÃÇÊéдµÄÃüÁîÊÇÖ´ÐÐÆðÀ´ÊÇ
ʲôÑù×ӵĻòÊÇʲô˳ÐòµÄ¡£
¶ømake²ÎÊý¡°-s¡±»ò¡°--slient¡±ÔòÊÇÈ«Ãæ½ûÖ¹ÃüÁîµÄÏÔʾ¡£

¶þ¡¢ÃüÁîÖ´ÐÐ
µ±ÒÀÀµÄ¿±êÐÂÓÚÄ¿±êʱ£¬Ò²¾ÍÊǵ±¹æÔòµÄÄ¿±êÐèÒª±»¸üÐÂʱ£¬make»áÒ»ÌõÒ»ÌõµÄÖ´ÐÐÆäºóµÄ
ÃüÁî¡£ÐèҪעÒâµÄÊÇ£¬Èç¹ûÄãÒªÈÃÉÏÒ»ÌõÃüÁîµÄ½á¹ûÓ¦ÓÃÔÚÏÂÒ»ÌõÃüÁîʱ£¬ÄãÓ¦¸ÃʹÓ÷ֺÅ
·Ö¸ôÕâÁ½ÌõÃüÁî¡£±ÈÈçÄãµÄµÚÒ»ÌõÃüÁîÊÇcdÃüÁÄãÏ£ÍûµÚ¶þÌõÃüÁîµÃÔÚcdÖ®ºóµÄ»ù´¡ÉÏÔË
ÐУ¬ÄÇôÄã¾Í²»ÄÜ°ÑÕâÁ½ÌõÃüÁîдÔÚÁ½ÐÐÉÏ£¬¶øÓ¦¸Ã°ÑÕâÁ½ÌõÃüÁîдÔÚÒ»ÐÐÉÏ£¬Ó÷ֺŷָô
¡£È磺
ʾÀýÒ»£º
exec:
cd /home/hchen
pwd
ʾÀý¶þ£º
exec:
cd /home/hchen; pwd
µ±ÎÒÃÇÖ´ÐС°make exec¡±Ê±£¬µÚÒ»¸öÀý×ÓÖеÄcdûÓÐ×÷Óã¬pwd»á´òÓ¡³öµ±Ç°µÄMakefileÄ¿
¼£¬¶øµÚ¶þ¸öÀý×ÓÖУ¬cd¾ÍÆð×÷ÓÃÁË£¬pwd»á´òÓ¡³ö¡°/home/hchen¡±¡£
makeÒ»°ãÊÇʹÓû·¾³±äÁ¿SHELLÖÐËù¶¨ÒåµÄϵͳShellÀ´Ö´ÐÐÃüÁĬÈÏÇé¿öÏÂʹÓÃUNIXµÄ±ê
×¼Shell----/bin/shÀ´Ö´ÐÐÃüÁî¡£µ«ÔÚMS-DOSÏÂÓеãÌØÊ⣬ÒòΪMS-DOSÏÂûÓÐSHELL»·¾³±ä
Á¿£¬µ±È»ÄãÒ²¿ÉÒÔÖ¸¶¨¡£Èç¹ûÄãÖ¸¶¨ÁËUNIX·ç¸ñµÄĿ¼ÐÎʽ£¬Ê×ÏÈ£¬make»áÔÚSHELLËùÖ¸¶¨
µÄ·¾¶ÖÐÕÒÑ°ÃüÁî½âÊÍÆ÷£¬Èç¹ûÕÒ²»µ½£¬Æä»áÔÚµ±Ç°ÅÌ·ûÖеĵ±Ç°Ä¿Â¼ÖÐÑ°ÕÒ£¬Èç¹ûÔÙÕÒ²»
µ½£¬Æä»áÔÚPATH»·¾³±äÁ¿ÖÐËù¶¨ÒåµÄËùÓз¾¶ÖÐÑ°ÕÒ¡£MS-DOSÖУ¬Èç¹ûÄ㶨ÒåµÄÃüÁî½âÊÍÆ÷
ûÓÐÕÒµ½£¬Æä»á¸øÄãµÄÃüÁî½âÊÍÆ÷¼ÓÉÏÖîÈç¡°.exe¡±¡¢¡°.com¡±¡¢¡°.bat¡±¡¢¡°.sh¡±µÈºó
׺¡£


Èý¡¢ÃüÁî³ö´í
ÿµ±ÃüÁîÔËÐÐÍêºó£¬make»á¼ì²âÿ¸öÃüÁîµÄ·µ»ØÂ룬Èç¹ûÃüÁî·µ»Ø³É¹¦£¬ÄÇômake»áÖ´ÐÐÏÂ
Ò»ÌõÃüÁµ±¹æÔòÖÐËùÓеÄÃüÁî³É¹¦·µ»Øºó£¬Õâ¸ö¹æÔò¾ÍËãÊdzɹ¦Íê³ÉÁË¡£Èç¹ûÒ»¸ö¹æÔòÖÐ
µÄij¸öÃüÁî³ö´íÁË£¨ÃüÁîÍ˳öÂë·ÇÁ㣩£¬ÄÇômake¾Í»áÖÕÖ¹Ö´Ðе±Ç°¹æÔò£¬Õ⽫ÓпÉÄÜÖÕÖ¹
ËùÓйæÔòµÄÖ´ÐС£
ÓÐЩʱºò£¬ÃüÁîµÄ³ö´í²¢²»±íʾ¾ÍÊÇ´íÎóµÄ¡£ÀýÈçmkdirÃüÁÎÒÃÇÒ»¶¨ÐèÒª½¨Á¢Ò»¸öĿ¼
£¬Èç¹ûĿ¼²»´æÔÚ£¬ÄÇômkdir¾Í³É¹¦Ö´ÐУ¬ÍòÊ´󼪣¬Èç¹ûĿ¼´æÔÚ£¬ÄÇô¾Í³ö´íÁË¡£ÎÒ
ÃÇÖ®ËùÒÔʹÓÃmkdirµÄÒâ˼¾ÍÊÇÒ»¶¨ÒªÓÐÕâÑùµÄÒ»¸öĿ¼£¬ÓÚÊÇÎÒÃǾͲ»Ï£Íûmkdir³ö´í¶øÖÕ
Ö¹¹æÔòµÄÔËÐС£
ΪÁË×öµ½ÕâÒ»µã£¬ºöÂÔÃüÁîµÄ³ö´í£¬ÎÒÃÇ¿ÉÒÔÔÚMakefileµÄÃüÁîÐÐÇ°¼ÓÒ»¸ö¼õºÅ¡°-¡±£¨ÔÚT
ab¼üÖ®ºó£©£¬±ê¼ÇΪ²»¹ÜÃüÁî³ö²»³ö´í¶¼ÈÏΪÊdzɹ¦µÄ¡£È磺
clean:
-rm -f *.o
»¹ÓÐÒ»¸öÈ«¾ÖµÄ°ì·¨ÊÇ£¬¸ømake¼ÓÉÏ¡°-i¡±»òÊÇ¡°--ignore-errors¡±²ÎÊý£¬ÄÇô£¬Makefil
eÖÐËùÓÐÃüÁ»áºöÂÔ´íÎ󡣶øÈç¹ûÒ»¸ö¹æÔòÊÇÒÔ¡°.IGNORE¡±×÷ΪĿ±êµÄ£¬ÄÇôÕâ¸ö¹æÔòÖÐ
µÄËùÓÐÃüÁ»áºöÂÔ´íÎó¡£ÕâЩÊDz»Í¬¼¶±ðµÄ·ÀÖ¹ÃüÁî³ö´íµÄ·½·¨£¬Äã¿ÉÒÔ¸ù¾ÝÄãµÄ²»Í¬Ï²
»¶ÉèÖá£
»¹ÓÐÒ»¸öÒªÌáһϵÄmakeµÄ²ÎÊýµÄÊÇ¡°-k¡±»òÊÇ¡°--keep-going¡±£¬Õâ¸ö²ÎÊýµÄÒâ˼ÊÇ£¬Èç
¹ûij¹æÔòÖеÄÃüÁî³ö´íÁË£¬ÄÇô¾ÍÖÕÄ¿¸Ã¹æÔòµÄÖ´ÐУ¬µ«¼ÌÐøÖ´ÐÐÆäËü¹æÔò¡£


ËÄ¡¢Ç¶Ì×Ö´ÐÐmake
ÔÚһЩ´óµÄ¹¤³ÌÖУ¬ÎÒÃÇ»á°ÑÎÒÃDz»Í¬Ä£¿é»òÊDz»Í¬¹¦ÄܵÄÔ´Îļþ·ÅÔÚ²»Í¬µÄĿ¼ÖУ¬ÎÒÃÇ
¿ÉÒÔÔÚÿ¸öĿ¼Öж¼Êéдһ¸ö¸ÃĿ¼µÄMakefile£¬ÕâÓÐÀûÓÚÈÃÎÒÃǵÄMakefile±äµÃ¸ü¼ÓµØ¼ò
½à£¬¶ø²»ÖÁÓÚ°ÑËùÓеĶ«Î÷È«²¿Ð´ÔÚÒ»¸öMakefileÖУ¬ÕâÑù»áºÜÄÑά»¤ÎÒÃǵÄMakefile£¬Õâ
¸ö¼¼Êõ¶ÔÓÚÎÒÃÇÄ£¿é±àÒëºÍ·Ö¶Î±àÒëÓÐ×ŷdz£´óµÄºÃ´¦¡£
ÀýÈ磬ÎÒÃÇÓÐÒ»¸ö×ÓĿ¼½Ðsubdir£¬Õâ¸öĿ¼ÏÂÓиöMakefileÎļþ£¬À´Ö¸Ã÷ÁËÕâ¸öĿ¼ÏÂÎÄ
¼þµÄ±àÒë¹æÔò¡£ÄÇôÎÒÃÇ×ܿصÄMakefile¿ÉÒÔÕâÑùÊéд£º
subsystem:
cd subdir && $(MAKE)
ÆäµÈ¼ÛÓÚ£º
subsystem:
$(MAKE) -C subdir
¶¨Òå$(MAKE)ºê±äÁ¿µÄÒâ˼ÊÇ£¬Ò²ÐíÎÒÃǵÄmakeÐèҪһЩ²ÎÊý£¬ËùÒÔ¶¨Òå³ÉÒ»¸ö±äÁ¿±È½ÏÀû
ÓÚά»¤¡£ÕâÁ½¸öÀý×ÓµÄÒâ˼¶¼ÊÇÏȽøÈë¡°subdir¡±Ä¿Â¼£¬È»ºóÖ´ÐÐmakeÃüÁî¡£
ÎÒÃÇ°ÑÕâ¸öMakefile½Ð×ö¡°×Ü¿ØMakefile¡±£¬×Ü¿ØMakefileµÄ±äÁ¿¿ÉÒÔ´«µÝµ½Ï¼¶µÄMakefi
leÖУ¨Èç¹ûÄãÏÔʾµÄÉùÃ÷£©£¬µ«ÊDz»»á¸²¸ÇϲãµÄMakefileÖÐËù¶¨ÒåµÄ±äÁ¿£¬³ý·ÇÖ¸¶¨ÁË¡°
-e¡±²ÎÊý¡£
Èç¹ûÄãÒª´«µÝ±äÁ¿µ½Ï¼¶MakefileÖУ¬ÄÇôÄã¿ÉÒÔʹÓÃÕâÑùµÄÉùÃ÷£º
export
Èç¹ûÄã²»ÏëÈÃijЩ±äÁ¿´«µÝµ½Ï¼¶MakefileÖУ¬ÄÇôÄã¿ÉÒÔÕâÑùÉùÃ÷£º
unexport
È磺

ʾÀýÒ»£º
export variable = value
ÆäµÈ¼ÛÓÚ£º
variable = value
export variable
ÆäµÈ¼ÛÓÚ£º
export variable := value
ÆäµÈ¼ÛÓÚ£º
variable := value
export variable
ʾÀý¶þ£º
export variable += value
ÆäµÈ¼ÛÓÚ£º
variable += value
export variable
Èç¹ûÄãÒª´«µÝËùÓеıäÁ¿£¬ÄÇô£¬Ö»ÒªÒ»¸öexport¾ÍÐÐÁË¡£ºóÃæʲôҲ²»Óøú£¬±íʾ´«µÝËù
ÓеıäÁ¿¡£
ÐèҪעÒâµÄÊÇ£¬ÓÐÁ½¸ö±äÁ¿£¬Ò»¸öÊÇSHELL£¬Ò»¸öÊÇMAKEFLAGS£¬ÕâÁ½¸ö±äÁ¿²»¹ÜÄãÊÇ·ñexpo
rt£¬Æä×ÜÊÇÒª´«µÝµ½Ï²ãMakefileÖУ¬ÌرðÊÇMAKEFILES±äÁ¿£¬ÆäÖаüº¬ÁËmakeµÄ²ÎÊýÐÅÏ¢
£¬Èç¹ûÎÒÃÇÖ´ÐС°×Ü¿ØMakefile¡±Ê±ÓÐmake²ÎÊý»òÊÇÔÚÉϲãMakefileÖж¨ÒåÁËÕâ¸ö±äÁ¿£¬ÄÇ
ôMAKEFILES±äÁ¿½«»áÊÇÕâЩ²ÎÊý£¬²¢»á´«µÝµ½Ï²ãMakefileÖУ¬ÕâÊÇÒ»¸öϵͳ¼¶µÄ»·¾³±ä
Á¿¡£
µ«ÊÇmakeÃüÁîÖеÄÓм¸¸ö²ÎÊý²¢²»ÍùÏ´«µÝ£¬ËüÃÇÊÇ¡°-C¡±,¡°-f¡±,¡°-h¡±¡°-o¡±ºÍ¡°-W¡±
£¨ÓйØMakefile²ÎÊýµÄϸ½Ú½«ÔÚºóÃæ˵Ã÷£©£¬Èç¹ûÄã²»ÏëÍùϲ㴫µÝ²ÎÊý£¬ÄÇô£¬Äã¿ÉÒÔÕâ
ÑùÀ´£º
subsystem:
cd subdir && $(MAKE) MAKEFLAGS=
Èç¹ûÄ㶨ÒåÁË»·¾³±äÁ¿MAKEFLAGS£¬ÄÇôÄãµÃÈ·ÐÅÆäÖеÄÑ¡ÏîÊÇ´ó¼Ò¶¼»áÓõ½µÄ£¬Èç¹ûÆäÖÐ
ÓС°-t¡±,¡°-n¡±,ºÍ¡°-q¡±²ÎÊý£¬ÄÇô½«»áÓÐÈÃÄãÒâÏë²»µ½µÄ½á¹û£¬»òÐí»áÈÃÄãÒì³£µØ¿Ö»Å
¡£
»¹ÓÐÒ»¸öÔÚ¡°Ç¶Ì×Ö´ÐС±ÖбȽÏÓÐÓõIJÎÊý£¬¡°-w¡±»òÊÇ¡°--print-directory¡±»áÔÚmake
µÄ¹ý³ÌÖÐÊä³öһЩÐÅÏ¢£¬ÈÃÄã¿´µ½Ä¿Ç°µÄ¹¤×÷Ŀ¼¡£±ÈÈ磬Èç¹ûÎÒÃǵÄϼ¶makeĿ¼ÊÇ¡°/h
ome/hchen/gnu/make¡±£¬Èç¹ûÎÒÃÇʹÓá°make -w¡±À´Ö´ÐУ¬ÄÇôµ±½øÈë¸ÃĿ¼ʱ£¬ÎÒÃÇ»á
¿´µ½£º
make: Entering directory `/home/hchen/gnu/make'.
¶øÔÚÍê³ÉϲãmakeºóÀ뿪Ŀ¼ʱ£¬ÎÒÃǻῴµ½£º
make: Leaving directory `/home/hchen/gnu/make'
µ±ÄãʹÓá°-C¡±²ÎÊýÀ´Ö¸¶¨makeϲãMakefileʱ£¬¡°-w¡±»á±»×Ô¶¯´ò¿ªµÄ¡£Èç¹û²ÎÊýÖÐÓС°
-s¡±£¨¡°--slient¡±£©»òÊÇ¡°--no-print-directory¡±£¬ÄÇô£¬¡°-w¡±×ÜÊÇʧЧµÄ¡£


Îå¡¢¶¨ÒåÃüÁî°ü
Èç¹ûMakefileÖгöÏÖһЩÏàͬÃüÁîÐòÁУ¬ÄÇôÎÒÃÇ¿ÉÒÔΪÕâЩÏàͬµÄÃüÁîÐòÁж¨ÒåÒ»¸ö±äÁ¿
¡£¶¨ÒåÕâÖÖÃüÁîÐòÁеÄÓï·¨ÒÔ¡°define¡±¿ªÊ¼£¬ÒÔ¡°endef¡±½áÊø£¬È磺
define run-yacc
yacc $(firstword $^)
mv y.tab.c $@
endef
ÕâÀ¡°run-yacc¡±ÊÇÕâ¸öÃüÁî°üµÄÃû×Ö£¬Æä²»ÒªºÍMakefileÖеıäÁ¿ÖØÃû¡£ÔÚ¡°define¡±
ºÍ¡°endef¡±ÖеÄÁ½ÐоÍÊÇÃüÁîÐòÁС£Õâ¸öÃüÁî°üÖеĵÚÒ»¸öÃüÁîÊÇÔËÐÐYacc³ÌÐò£¬ÒòΪYac
c³ÌÐò×ÜÊÇÉú³É¡°y.tab.c¡±µÄÎļþ£¬ËùÒÔµÚ¶þÐеÄÃüÁî¾ÍÊÇ°ÑÕâ¸öÎļþ¸Ä¸ÄÃû×Ö¡£»¹ÊÇ°ÑÕâ
¸öÃüÁî°ü·Åµ½Ò»¸öʾÀýÖÐÀ´¿´¿´°É¡£
foo.c : foo.y
$(run-yacc)
ÎÒÃÇ¿ÉÒÔ¿´¼û£¬ÒªÊ¹ÓÃÕâ¸öÃüÁî°ü£¬ÎÒÃǾͺÃÏñʹÓñäÁ¿Ò»Ñù¡£ÔÚÕâ¸öÃüÁî°üµÄʹÓÃÖУ¬Ãü
Áî°ü¡°run-yacc¡±Öеġ°$^¡±¾ÍÊÇ¡°foo.y¡±£¬¡°$@¡±¾ÍÊÇ¡°foo.c¡±£¨ÓйØÕâÖÖÒÔ¡°$¡±¿ª
Í·µÄÌØÊâ±äÁ¿£¬ÎÒÃÇ»áÔÚºóÃæ½éÉÜ£©£¬makeÔÚÖ´ÐÐÃüÁî°üʱ£¬ÃüÁî°üÖеÄÿ¸öÃüÁî»á±»ÒÀ´Î
¶ÀÁ¢Ö´ÐС£



Æß
ʹÓñäÁ¿
--------
ÔÚMakefileÖеĶ¨ÒåµÄ±äÁ¿£¬¾ÍÏñÊÇC/C++ÓïÑÔÖеĺêÒ»Ñù£¬Ëû´ú±íÁËÒ»¸öÎı¾×Ö´®£¬ÔÚMak
efileÖÐÖ´ÐеÄʱºòÆä»á×Ô¶¯Ô­Ä£Ô­ÑùµØÕ¹¿ªÔÚËùʹÓõĵط½¡£ÆäÓëC/C++Ëù²»Í¬µÄÊÇ£¬Äã¿É
ÒÔÔÚMakefileÖиıäÆäÖµ¡£ÔÚMakefileÖУ¬±äÁ¿¿ÉÒÔʹÓÃÔÚ¡°Ä¿±ê¡±£¬¡°ÒÀÀµÄ¿±ê¡±£¬¡°Ãü
Á»òÊÇMakefileµÄÆäËü²¿·ÖÖС£
±äÁ¿µÄÃüÃû×Ö¿ÉÒÔ°üº¬×Ö·û¡¢Êý×Ö£¬Ï»®Ïߣ¨¿ÉÒÔÊÇÊý×Ö¿ªÍ·£©£¬µ«²»Ó¦¸Ãº¬ÓС°:¡±¡¢¡°
#¡±¡¢¡°=¡±»òÊÇ¿Õ×Ö·û£¨¿Õ¸ñ¡¢»Ø³µµÈ£©¡£±äÁ¿ÊÇ´óСдÃô¸ÐµÄ£¬¡°foo¡±¡¢¡°Foo¡±ºÍ¡°FO
O¡±ÊÇÈý¸ö²»Í¬µÄ±äÁ¿Ãû¡£´«Í³µÄMakefileµÄ±äÁ¿ÃûÊÇÈ«´óдµÄÃüÃû·½Ê½£¬µ«ÎÒÍƼöʹÓôó
Сд´îÅäµÄ±äÁ¿Ãû£¬È磺MakeFlags¡£ÕâÑù¿ÉÒÔ±ÜÃâºÍϵͳµÄ±äÁ¿³åÍ»£¬¶ø·¢ÉúÒâÍâµÄÊÂÇé
¡£
ÓÐһЩ±äÁ¿ÊǺÜÆæ¹Ö×Ö´®£¬Èç¡°$<¡±¡¢¡°$@¡±µÈ£¬ÕâЩÊÇ×Ô¶¯»¯±äÁ¿£¬ÎÒ»áÔÚºóÃæ½éÉÜ¡£
Ò»¡¢±äÁ¿µÄ»ù´¡
±äÁ¿ÔÚÉùÃ÷ʱÐèÒª¸øÓè³õÖµ£¬¶øÔÚʹÓÃʱ£¬ÐèÒª¸øÔÚ±äÁ¿ÃûÇ°¼ÓÉÏ¡°$¡±·ûºÅ£¬µ«×îºÃÓÃС
À¨ºÅ¡°£¨£©¡±»òÊÇ´óÀ¨ºÅ¡°{}¡±°Ñ±äÁ¿¸ø°üÀ¨ÆðÀ´¡£Èç¹ûÄãҪʹÓÃÕæʵµÄ¡°$¡±×Ö·û£¬ÄÇô
ÄãÐèÒªÓá°$$¡±À´±íʾ¡£
±äÁ¿¿ÉÒÔʹÓÃÔÚÐí¶àµØ·½£¬Èç¹æÔòÖеġ°Ä¿±ê¡±¡¢¡°ÒÀÀµ¡±¡¢¡°ÃüÁÒÔ¼°ÐµıäÁ¿ÖС£ÏÈ
¿´Ò»¸öÀý×Ó£º
objects = program.o foo.o utils.o
program : $(objects)
cc -o program $(objects)
$(objects) : defs.h
±äÁ¿»áÔÚʹÓÃËüµÄµØ·½¾«È·µØÕ¹¿ª£¬¾ÍÏñC/C++ÖеĺêÒ»Ñù£¬ÀýÈ磺
foo = c
prog.o : prog.$(foo)
$(foo)$(foo) -$(foo) prog.$(foo)
Õ¹¿ªºóµÃµ½£º
prog.o : prog.c
cc -c prog.c
µ±È»£¬Ç§Íò²»ÒªÔÚÄãµÄMakefileÖÐÕâÑù¸É£¬ÕâÀïÖ»ÊǾٸöÀý×ÓÀ´±íÃ÷MakefileÖеıäÁ¿ÔÚʹ
Óô¦Õ¹¿ªµÄÕæʵÑù×Ó¡£¿É¼ûÆä¾ÍÊÇÒ»¸ö¡°Ìæ´ú¡±µÄÔ­Àí¡£
ÁíÍ⣬¸ø±äÁ¿¼ÓÉÏÀ¨ºÅÍêÈ«ÊÇΪÁ˸ü¼Ó°²È«µØʹÓÃÕâ¸ö±äÁ¿£¬ÔÚÉÏÃæµÄÀý×ÓÖУ¬Èç¹ûÄã²»Ïë
¸ø±äÁ¿¼ÓÉÏÀ¨ºÅ£¬ÄÇÒ²¿ÉÒÔ£¬µ«ÎÒ»¹ÊÇÇ¿ÁÒ½¨ÒéÄã¸ø±äÁ¿¼ÓÉÏÀ¨ºÅ¡£

¶þ¡¢±äÁ¿ÖеıäÁ¿
ÔÚ¶¨Òå±äÁ¿µÄֵʱ£¬ÎÒÃÇ¿ÉÒÔʹÓÃÆäËü±äÁ¿À´¹¹Ôì±äÁ¿µÄÖµ£¬ÔÚMakefileÖÐÓÐÁ½ÖÖ·½Ê½À´ÔÚ
ÓñäÁ¿¶¨Òå±äÁ¿µÄÖµ¡£
ÏÈ¿´µÚÒ»ÖÖ·½Ê½£¬Ò²¾ÍÊǼòµ¥µÄʹÓá°=¡±ºÅ£¬ÔÚ¡°=¡±×ó²àÊDZäÁ¿£¬ÓÒ²àÊDZäÁ¿µÄÖµ£¬ÓÒ²à
±äÁ¿µÄÖµ¿ÉÒÔ¶¨ÒåÔÚÎļþµÄÈκÎÒ»´¦£¬Ò²¾ÍÊÇ˵£¬ÓÒ²àÖеıäÁ¿²»Ò»¶¨·ÇÒªÊÇÒѶ¨ÒåºÃµÄÖµ
£¬ÆäÒ²¿ÉÒÔʹÓúóÃ涨ÒåµÄÖµ¡£È磺
foo = $(bar)
bar = $(ugh)
ugh = Huh?
all:
echo $(foo)
ÎÒÃÇÖ´ÐС°make all¡±½«»á´ò³ö±äÁ¿$(foo)µÄÖµÊÇ¡°Huh?¡±£¨ $(foo)µÄÖµÊÇ$(bar)£¬$(bar
)µÄÖµÊÇ$(ugh)£¬$(ugh)µÄÖµÊÇ¡°Huh?¡±£©¿É¼û£¬±äÁ¿ÊÇ¿ÉÒÔʹÓúóÃæµÄ±äÁ¿À´¶¨ÒåµÄ¡£
Õâ¸ö¹¦ÄÜÓкõĵط½£¬Ò²Óв»ºÃµÄµØ·½£¬ºÃµÄµØ·½ÊÇ£¬ÎÒÃÇ¿ÉÒ԰ѱäÁ¿µÄÕæʵֵÍƵ½ºóÃæÀ´
¶¨Ò壬È磺
CFLAGS = $(include_dirs) -O
include_dirs = -Ifoo -Ibar
µ±¡°CFLAGS¡±ÔÚÃüÁîÖб»Õ¹¿ªÊ±£¬»áÊÇ¡°-Ifoo -Ibar -O¡±¡£µ«ÕâÖÖÐÎʽҲÓв»ºÃµÄµØ·½£¬
ÄǾÍÊǵݹ鶨Ò壬È磺
CFLAGS = $(CFLAGS) -O
»ò£º
A = $(B)
B = $(A)
Õâ»áÈÃmakeÏÝÈëÎÞÏ޵ıäÁ¿Õ¹¿ª¹ý³ÌÖÐÈ¥£¬µ±È»£¬ÎÒÃǵÄmakeÊÇÓÐÄÜÁ¦¼ì²âÕâÑùµÄ¶¨Ò壬²¢
»á±¨´í¡£»¹ÓоÍÊÇÈç¹ûÔÚ±äÁ¿ÖÐʹÓú¯Êý£¬ÄÇô£¬ÕâÖÖ·½Ê½»áÈÃÎÒÃǵÄmakeÔËÐÐʱ·Ç³£Âý£¬
¸üÔã¸âµÄÊÇ£¬Ëû»áʹÓõÃÁ½¸ömakeµÄº¯Êý¡°wildcard¡±ºÍ¡°shell¡±·¢Éú²»¿ÉÔ¤ÖªµÄ´íÎó¡£
ÒòΪÄã²»»áÖªµÀÕâÁ½¸öº¯Êý»á±»µ÷ÓöàÉٴΡ£
ΪÁ˱ÜÃâÉÏÃæµÄÕâÖÖ·½·¨£¬ÎÒÃÇ¿ÉÒÔʹÓÃmakeÖеÄÁíÒ»ÖÖÓñäÁ¿À´¶¨Òå±äÁ¿µÄ·½·¨¡£ÕâÖÖ·½
·¨Ê¹ÓõÄÊÇ¡°:=¡±²Ù×÷·û£¬È磺
x := foo
y := $(x) bar
x := later
ÆäµÈ¼ÛÓÚ£º
y := foo bar
x := later
ÖµµÃÒ»ÌáµÄÊÇ£¬ÕâÖÖ·½·¨£¬Ç°ÃæµÄ±äÁ¿²»ÄÜʹÓúóÃæµÄ±äÁ¿£¬Ö»ÄÜʹÓÃÇ°ÃæÒѶ¨ÒåºÃÁ˵ıä
Á¿¡£Èç¹ûÊÇÕâÑù£º
y := $(x) bar
x := foo
ÄÇô£¬yµÄÖµÊÇ¡°bar¡±£¬¶ø²»ÊÇ¡°foo bar¡±¡£
ÉÏÃ涼ÊÇһЩ±È½Ï¼òµ¥µÄ±äÁ¿Ê¹ÓÃÁË£¬ÈÃÎÒÃÇÀ´¿´Ò»¸ö¸´ÔÓµÄÀý×Ó£¬ÆäÖаüÀ¨ÁËmakeµÄº¯Êý
¡¢Ìõ¼þ±í´ïʽºÍÒ»¸öϵͳ±äÁ¿¡°MAKELEVEL¡±µÄʹÓãº
ifeq (0,${MAKELEVEL})
cur-dir := $(shell pwd)
whoami := $(shell whoami)
host-type := $(shell arch)
MAKE := ${MAKE} host-type=${host-type} whoami=${whoami}
endif
¹ØÓÚÌõ¼þ±í´ïʽºÍº¯Êý£¬ÎÒÃÇÔÚºóÃæÔÙ˵£¬¶ÔÓÚϵͳ±äÁ¿¡°MAKELEVEL¡±£¬ÆäÒâ˼ÊÇ£¬Èç¹û
ÎÒÃǵÄmakeÓÐÒ»¸öǶÌ×Ö´ÐеĶ¯×÷£¨²Î¼ûÇ°ÃæµÄ¡°Ç¶Ì×ʹÓÃmake¡±£©£¬ÄÇô£¬Õâ¸ö±äÁ¿»á¼Ç
¼ÁËÎÒÃǵĵ±Ç°MakefileµÄµ÷ÓòãÊý¡£
ÏÂÃæÔÙ½éÉÜÁ½¸ö¶¨Òå±äÁ¿Ê±ÎÒÃÇÐèÒªÖªµÀµÄ£¬ÇëÏÈ¿´Ò»¸öÀý×Ó£¬Èç¹ûÎÒÃÇÒª¶¨ÒåÒ»¸ö±äÁ¿£¬
ÆäÖµÊÇÒ»¸ö¿Õ¸ñ£¬ÄÇôÎÒÃÇ¿ÉÒÔÕâÑùÀ´£º
nullstring :=
space := $(nullstring) # end of the line
nullstringÊÇÒ»¸öEmpty±äÁ¿£¬ÆäÖÐʲôҲûÓУ¬¶øÎÒÃǵÄspaceµÄÖµÊÇÒ»¸ö¿Õ¸ñ¡£ÒòΪÔÚ²Ù
×÷·ûµÄÓÒ±ßÊǺÜÄÑÃèÊöÒ»¸ö¿Õ¸ñµÄ£¬ÕâÀï²ÉÓõļ¼ÊõºÜ¹ÜÓã¬ÏÈÓÃÒ»¸öEmpty±äÁ¿À´±êÃ÷±ä
Á¿µÄÖµ¿ªÊ¼ÁË£¬¶øºóÃæ²ÉÓá°#¡±×¢ÊÍ·ûÀ´±íʾ±äÁ¿¶¨ÒåµÄÖÕÖ¹£¬ÕâÑù£¬ÎÒÃÇ¿ÉÒÔ¶¨Òå³öÆä
ÖµÊÇÒ»¸ö¿Õ¸ñµÄ±äÁ¿¡£Çë×¢ÒâÕâÀï¹ØÓÚ¡°#¡±µÄʹÓã¬×¢ÊÍ·û¡°#¡±µÄÕâÖÖÌØÐÔÖµµÃÎÒÃÇ×¢Òâ
£¬Èç¹ûÎÒÃÇÕâÑù¶¨ÒåÒ»¸ö±äÁ¿£º
dir := /foo/bar # directory to put the frobs in
dirÕâ¸ö±äÁ¿µÄÖµÊÇ¡°/foo/bar¡±£¬ºóÃ滹¸úÁË4¸ö¿Õ¸ñ£¬Èç¹ûÎÒÃÇÕâÑùʹÓÃÕâÑù±äÁ¿À´Ö¸¶¨
±ðµÄĿ¼----¡°$(dir)/file¡±ÄÇô¾ÍÍêµ°ÁË¡£
»¹ÓÐÒ»¸ö±È½ÏÓÐÓõIJÙ×÷·ûÊÇ¡°?=¡±£¬ÏÈ¿´Ê¾Àý£º
FOO ?= bar
Æ京ÒåÊÇ£¬Èç¹ûFOOûÓỶ¨Òå¹ý£¬ÄÇô±äÁ¿FOOµÄÖµ¾ÍÊÇ¡°bar¡±£¬Èç¹ûFOOÏÈÇ°±»¶¨Òå¹ý£¬
ÄÇôÕâÌõÓォʲôҲ²»×ö£¬ÆäµÈ¼ÛÓÚ£º
ifeq ($(origin FOO), undefined)
FOO = bar
endif

Èý¡¢±äÁ¿¸ß¼¶Ó÷¨
ÕâÀï½éÉÜÁ½ÖÖ±äÁ¿µÄ¸ß¼¶Ê¹Ó÷½·¨£¬µÚÒ»ÖÖÊDZäÁ¿ÖµµÄÌæ»»¡£
ÎÒÃÇ¿ÉÒÔÌæ»»±äÁ¿ÖеĹ²ÓеIJ¿·Ö£¬Æä¸ñʽÊÇ¡°$(var:a=b)¡±»òÊÇ¡°${var:a=b}¡±£¬ÆäÒâ˼
ÊÇ£¬°Ñ±äÁ¿¡°var¡±ÖÐËùÓÐÒÔ¡°a¡±×Ö´®¡°½áβ¡±µÄ¡°a¡±Ìæ»»³É¡°b¡±×Ö´®¡£ÕâÀïµÄ¡°½áβ¡±
Òâ˼ÊÇ¡°¿Õ¸ñ¡±»òÊÇ¡°½áÊø·û¡±¡£
»¹ÊÇ¿´Ò»¸öʾÀý°É£º
foo := a.o b.o c.o
bar := $(foo:.o=.c)
Õâ¸öʾÀýÖУ¬ÎÒÃÇÏȶ¨ÒåÁËÒ»¸ö¡°$(foo)¡±±äÁ¿£¬¶øµÚ¶þÐеÄÒâ˼ÊÇ°Ñ¡°$(foo)¡±ÖÐËùÓÐÒÔ
¡°.o¡±×Ö´®¡°½áβ¡±È«²¿Ìæ»»³É¡°.c¡±£¬ËùÒÔÎÒÃǵġ°$(bar)¡±µÄÖµ¾ÍÊÇ¡°a.c b.c c.c¡±
¡£
ÁíÍâÒ»ÖÖ±äÁ¿Ìæ»»µÄ¼¼ÊõÊÇÒÔ¡°¾²Ì¬Ä£Ê½¡±£¨²Î¼ûÇ°ÃæÕ½ڣ©¶¨ÒåµÄ£¬È磺
foo := a.o b.o c.o
bar := $(foo:%.o=%.c)
ÕâÒÀÀµÓÚ±»Ìæ»»×Ö´®ÖеÄÓÐÏàͬµÄģʽ£¬Ä£Ê½ÖбØÐë°üº¬Ò»¸ö¡°%¡±×Ö·û£¬Õâ¸öÀý×ÓͬÑùÈÃ$
(bar)±äÁ¿µÄֵΪ¡°a.c b.c c.c¡±¡£
µÚ¶þÖָ߼¶Ó÷¨ÊÇ----¡°°Ñ±äÁ¿µÄÖµÔÙµ±³É±äÁ¿¡±¡£ÏÈ¿´Ò»¸öÀý×Ó£º
x = y
y = z
a := $($(x))
ÔÚÕâ¸öÀý×ÓÖУ¬$(x)µÄÖµÊÇ¡°y¡±£¬ËùÒÔ$($(x))¾ÍÊÇ$(y)£¬ÓÚÊÇ$(a)µÄÖµ¾ÍÊÇ¡°z¡±¡££¨×¢
Ò⣬ÊÇ¡°x=y¡±£¬¶ø²»ÊÇ¡°x=$(y)¡±£©
ÎÒÃÇ»¹¿ÉÒÔʹÓøü¶àµÄ²ã´Î£º
x = y
y = z
z = u
a := $($($(x)))
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 2 ÌõÆÀÂÛ

  1. taotie2007 ÓÚ 2008-02-05 14:42:33·¢±í:

    Ôõô¿´²»ÁË°¡(5ty( (5ty( (5ty(

  2. @naigo@ ÓÚ 2006-10-19 15:55:07·¢±í:

    ²»´íµÄÌû×Ó£¡