--------------------------------------------------------------------------------
C-Scene ÌâÄ¿ #2
¶àÎļþÏîÄ¿ºÍ GNU Make ¹¤¾ß
×÷Õß: ÇÇÖθ»ÌØ (Goerge Foot)
µç×ÓÓʼþ: george.foot@merton.ox.ac.uk
Occupation: Student at Merton College, Oxford University, England
Ö°Òµ:ѧÉú£¬Ä¬¶û¶ÙѧԺ,Å£½ò³Ç´óѧ,Ó¢¸ñÀ¼
IRCÄäÃû: gfoot
--------------------------------------------------------------------------------
¾Ü¾ø³Ðŵ£º×÷Õ߶ÔÓÚÈκÎÒò´Ë¶ø¶ÔÈκÎÊÂÎïÔì³ÉµÄËùÓÐË𺦣¨ÄãËùÓµÓлò²» ÓµÓеÄʵ¼ÊµÄ£¬³éÏóµÄ£¬»òÕßÐéÄâµÄ£©¡£ËùÓеÄË𻵶¼ÊÇÄã×Ô¼ºµÄÔðÈΣ¬¶øÓëÎÒÎ޹ء£
ËùÓÐȨ£º ¡°¶àÎļþÏîÄ¿¡±²¿·ÖÊôÓÚ×÷ÕߵIJƲú£¬°æȨ¹éÇÇÖθ»ÌØ£±£¹£¹£·Äê ÎåÔÂÖÁÆßÔ¡£ÆäËü²¿·ÖÊô CScene ²Æ²ú£¬°æȨ CScene £±£¹£¹£·Ä꣬±£ÁôËùÓÐ °æȨ¡£±¾ CScene ÎÄÕµķַ¢£¬²¿·Ö»òÈ«²¿£¬Ó¦ÒÀÕÕËùÓÐÆäËü CScene µÄÎÄÕ µÄÌõ¼þÀ´´¦Àí¡£
0) ½éÉÜ
~~~~~~~~~~~~~~~
±¾ÎĽ«Ê×ÏȽéÉÜΪʲôҪ½«ÄãµÄ£ÃÔ´´úÂë·ÖÀë³É¼¸¸öºÏÀíµÄ¶ÀÁ¢µµ°¸£¬Ê²Ã´Ê± ºòÐèÒª·Ö£¬Ôõô²ÅÄֵܷĺá£È»ºó½«»á¸æËßÄã GNU Make ÔõÑùʹÄãµÄ±àÒëºÍÁ¬ ½Ó²½Öè×Ô¶¯»¯¡£¶ÔÓÚÆäËü Make ¹¤¾ßµÄÓû§À´Ëµ£¬ËäÈ»ÔÚÓÃÆäËüÀàËƹ¤¾ßʱҪ×öÊʵ±µÄµ÷Õû£¬±¾ÎĵÄÄÚÈÝÈÔÈ»ÊǷdz£ÓÐÓõġ£Èç¹û¶ÔÄã×Ô¼ºµÄ±à³Ì¹¤¾ßÓл³ ÒÉ£¬¿ÉÒÔʵ¼ÊµÄÊÔÒ»ÊÔ£¬µ«ÇëÏÈÔĶÁÓû§Êֲᡣ
1) ¶àÎļþÏîÄ¿
~~~~~~~~~~~~~~~~~~~~~~
1.1ΪʲôʹÓÃËüÃÇ?
Ê×ÏÈ£¬¶àÎļþÏîÄ¿µÄºÃ´¦ÔÚÄÇÀïÄØ£¿
ËüÃÇ¿´ÆðÀ´°ÑÊÂÇéŪµÄ¸´ÔÓÎޱȡ£ÓÖÒª header Îļþ£¬ÓÖÒª extern ÉùÃ÷£¬¶øÇÒÈç¹ûÐèÒª²éÕÒÒ»¸öÎļþ£¬ÄãÒªÔÚ¸ü¶àµÄÎļþÀïËÑË÷¡£
µ«ÆäʵÎÒÃÇÓкÜÓÐÁ¦µÄÀíÓÉÖ§³ÖÎÒÃÇ°ÑÒ»¸öÏîÄ¿·Ö½â³ÉС¿é¡£µ±Äã¸Ä¶¯Ò»ÐдúÂ룬±àÒëÆ÷ÐèҪȫ²¿ÖØбàÒëÀ´Éú³ÉÒ»¸öеĿÉÖ´ÐÐÎļþ¡£µ«Èç¹ûÄãµÄÏîÄ¿ÊÇ·Ö¿ªÔÚ¼¸¸öСÎļþÀµ±Äã¸Ä¶¯ÆäÖÐÒ»¸öÎļþµÄʱºò£¬±ðµÄÔ´ÎļþµÄÄ¿±êÎļþ(object files)ÒѾ´æÔÚ£¬ËùÒÔûÓÐʲôÔÒòÈ¥ÖØбàÒëËüÃÇ¡£ÄãËùÐèÒª×öµÄÖ»ÊÇÖØÏÖ±àÒë±»¸Ä¶¯¹ýµÄÄǸöÎļþ£¬È»ºóÖØÐÂÁ¬½ÓËùÓеÄÄ¿±êÎļþ°ÕÁË¡£ÔÚ´óÐ͵ÄÏîÄ¿ÖУ¬ÕâÒâζ×ŴӺܳ¤µÄ£¨¼¸·ÖÖÓµ½¼¸Ð¡Ê±£©ÖØбàÒëËõ¶ÌΪʮ¼¸£¬¶þÊ®¼¸ÃëµÄ¼òµ¥µ÷Õû¡£
ֻҪͨ¹ý»ù±¾µÄ¹æ»®£¬½«Ò»¸öÏîÄ¿·Ö½â³É¶à¸öСÎļþ¿ÉʹÄã¸ü¼ÓÈÝÒ×µÄÕÒµ½Ò»¶Î´úÂë¡£ºÜ¼òµ¥£¬Äã¸ù¾Ý´úÂëµÄ×÷ÓðÑÄãµÄ´úÂë·Ö½âµ½²»Í¬µÄÎļþÀï¡£µ±ÄãÒª¿´Ò»¶Î´úÂëʱ£¬Äã¿ÉÒÔ׼ȷµÄÖªµÀÔÚÄǸöÎļþÖÐȥѰÕÒËü¡£
´ÓºÜ¶àÄ¿±êÎļþÉú³ÉÒ»¸ö³ÌÐò°ü (Library)±È´ÓÒ»¸öµ¥Ò»µÄ´óÄ¿±êÎļþÉú³ÉÒªºÃµÄ¶à¡£µ±È»Êµ¼ÊÉÏÕâÊÇ·ñÕæÊÇÒ»¸öÓÅÊÆÔòÊÇÓÉÄãËùÓõÄϵͳÀ´¾ö¶¨µÄ¡£µ«Êǵ±Ê¹Óà gcc/ld (Ò»¸ö GNU C ±àÒ룯Á¬½ÓÆ÷) °ÑÒ»¸ö³ÌÐò°üÁ¬½Óµ½Ò»¸ö³ÌÐòʱ£¬ÔÚÁ¬½ÓµÄ¹ý³ÌÖУ¬Ëü»á³¢ÊÔ²»È¥Á¬½ÓûÓÐʹÓõ½µÄ²¿·Ö¡£µ«Ëüÿ´ÎÖ»ÄÜ´Ó³ÌÐò°üÖаÑÒ»¸öÍêÕûµÄÄ¿±êÎļþÅųýÔÚÍâ¡£Òò´ËÈç¹ûÄã²Î¿¼Ò»¸ö³ÌÐò°üÖÐijһ¸öÄ¿±êµµÖÐÈκÎÒ»¸ö·ûºÅµÄ»°£¬ÄÇôÕâ¸öÄ¿±êÎļþÕû¸ö¶¼»á±»Á¬½Ó½øÀ´¡£ÒªÊÇÒ»¸ö³ÌÐò°ü±»·Ç³£³ä·ÖµÄ·Ö½âÁ˵Ļ°£¬ÄÇô¾Á¬½Óºó£¬µÃµ½µÄ¿ÉÖ´ÐÐÎļþ»á±È´ÓÒ»¸ö´óÄ¿±êÎļþ×é³ÉµÄ³ÌÐò°üÁ¬½ÓµÃµ½µÄÎļþСµÃ¶à¡£
ÓÖÒòΪÄãµÄ³ÌÐòÊǺÜÄ£¿é»¯µÄ£¬ÎļþÖ®¼äµÄ¹²Ïí²¿·Ö±»¼õµ½×îÉÙ£¬ÄǾÍÓкܶàºÃ´¦----¿ÉÒÔºÜÈÝÒ×µÄ×·×Ùµ½³ô³æ£¬ÕâЩģ¿é¾³£ÊÇ¿ÉÒÔÓÃÔÚÆäËüµÄÏîÄ¿ÀïµÄ£¬Í¬Ê±±ðÈËÒ²¿ÉÒÔ¸üÈÝÒ×µÄÀí½âÄãµÄÒ»¶Î´úÂëÊǸÉʲôµÄ¡£µ±È»´ËÍ⻹ÓÐÐí¶à±ðµÄºÃ´¦¡¡
1.2 ºÎʱ·Ö½âÄãµÄÏîÄ¿
ºÜÃ÷ÏÔ£¬°ÑÈκζ«Î÷¶¼·Ö½âÊDz»ºÏÀíµÄ¡£Ïó¡°ÊÀ½ç£¬ÄãÃǺá±ÕâÑùµÄ¼òµ¥³ÌÐò¸ù±¾¾Í²»ÄÜ·Ö£¬ÒòΪʵÔÚҲûʲô¿É·ÖµÄ¡£°ÑÓÃÓÚ²âÊÔÓõÄС³ÌÐò·Ö½âÒ²ÊÇûʲôÒâ˼µÄ¡£µ«Ò»°ãÀ´Ëµ£¬µ±·Ö½âÏîÄ¿ÓÐÖúÓÚ²¼¾Ö¡¢·¢Õ¹ºÍÒ׶ÁÐÔµÄʱºò£¬ÎÒ¶¼»á²ÉÈ¡Ëü¡£ÔÚ´ó¶àÊýµÄÇé¿öÏ£¬ÕⶼÊÇÊÊÓõġ££¨Ëùν¡°ÊÀ½ç£¬ÄãÃǺá±£¬¼È 'hello world' £¬Ö»ÊÇÒ»¸ö½éÉÜÒ»ÖÖ±à³ÌÓïÑÔʱ¹ßÓõķ¶Àý³ÌÐò£¬Ëü»áÔÚÆÁÄ»ÉÏÏÔʾһÐÐ 'hello world' ¡£ÊÇ×î¼òµ¥µÄ³ÌÐò¡££©
Èç¹ûÄãÐèÒª¿ª·¢Ò»¸öÏ൱´óµÄÏîÄ¿£¬ÔÚ¿ªÊ¼Ç°£¬Ó¦¸Ã¿¼ÂÇÒ»ÏÂÄ㽫ÈçºÎʵÏÖËü£¬²¢ÇÒÉú³É¼¸¸öÎļþ£¨ÓÃÊʵ±µÄÃû×Ö£©À´·ÅÄãµÄ´úÂë¡£µ±È»£¬ÔÚÄãµÄÏîÄ¿¿ª·¢µÄ¹ý³ÌÖУ¬Äã¿ÉÒÔ½¨Á¢ÐµÄÎļþ£¬µ«Èç¹ûÄãÕâô×öµÄ»°£¬ËµÃ÷Äã¿ÉÄܸıäÁ˵±³õµÄÏë·¨£¬ÄãÓ¦¸ÃÏëÏëÊÇ·ñÐèÒª¶ÔÕûÌå½á¹¹Ò²½øÐÐÏàÓ¦µÄµ÷Õû¡£
¶ÔÓÚÖÐÐ͵ÄÏîÄ¿£¬Ä㵱ȻҲ¿ÉÒÔ²ÉÓÃÉÏÊö¼¼ÇÉ£¬µ«ÄãÒ²¿ÉÒÔ¾ÍÄÇô¿ªÊ¼ÊäÈëÄãµÄ´úÂ룬µ±ÄãµÄÂë¶àµ½ÄÑÒÔ¹ÜÀíµÄʱºòÔÙ°ÑËüÃÇ·Ö½â³É²»Í¬µÄµµ°¸¡£µ«ÒÔÎҵľÑéÀ´Ëµ£¬¿ªÊ¼Ê±ÔÚÄÔ×ÓÀïÐγÉÒ»¸ö´ó¸ÅµÄ·½°¸£¬²¢ÇÒ¾¡Á¿×ñ´ÓËü£¬»òÔÚ¿ª·¢¹ý³ÌÖУ¬Ëæ×ųÌÐòµÄÐèÒª¶øÐ޸ģ¬»áʹ¿ª·¢±äµÃ¸ü¼ÓÈÝÒס£
1.3 ÔõÑù·Ö½âÏîÄ¿
ÏÈ˵Ã÷£¬ÕâÍêÈ«ÊÇÎÒ¸öÈ˵ÄÒâ¼û£¬Äã¿ÉÒÔ£¨Ò²ÐíÄãÕæµÄ»á£¿£©ÓñðµÄ·½Ê½À´×ö¡£Õâ»á´¥¶¯µ½ÓйرàÂë·ç¸ñµÄÎÊÌ⣬¶ø´ó¼Ò´ÓÀ´¾ÍûÓÐÍ£Ö¹¹ýÔÚÕâ¸öÎÊÌâÉϵÄÕùÂÛ¡£ÔÚÕâÀïÎÒÖ»ÊǸø³öÎÒ×Ô¼ºÏ²»¶µÄ×ö·¨£¨Í¬Ê±Ò²¸ø³öÕâô×öµÄÔÒò£©£º
i) ²»ÒªÓÃÒ»¸ö header ÎļþÖ¸Ïò¶à¸öÔ´ÂëÎļþ£¨ÀýÍ⣺³ÌÐò°ü µÄ header Îļþ£©¡£ÓÃÒ»¸ö header¶¨ÒåÒ»¸öÔ´ÂëÎļþµÄ·½Ê½ »á¸üÓÐЧ£¬Ò²¸üÈÝÒײéÑ°¡£·ñÔò¸Ä±äÒ»¸öÔ´ÎļþµÄ½á¹¹£¨²¢ÇÒ ËüµÄ header Îļþ£©¾Í±ØÐëÖØбàÒëºÃ¼¸¸öÎļþ¡£
ii) Èç¹û¿ÉÒԵĻ°£¬ÍêÈ«¿ÉÒÔÓó¬¹ýÒ»¸öµÄ header ÎļþÀ´Ö¸Ïòͬ Ò»¸öÔ´ÂëÎļþ¡£ÓÐʱ½«²»¿É¹«¿ªµ÷Óõĺ¯ÊýÔÐÍ£¬ÀàÐͶ¨ÒåµÈµÈ£¬´ÓËüÃǵģÃÔ´ÂëÎļþÖзÖÀë³öÀ´ÊǷdz£ÓÐÓõġ£Ê¹ÓÃÒ» ¸ö header Îļþ×°¹«¿ª·ûºÅ£¬ÓÃÁíÒ»¸öװ˽ÈË·ûºÅÒâζ×ÅÈç¹ûÄã¸Ä±äÁËÕâ¸öÔ´ÂëÎļþµÄÄÚ²¿½á¹¹£¬Äã¿ÉÒÔÖ»ÊÇÖØбàÒëËü¶ø ²»ÐèÒªÖØбàÒëÄÇЩʹÓÃËüµÄ¹«¿ª header ÎļþµÄÆäËüµÄÔ´Îļþ¡£
iii) ²»ÒªÔÚ¶à¸ö header ÎļþÖÐÖظ´¶¨ÒåÐÅÏ¢¡£ Èç¹ûÐèÒª£¬ ÔÚÆäÖÐÒ»¸ö header ÎļþÀï #include ÁíÒ»¸ö£¬µ«ÊDz»ÒªÖظ´ÊäÈëÏàͬµÄ header ÐÅÏ¢Á½´Î¡£ÔÒòÊÇÈç¹ûÄãÒÔºó¸Ä ±äÁËÕâ¸öÐÅÏ¢£¬ÄãÖ»ÐèÒª°ÑËü¸Ä±äÒ»´Î£¬²»ÓÃËÑË÷²¢¸Ä±äÁíÍâÒ»¸öÖظ´µÄÐÅÏ¢¡£
iv) ÔÚÿһ¸öÔ´ÂëÎļþÀ #include ÄÇЩÉùÃ÷ÁËÔ´ÂëÎļþÖеķû ºÅµÄËùÓÐ header Îļþ¡£ÕâÑùÒ»À´£¬ÄãÔÚÔ´ÂëÎļþºÍ header Îļþ¶ÔijЩº¯Êý×ö³öµÄì¶ÜÉùÃ÷¿ÉÒԱȽÏÈÝÒ׵ı»±àÒëÆ÷·¢ÏÖ¡£
1.4 ¶ÔÓÚ³£¼û´íÎóµÄ×¢ÊÍ
a) ¶¨Òå·û (Identifier) ÔÚÔ´ÂëÎļþÖеÄì¶Ü£ºÔÚ£ÃÀ±äÁ¿ºÍº¯ÊýµÄȱ ʡ״̬Êǹ«Óõġ£Òò´Ë£¬ÈκΣÃÔ´Âëµµ°¸¶¼¿ÉÒÔÒýÓôæÔÚÓÚÆäËüÔ´ ÂëµµÖеÄͨÓà (global) º¯ÊýºÍͨÓñäÁ¿£¬¼ÈʹÕâ¸öµµ°¸Ã»ÓÐÄǸö±ä Á¿»òº¯ÊýµÄÉùÃ÷»òÔÐÍ¡£Òò´ËÄã±ØÐë±£Ö¤ÔÚ²»Í¬µÄÁ½¸öµµ°¸Àï²»ÄÜÓÃͬһ¸ö·ûºÅÃû³Æ£¬·ñÔò»áÓÐÁ¬½Ó´íÎó»òÕßÔÚ±àÒëʱ»áÓо¯¸æ¡£
Ò»ÖÖ±ÜÃâÕâÖÖ´íÎóµÄ·½·¨ÊÇÔÚ¹«ÓõķûºÅÇ°¼ÓÉϸúÆäËùÔÚÔ´ÎļþÓÐ ¹ØµÄǰ׺¡£±ÈÈ磺ËùÓÐÔÚ gfx.c ÀïµÄº¯Êý¶¼¼ÓÉÏǰ׺¡°gfx_¡±¡£Èç¹ûÄãºÜСÐĵķֽâÄãµÄ³ÌÐò£¬Ê¹ÓÃÓÐÒâÒåµÄº¯ÊýÃû³Æ£¬²¢ÇÒ²»Êǹý·Ö ʹÓÃͨÓñäÁ¿£¬µ±È»Õâ¸ù±¾¾Í²»ÊÇÎÊÌâ¡£
Òª·ÀÖ¹Ò»¸ö·ûºÅÔÚËü±»¶¨ÒåµÄÔ´ÎļþÒÔÍâ±»¿´µ½£¬¿ÉÔÚËüµÄ¶¨ÒåÇ° ¼ÓÉϹؼü×Ö¡°static¡±¡£Õâ¶ÔÖ»ÔÚÒ»¸öµµ°¸ÄÚ²¿Ê¹Óã¬ÆäËüµµ°¸¶¼¶¼²»»áÓõ½µÄ¼òµ¥º¯ÊýÊǺÜÓÐÓõġ£
b) ¶à´Î¶¨ÒåµÄ·ûºÅ£º header µµ»á±»Öð×ÖµÄÌæ»»µ½ÄãÔ´ÎļþÀï #include µÄλÖõġ£Òò´Ë£¬Èç¹û header µµ±» #include µ½Ò»¸öÒÔÉϵÄÔ´Îļþ ÀÕâ¸ö header µµÖÐËùÓеĶ¨Òå¾Í»á³öÏÖÔÚÿһ¸öÓйصÄÔ´ÂëÎļþÀï¡£Õâ»áʹËüÃÇÀïµÄ·ûºÅ±»¶¨ÒåÒ»´ÎÒÔÉÏ£¬´Ó¶ø³öÏÖÁ¬½Ó´íÎ󣨼û ÉÏ£©¡£
½â¾ö·½·¨£º ²»ÒªÔÚ header µµÀﶨÒå±äÁ¿¡£ÄãÖ»ÐèÒªÔÚ header µµÀïÉùÃ÷ËüÃÇÈ»ºóÔÚÊʵ±µÄ£ÃÔ´ÂëÎļþ£¨Ó¦¸Ã #include ÄǸö header µµµÄÄǸö£©ÀﶨÒåËüÃÇ£¨Ò»´Î£©¡£¶ÔÓÚ³õѧÕßÀ´Ëµ£¬¶¨ÒåºÍÉùÃ÷ÊǺÜÈÝÒ×»ìÏýµÄ¡£ÉùÃ÷µÄ×÷ÓÃÊǸæËß±àÒëÆ÷ÆäËùÉùÃ÷µÄ·ûºÅÓ¦¸Ã´æÔÚ£¬ ²¢ÇÒÒªÓÐËùÖ¸¶¨µÄÀàÐÍ¡£µ«ÊÇ£¬Ëü²¢²»»áʹ±àÒëÆ÷·ÖÅäÖü´æ¿Õ¼ä¡£¶ø¶¨ÒåµÄ×öÓÃÊÇÒªÇó±àÒëÆ÷·ÖÅäÖü´æ¿Õ¼ä¡£µ±×öÒ»¸öÉùÃ÷¶ø²»ÊÇ×ö ¶¨ÒåµÄʱºò£¬ÔÚÉùÃ÷Ç°·ÅÒ»¸ö¹Ø¼ü×Ö¡°extern¡±¡£
ÀýÈ磬ÎÒÃÇÓÐÒ»¸ö½Ð¡°counter¡±µÄ±äÁ¿£¬Èç¹ûÏëÈÃËü³ÉΪ¹«Óõģ¬ ÎÒÃÇÔÚÒ»¸öÔ´Âë³ÌÐò£¨Ö»ÔÚÒ»¸öÀïÃ棩µÄ¿ªÊ¼¶¨ÒåËü£º¡°int counter;¡±£¬ÔÙÔÚÏà¹ØµÄ header µµÀïÉùÃ÷Ëü£º¡°extern int counter;¡±¡£
º¯ÊýÔÐÍÀïÒþº¬×Å extern µÄÒâ˼£¬ËùÒÔ²»Ðè¹ËÂÇÕâ¸öÎÊÌâ¡£
c) Öظ´¶¨Ò壬Öظ´ÉùÃ÷£¬Ã¬¶ÜÀàÐÍ£º
Ç뿼ÂÇÈç¹ûÔÚÒ»¸ö£ÃÔ´ÂëÎļþÖÐ #include Á½¸öµµ a.h ºÍ b.h£¬ ¶ø a.h ÓÖ #include ÁË b.h µµ£¨ÔÒòÊÇ b.h µµ¶¨ÒåÁËһЩ a.h ÐèÒªµÄÀàÐÍ£©£¬»á·¢ÉúʲôÊÂÄØ£¿Õâʱ¸Ã£ÃÔ´ÂëÎļþ #include ÁË b.h Á½´Î¡£Òò´Ëÿһ¸öÔÚ b.h ÖÐµÄ #define ¶¼·¢ÉúÁËÁ½´Î£¬Ã¿Ò» ¸öÉùÃ÷·¢ÉúÁËÁ½´Î£¬µÈµÈ¡£ÀíÂÛÉÏ£¬ÒòΪËüÃÇÊÇÍêÈ«Ò»ÑùµÄ¿½±´£¬ËùÒÔÓ¦¸Ã²»»áÓÐʲôÎÊÌ⣬µ«ÔÚʵ¼ÊÓ¦ÓÃÉÏ£¬ÕâÊDz»·ûºÏ£ÃµÄÓï·¨ µÄ£¬¿ÉÄÜÔÚ±àÒëʱ³öÏÖ´íÎ󣬻òÖÁÉÙÊǾ¯¸æ¡£
½â¾öµÄ·½·¨ÊÇҪȷ¶¨Ã¿Ò»¸ö header µµÔÚÈÎÒ»¸öÔ´ÂëÎļþÖÐÖ»±»°ü º¬ÁËÒ»´Î¡£ÎÒÃÇÒ»°ãÊÇÓÃÔ¤´¦ÀíÆ÷À´´ïµ½Õâ¸öÄ¿µÄµÄ¡£µ±ÎÒÃǽøÈë ÿһ¸ö header µµÊ±£¬ÎÒÃÇΪÕâ¸ö header µµ #define Ò»¸ö¾Þ¼¯ Ö¸Áî¡£Ö»ÓÐÔÚÕâ¸ö¾Þ¼¯Ö¸ÁîûÓỶ¨ÒåµÄÇ°ÌáÏ£¬ÎÒÃDzÅÕæÕýʹÓøà header µµµÄÖ÷Ìå¡£ÔÚʵ¼ÊÓ¦ÓÃÉÏ£¬ÎÒÃÇÖ»Òª¼òµ¥µÄ°ÑÏÂÃæÒ»¶Î Âë·ÅÔÚÿһ¸ö header µµµÄ¿ªÊ¼²¿·Ö£º
#ifndef FILENAME_H
#define FILENAME_H
È»ºó°ÑÏÂÃæÒ»ÐÐÂë·ÅÔÚ×îºó£º
#endif
Óà header µµµÄµµÃû£¨´óдµÄ£©´úÌæÉÏÃæµÄ FILENAME_H£¬Óõ×Ïß ´úÌæµµÃûÖеĵ㡣ÓÐЩÈËϲ»¶ÔÚ #endif ¼ÓÉÏ×¢ÊÍÀ´ÌáÐÑËûÃÇÕâ¸ö #endif Ö¸µÄÊÇʲô¡£ÀýÈ磺
#endif /* #ifndef FILENAME_H */
ÎÒ¸öÈËûÓÐÕâ¸öÏ°¹ß£¬ÒòΪÕâÆäʵÊǺÜÃ÷ÏԵġ£µ±È»ÕâÖ»ÊǸ÷È赀 ·ç¸ñ²»Í¬£¬ÎÞÉË´óÑÅ¡£
ÄãÖ»ÐèÒªÔÚÄÇЩÓбàÒë´íÎóµÄ header µµÖмÓÈëÕâ¸ö¼¼ÇÉ£¬µ«ÔÚËù ÓÐµÄ header µµÖж¼¼ÓÈëҲûʲôËðʧ£¬µ½µ×ÕâÊǸöºÃÏ°¹ß¡£
1.5 ÖØбàÒëÒ»¸ö¶àÎļþÏîÄ¿
Çå³þµÄÇø±ð±àÒëºÍÁ¬½ÓÊǺÜÖØÒªµÄ¡£±àÒëÆ÷ʹÓÃÔ´ÂëÎļþÀ´²úÉúijÖÖ ÐÎʽµÄÄ¿±êÎļþ(object files)¡£ÔÚÕâ¸ö¹ý³ÌÖУ¬ÍⲿµÄ·ûºÅ²Î¿¼²¢ ûÓб»½âÊÍ»òÌæ»»¡£È»ºóÎÒÃÇʹÓÃÁ¬½ÓÆ÷À´Á¬½ÓÕâЩĿ±êÎļþºÍһЩ±ê×¼µÄ³ÌÐò°üÔÙ¼ÓÄãÖ¸¶¨µÄ³ÌÐò°ü£¬×îºóÁ¬½ÓÉú³ÉÒ»¸ö¿ÉÖ´ÐгÌÐò¡£ ÔÚÕâ¸ö½×¶Î£¬Ò»¸öÄ¿±êÎļþÖжԱðµÄÎļþÖеķûºÅµÄ²Î¿¼±»½âÊÍ£¬²¢±¨¸æ²»Äܱ»½âÊ͵IJο¼£¬Ò»°ãÊÇÒÔ´íÎóÐÅÏ¢µÄÐÎʽ±¨¸æ³öÀ´¡£
»ù±¾µÄ²½Öè¾ÍÓ¦¸ÃÊÇ£¬°ÑÄãµÄÔ´ÂëÎļþÒ»¸öÒ»¸öµÄ±àÒë³ÉÄ¿±êÎļþµÄ¸ñ ʽ£¬×îºó°ÑËùÓеÄÄ¿±êÎļþ¼ÓÉÏÐèÒªµÄ³ÌÐò°üÁ¬½Ó³ÉÒ»¸ö¿ÉÖ´ÐÐÎļþ¡£¾ßÌåÔõô×öÊÇÓÉÄãµÄ±àÒëÆ÷¾ö¶¨µÄ¡£ÕâÀïÎÒÖ»¸ø³ö gcc £¨GNU C ±àÒë Æ÷£©µÄÓйØÃüÁÕâЩÓпÉÄܶÔÄãµÄ·Ç gcc ±àÒëÆ÷Ò²ÊÊÓá£
gcc ÊÇÒ»¸ö¶àÄ¿±êµÄ¹¤¾ß¡£ËüÔÚÐèÒªµÄʱºòºô½ÐÆäËüµÄÔª¼þ£¨Ô¤´¦Àí ³ÌÐò£¬±àÒëÆ÷£¬×éºÏ³ÌÐò£¬Á¬½ÓÆ÷£©¡£¾ßÌåµÄÄÄЩԪ¼þ±»ºô½ÐÈ¡¾öÓÚÊäÈëÎļþµÄÀàÐͺÍÄã´«µÝ¸øËüµÄ¿ª¹Ø¡£
Ò»°ãÀ´Ëµ£¬Èç¹ûÄãÖ»¸øËü£ÃÔ´ÂëÎļþ£¬Ëü½«Ô¤´¦Àí£¬±àÒ룬×éºÏËùÓÐ µÄÎļþ£¬È»ºó°ÑËùµÃµÄÄ¿±êÎļþÁ¬½Ó³ÉÒ»¸ö¿ÉÖ´ÐÐÎļþ£¨Ò»°ãÉú³ÉµÄÎļþ±»ÃüÃûΪ a.out £©¡£Ä㵱Ȼ¿ÉÒÔÕâô×ö£¬µ«Õâ»áÆÆ»µºÜ¶àÎÒÃÇ °ÑÒ»¸öÏîÄ¿·Ö½â³É¶à¸öÎļþËùµÃµ½µÄºÃ´¦¡£
Èç¹ûÄã¸øËüÒ»¸ö -c ¿ª¹Ø£¬gcc Ö»°Ñ¸øËüµÄÎļþ±àÒë³ÉÄ¿±êÎļþ£¬ÓÃÔ´ÂëÎļþµÄÎļþÃûÃüÃûµ«°ÑÆäºó׺ÓÉ¡°.c¡±»ò¡°.cc¡±±ä³É¡°.o¡±¡£ Èç¹ûÄã¸øËüµÄÊÇÒ»ÁÐÄ¿±êÎļþ£¬ gcc »á°ÑËüÃÇÁ¬½Ó³É¿ÉÖ´ÐÐÎļþ£¬È±Ê¡ÎļþÃûÊÇ a.out ¡£Äã¿ÉÒԸıäȱʡÃû£¬Óÿª¹Ø -o ºó¸úÄãÖ¸¶¨ µÄÎļþÃû¡£
Òò´Ë£¬µ±Äã¸Ä±äÁËÒ»¸öÔ´ÂëÎļþºó£¬ÄãÐèÒªÖØбàÒëËü£º 'gcc -c filename.c' È»ºóÖØÐÂÁ¬½ÓÄãµÄÏîÄ¿£º 'gcc -o exec_filename *.o'¡£ Èç¹ûÄã¸Ä±äÁËÒ»¸ö header µµ£¬ÄãÐèÒªÖØбàÒëËùÓÐ #include ¹ýÕâ¸öµµµÄÔ´ÂëÎļþ£¬Äã¿ÉÒÔÓà 'gcc -c file1.c file2.c file3.c' È»ºóÏóÉϱßÒ»ÑùÁ¬½Ó¡£
µ±È»Õâô×öÊǺܷ±ËöµÄ£¬ÐÒ¿÷ÎÒÃÇÓÐЩ¹¤¾ßʹÕâ¸ö²½Öè±äµÃ¼òµ¥¡£ ±¾Îĵĵڶþ²¿·Ö¾ÍÊǽéÉÜÆäÖеÄÒ»¼þ¹¤¾ß£ºGNU Make ¹¤¾ß¡£
£¨ºÃ¼Ò»ï£¬ÏÖÔڲſªÊ¼¼ûÕæÕ¡£Äúѧµ½µã¶ù¶«Î÷û£¿£©
2) GNU Make ¹¤¾ß
~~~~~~~~~~~~~~~~
2.1 »ù±¾ makefile ½á¹¹
GNU Make µÄÖ÷Òª¹¤×÷ÊǶÁ½øÒ»¸öÎı¾Îļþ£¬ makefile ¡£Õâ¸öÎļþÀïÖ÷ÒªÊÇÓйØÄÄЩÎļþ£¨¡®target¡¯Ä¿µÄÎļþ£©ÊÇ´ÓÄÄЩ±ðµÄ Îļþ£¨¡®dependencies¡¯ÒÀ¿¿Îļþ£©ÖвúÉúµÄ£¬ÓÃʲôÃüÁîÀ´½øÐÐÕâ¸ö²úÉú¹ý³Ì¡£ÓÐÁËÕâЩÐÅÏ¢£¬ make »á¼ì²é´ÅµúÉϵÄÎļþ£¬Èç¹û Ä¿µÄÎļþµÄʱ¼ä´Á£¨¸ÃÎļþÉú³É»ò±»¸Ä¶¯Ê±µÄʱ¼ä£©±ÈÖÁÉÙËüµÄÒ»¸öÒÀ¿¿Îļþ¾ÉµÄ»°£¬ make ¾ÍÖ´ÐÐÏàÓ¦µÄÃüÁÒÔ±ã¸üÐÂÄ¿µÄÎļþ¡££¨Ä¿µÄÎļþ²»Ò»¶¨ÊÇ×îºóµÄ¿ÉÖ´Ðеµ£¬Ëü¿ÉÒÔÊÇÈκÎÒ»¸öÎļþ¡££©
makefile Ò»°ã±»½Ð×ö¡°makefile¡±»ò¡°Makefile¡±¡£µ±È»Äã¿ÉÒÔ ÔÚ make µÄÃüÁîÐÐÖ¸¶¨±ðµÄÎļþÃû¡£Èç¹ûÄã²»ÌرðÖ¸¶¨£¬Ëü»áÑ° ÕÒ¡°makefile¡±»ò¡°Makefile¡±£¬Òò´ËʹÓÃÕâÁ½¸öÃû×ÖÊÇ×î¼òµ¥µÄ¡£
Ò»¸ö makefile Ö÷Òªº¬ÓÐһϵÁеĹæÔò£¬ÈçÏ£º
: ...
(tab)
(tab)
.
.
.
ÀýÈ磬¿¼ÂÇÒÔÏ嵀 makefile £º
=== makefile ¿ªÊ¼ ===
myprog : foo.o bar.o
gcc foo.o bar.o -o myprog
foo.o : foo.c foo.h bar.h
gcc -c foo.c -o foo.o
bar.o : bar.c bar.h
gcc -c bar.c -o bar.o
=== makefile ½áÊø ===
ÕâÊÇÒ»¸ö·Ç³£»ù±¾µÄ makefile ---- make ´Ó×îÉÏÃ濪ʼ£¬°ÑÉÏÃæµÚÒ»¸öÄ¿µÄ£¬¡®myprog¡¯£¬×öΪËüµÄÖ÷ҪĿ±ê£¨Ò»¸öËüÐèÒª±£ Ö¤Æä×ÜÊÇ×îеÄ×îÖÕÄ¿±ê£©¡£¸ø³öµÄ¹æÔò˵Ã÷Ö»ÒªÎļþ¡®myprog¡¯ ±ÈÎļþ¡®foo.o¡¯»ò¡®bar.o¡¯ÖеÄÈκÎÒ»¸ö¾É£¬ÏÂÒ»ÐеÄÃüÁ »á±»Ö´ÐС£
µ«ÊÇ£¬ÔÚ¼ì²éÎļþ foo.o ºÍ bar.o µÄʱ¼ä´Á֮ǰ£¬Ëü»áÍùϲé ÕÒÄÇЩ°Ñ foo.o »ò bar.o ×öΪĿ±êÎļþµÄ¹æÔò¡£ËüÕÒµ½µÄ¹ØÓÚ foo.o µÄ¹æÔò£¬¸ÃÎļþµÄÒÀ¿¿ÎļþÊÇ foo.c, foo.h ºÍ bar.h ¡£Ëü´ÓÏÂÃæÔÙÕÒ²»µ½Éú³ÉÕâЩÒÀ¿¿ÎļþµÄ¹æÔò£¬Ëü¾Í¿ªÊ¼¼ì²é´Åµú ÉÏÕâЩÒÀ¿¿ÎļþµÄʱ¼ä´Á¡£Èç¹ûÕâЩÎļþÖÐÈκÎÒ»¸öµÄʱ¼ä´Á±È foo.o µÄУ¬ÃüÁî 'gcc -o foo.o foo.c' ½«»áÖ´ÐУ¬´Ó¶ø¸üРÎļþ foo.o ¡£
½ÓÏÂÀ´¶ÔÎļþ bar.o ×öÀàËƵļì²é£¬ÒÀ¿¿ÎļþÔÚÕâÀïÊÇÎļþ bar.c ºÍ bar.h ¡£
ÏÖÔÚ£¬ make »Øµ½¡®myprog¡¯µÄ¹æÔò¡£Èç¹û¸Õ²ÅÁ½¸ö¹æÔòÖеÄÈÎ ºÎÒ»¸ö±»Ö´ÐУ¬myprog ¾ÍÐèÒªÖؽ¨£¨ÒòΪÆäÖÐÒ»¸ö .o µµ¾Í»á±È ¡®myprog¡¯Ð£©£¬Òò´ËÁ¬½ÓÃüÁ±»Ö´ÐС£
Ï£Íûµ½´Ë£¬Äã¿ÉÒÔ¿´³öʹÓà make ¹¤¾ßÀ´½¨Á¢³ÌÐòµÄºÃ´¦----Ç° Ò»ÕÂÖÐËùÓз±ËöµÄ¼ì²é²½Ö趼ÓÉ make ÌæÄã×öÁË£º¼ì²éʱ¼ä´Á¡£ÄãµÄÔ´ÂëÎļþÀïÒ»¸ö¼òµ¥¸Ä±ä¶¼»áÔì³ÉÄǸöÎļþ±»ÖØбàÒ루Òò Ϊ .o ÎļþÒÀ¿¿ .c Îļþ£©£¬½ø¶ø¿ÉÖ´ÐÐÎļþ±»ÖØÐÂÁ¬½Ó£¨ÒòΪ .o Îļþ±»¸Ä±äÁË£©¡£ÆäʵÕæÕýµÄµÃÒæÊÇÔÚµ±Äã¸Ä±äÒ»¸ö header µµµÄʱºò----Äã²»ÔÙÐèÒª¼ÇסÄǸöÔ´ÂëÎļþÒÀ¿¿Ëü£¬ÒòΪËùÓÐµÄ ×ÊÁ϶¼ÔÚ makefile Àï¡£ make »áºÜÇáËɵÄÌæÄãÖØбàÒëËùÓÐÄÇ Ð©ÒòÒÀ¿¿Õâ¸ö header Îļþ¶ø¸Ä±äÁ˵ÄÔ´ÂëÎļþ£¬ÈçÓÐÐèÒª£¬ÔÙ½øÐÐÖØÐÂÁ¬½Ó¡£
µ±È»£¬ÄãҪȷ¶¨ÄãÔÚ makefile ÖÐËùдµÄ¹æÔòÊÇÕýÈ·ÎÞÎóµÄ£¬Ö» ÁгöÄÇЩÔÚÔ´ÂëÎļþÖб» #include µÄ header µµ¡¡
2.2 ±àд make ¹æÔò (Rules)
×îÃ÷ÏԵģ¨Ò²ÊÇ×î¼òµ¥µÄ£©±àд¹æÔòµÄ·½·¨ÊÇÒ»¸öÒ»¸öµÄ²é ¿´Ô´ÂëÎļþ£¬°ÑËüÃǵÄÄ¿±êÎļþ×öΪĿµÄ£¬¶ø£ÃÔ´ÂëÎļþºÍ±»Ëü #include µÄ header µµ×öΪÒÀ¿¿Îļþ¡£µ«ÊÇÄãÒ²Òª°ÑÆäËü±»ÕâЩ header µµ #include µÄ header µµÒ²ÁÐΪÒÀ¿¿Îļþ£¬»¹ÓÐÄÇЩ±» °üÀ¨µÄÎļþËù°üÀ¨µÄÎļþ¡¡È»ºóÄã»á·¢ÏÖÒª¶ÔÔ½À´Ô½¶àµÄÎļþ½øÐйÜÀí£¬È»ºóÄãµÄÍ··¢¿ªÊ¼ÍÑÂ䣬ÄãµÄÆ¢Æø¿ªÊ¼±ä»µ£¬ÄãµÄÁ³ É«±ä³É²ËÉ«£¬Äã×ßÔÚ·ÉÏ¿ªÊ¼¸úµçÏ߸Ë×ÓÅöײ£¬ÖÕÓÚÄãµ·»ÙÄãµÄµçÄÔÏÔʾÆ÷£¬Í£Ö¹±à³Ì¡£µ½µÍÓÐûÓÐЩÈÝÒ×µã¶ùµÄ·½·¨ÄØ£¿
µ±È»ÓУ¡Ïò±àÒëÆ÷Òª£¡ÔÚ±àÒëÿһ¸öÔ´ÂëÎļþµÄʱºò£¬ËüʵÔÚÓ¦ ¸ÃÖªµÀÓ¦¸Ã°üÀ¨Ê²Ã´ÑùµÄ header µµ¡£Ê¹Óà gcc µÄʱºò£¬Óà -M ¿ª¹Ø£¬Ëü»áΪÿһ¸öÄã¸øËüµÄ£ÃÎļþÊä³öÒ»¸ö¹æÔò£¬°ÑÄ¿±êÎļþ ×öΪĿµÄ£¬¶øÕâ¸ö£ÃÎļþºÍËùÓÐÓ¦¸Ã±» #include µÄ header Îļþ½«×öΪÒÀ¿¿Îļþ¡£×¢ÒâÕâ¸ö¹æÔò»á¼ÓÈëËùÓÐ header Îļþ£¬°ü À¨±»½ÇÀ¨ºÅ(`<', `>')ºÍË«ÒýºÅ(`"')Ëù°üΧµÄÎļþ¡£ÆäʵÎÒÃÇ¿ÉÒÔ Ï൱¿Ï¶¨ÏµÍ³ header µµ£¨±ÈÈç stdio.h, stdlib.h µÈµÈ£©²»»á ±»ÎÒÃǸü¸Ä£¬Èç¹ûÄãÓà -MM À´´úÌæ -M ´«µÝ¸ø gcc£¬ÄÇЩÓýÇÀ¨ ºÅ°üΧµÄ header µµ½«²»»á±»°üÀ¨¡££¨Õâ»á½ÚʡһЩ±àÒëʱ¼ä£©
ÓÉ gcc Êä³öµÄ¹æÔò²»»áº¬ÓÐÃüÁ·Ö£»Äã¿ÉÒÔ×Ô¼ºÐ´ÈëÄãµÄÃüÁî »òÕßʲôҲ²»Ð´£¬¶øÈà make ʹÓÃËüµÄÒþº¬µÄ¹æÔò£¨²Î¿¼ÏÂÃæµÄ 2.4 ½Ú£©¡£
2.3 Makefile ±äÁ¿
ÉÏÃæÌáµ½ makefiles ÀïÖ÷Òª°üº¬Ò»Ð©¹æÔò¡£ËüÃÇ°üº¬µÄÆäËüµÄ¶« Î÷ÊDZäÁ¿¶¨Òå¡£
makefile ÀïµÄ±äÁ¿¾ÍÏñÒ»¸ö»·¾³±äÁ¿(environment variable)¡£ ÊÂʵÉÏ£¬»·¾³±äÁ¿ÔÚ make ¹ý³ÌÖб»½âÊÍ³É make µÄ±äÁ¿¡£ÕâЩ ±äÁ¿ÊÇ´óСдÃô¸ÐµÄ£¬Ò»°ãʹÓôóд×Öĸ¡£ËüÃÇ¿ÉÒÔ´Ó¼¸ºõÈκεط½±»ÒýÓã¬Ò²¿ÉÒÔ±»ÓÃÀ´×öºÜ¶àÊÂÇ飬±ÈÈ磺
i) Öü´æÒ»¸öÎļþÃûÁÐ±í¡£ÔÚÉÏÃæµÄÀý×ÓÀÉú³É¿ÉÖ´ÐÐÎļþµÄ¹æÔò°üº¬Ò»Ð©Ä¿±êÎļþÃû×öΪÒÀ¿¿¡£ÔÚÕâ¸ö¹æÔòµÄÃüÁîÐÐ ÀïͬÑùµÄÄÇЩÎļþ±»ÊäË͸ø gcc ×öΪÃüÁî²ÎÊý¡£Èç¹ûÔÚÕâÀïʹÓÃÒ»¸ö±äÊýÀ´Öü´æËùÓеÄÄ¿±êÎļþÃû£¬¼ÓÈëеÄÄ¿±ê Îļþ»á±äµÄ¼òµ¥¶øÇҽϲ»Ò׳ö´í¡£
ii) Öü´æ¿ÉÖ´ÐÐÎļþÃû¡£Èç¹ûÄãµÄÏîÄ¿±»ÓÃÔÚÒ»¸ö·Ç gcc µÄϵ ͳÀ»òÕßÈç¹ûÄãÏëʹÓÃÒ»¸ö²»Í¬µÄ±àÒëÆ÷£¬Äã±ØÐ뽫ËùÓÐʹÓñàÒëÆ÷µÄµØ·½¸Ä³ÉÓÃеıàÒëÆ÷Ãû¡£µ«ÊÇÈç¹ûʹÓÃÒ» ¸ö±äÁ¿À´´úÌæ±àÒëÆ÷Ãû£¬ÄÇôÄãÖ»ÐèÒª¸Ä±äÒ»¸öµØ·½£¬ÆäËüËùÓеط½µÄÃüÁîÃû¾Í¶¼¸Ä±äÁË¡£
iii) Öü´æ±àÒëÆ÷Æì±ê¡£¼ÙÉèÄãÏë¸øÄãËùÓеıàÒëÃüÁî´«µÝÒ»×é ÏàͬµÄÑ¡ÏÀýÈç -Wall -O -g£©£»Èç¹ûÄã°ÑÕâ×éÑ¡Ïî´æÈëÒ»¸ö±äÁ¿£¬ÄÇôÄã¿ÉÒÔ°ÑÕâ¸ö±äÁ¿·ÅÔÚËùÓкô½Ð±àÒëÆ÷ µÄµØ·½¡£¶øµ±ÄãÒª¸Ä±äÑ¡ÏîµÄʱºò£¬ÄãÖ»ÐèÔÚÒ»¸öµØ·½¸Ä±äÕâ¸ö±äÁ¿µÄÄÚÈÝ¡£
ÒªÉ趨һ¸ö±äÁ¿£¬ÄãÖ»ÒªÔÚÒ»ÐеĿªÊ¼Ð´ÏÂÕâ¸ö±äÁ¿µÄÃû×Ö£¬ºó Ãæ¸úÒ»¸ö = ºÅ£¬ºóÃæ¸úÄãÒªÉ趨µÄÕâ¸ö±äÁ¿µÄÖµ¡£ÒÔºóÄãÒªÒýÓÃÕâ¸ö±äÁ¿£¬Ð´Ò»¸ö $ ·ûºÅ£¬ºóÃæÊÇΧÔÚÀ¨ºÅÀïµÄ±äÁ¿Ãû¡£±ÈÈçÔÚ ÏÂÃ棬ÎÒÃÇ°ÑÇ°ÃæµÄ makefile ÀûÓñäÁ¿ÖØдһ±é£º
=== makefile ¿ªÊ¼ ===
OBJS = foo.o bar.o
CC = gcc
CFLAGS = -Wall -O -g
myprog : $(OBJS)
$(CC) $(OBJS) -o myprog
foo.o : foo.c foo.h bar.h
$(CC) $(CFLAGS) -c foo.c -o foo.o
bar.o : bar.c bar.h
$(CC) $(CFLAGS) -c bar.c -o bar.o
=== makefile ½áÊø ===
»¹ÓÐһЩÉ趨ºÃµÄÄÚ²¿±äÁ¿£¬ËüÃǸù¾Ýÿһ¸ö¹æÔòÄÚÈݶ¨Òå¡£Èý¸ö ±È½ÏÓÐÓõıäÁ¿ÊÇ $@, $< ºÍ $^ £¨ÕâЩ±äÁ¿²»ÐèÒªÀ¨ºÅÀ¨×¡£©¡£ $@ À©Õ¹³Éµ±Ç°¹æÔòµÄÄ¿µÄÎļþÃû£¬ $< À©Õ¹³ÉÒÀ¿¿ÁбíÖÐµÄµÚ Ò»¸öÒÀ¿¿Îļþ£¬¶ø $^ À©Õ¹³ÉÕû¸öÒÀ¿¿µÄÁÐ±í£¨³ýµôÁËÀïÃæËùÓÐÖØ ¸´µÄÎļþÃû£©¡£ÀûÓÃÕâЩ±äÁ¿£¬ÎÒÃÇ¿ÉÒÔ°ÑÉÏÃæµÄ makefile д³É£º
=== makefile ¿ªÊ¼ ===
OBJS = foo.o bar.o
CC = gcc
CFLAGS = -Wall -O -g
myprog : $(OBJS)
$(CC) $^ -o $@
foo.o : foo.c foo.h bar.h
$(CC) $(CFLAGS) -c $< -o $@
bar.o : bar.c bar.h
$(CC) $(CFLAGS) -c $< -o $@
=== makefile ½áÊø ===
Äã¿ÉÒÔÓñäÁ¿×öÐí¶àÆäËüµÄÊÂÇ飬ÌرðÊǵ±Äã°ÑËüÃǺͺ¯Êý»ìºÏ ʹÓõÄʱºò¡£Èç¹ûÐèÒª¸ü½øÒ»²½µÄÁ˽⣬Çë²Î¿¼ GNU Make Êֲᡣ ('man make', 'man makefile')
2.4 Òþº¬¹æÔò (Implicit Rules)
Çë×¢Ò⣬ÔÚÉÏÃæµÄÀý×ÓÀ¼¸¸ö²úÉú .o ÎļþµÄÃüÁÊÇÒ»ÑùµÄ¡£ ¶¼ÊÇ´Ó .c ÎļþºÍÏà¹ØÎļþÀï²úÉú .o Îļþ£¬ÕâÊÇÒ»¸ö±ê×¼µÄ²½Öè¡£Æäʵ make ÒѾ֪µÀÔõô×ö----ËüÓÐһЩ½Ð×öÒþº¬¹æÔòµÄÄÚ ÖõĹæÔò£¬ÕâЩ¹æÔò¸æËßËüµ±ÄãûÓиø³öijЩÃüÁîµÄʱºò£¬Ó¦¸ÃÔõô°ì¡£
Èç¹ûÄã°ÑÉú³É foo.o ºÍ bar.o µÄÃüÁî´ÓËüÃǵĹæÔòÖÐɾ³ý£¬ make ½«»á²éÕÒËüµÄÒþº¬¹æÔò£¬È»ºó»áÕÒµ½Ò»¸öÊʵ±µÄÃüÁî¡£ËüµÄÃüÁî»á ʹÓÃһЩ±äÁ¿£¬Òò´ËÄã¿ÉÒÔ°´ÕÕÄãµÄÏë·¨À´É趨Ëü£ºËüʹÓñäÁ¿ CC ×öΪ±àÒëÆ÷£¨ÏóÎÒÃÇÔÚÇ°ÃæµÄÀý×Ó£©£¬²¢ÇÒ´«µÝ±äÁ¿ CFLAGS £¨¸ø C ±àÒëÆ÷£¬C++ ±àÒëÆ÷Óà CXXFLAGS £©£¬CPPFLAGS £¨ C Ô¤ ´¦ÀíÆ÷Æì±ê£©£¬ TARGET_ARCH £¨ÏÖÔÚ²»Óÿ¼ÂÇÕâ¸ö£©£¬È»ºóËü¼Ó ÈëÆì±ê '-c' £¬ºóÃæ¸ú±äÁ¿ $< £¨µÚÒ»¸öÒÀ¿¿Ãû£©£¬È»ºóÊÇÆì ±ê '-o' ¸ú±äÁ¿ $@ £¨Ä¿µÄÎļþÃû£©¡£Ò»¸ö£Ã±àÒëµÄ¾ßÌåÃüÁ»áÊÇ£º
$(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c $< -o $@
µ±È»Äã¿ÉÒÔ°´ÕÕÄã×Ô¼ºµÄÐèÒªÀ´¶¨ÒåÕâЩ±äÁ¿¡£Õâ¾ÍÊÇΪʲôÓà gcc µÄ -M »ò -MM ¿ª¹ØÊä³öµÄÂë¿ÉÒÔÖ±½ÓÓÃÔÚÒ»¸ö makefile Àï¡£
2.5 ¼ÙÏóÄ¿µÄ (Phony Targets)
¼ÙÉèÄãµÄÒ»¸öÏîÄ¿×îºóÐèÒª²úÉúÁ½¸ö¿ÉÖ´ÐÐÎļþ¡£ÄãµÄÖ÷ҪĿ±ê ÊDzúÉúÁ½¸ö¿ÉÖ´ÐÐÎļþ£¬µ«ÕâÁ½¸öÎļþÊÇÏ໥¶ÀÁ¢µÄ----Èç¹ûÒ»¸öÎļþÐèÒªÖؽ¨£¬²¢²»Ó°ÏìÁíÒ»¸ö¡£Äã¿ÉÒÔʹÓá°¼ÙÏóÄ¿µÄ¡±À´ ´ïµ½ÕâÖÖЧ¹û¡£Ò»¸ö¼ÙÏóÄ¿µÄ¸úÒ»¸öÕý³£µÄÄ¿µÄ¼¸ºõÊÇÒ»ÑùµÄ£¬Ö»ÊÇÕâ¸öÄ¿µÄÎļþÊDz»´æÔڵġ£Òò´Ë£¬ make ×ÜÊÇ»á¼ÙÉèËüÐèÒª ±»Éú³É£¬µ±°ÑËüµÄÒÀÀµÎļþ¸üк󣬾ͻáÖ´ÐÐËüµÄ¹æÔòÀïµÄÃüÁîÐС£
Èç¹ûÔÚÎÒÃÇµÄ makefile ¿ªÊ¼´¦ÊäÈ룺
all : exec1 exec2
ÆäÖÐ exec1 ºÍ exec2 ÊÇÎÒÃÇ×öΪĿµÄµÄÁ½¸ö¿ÉÖ´ÐÐÎļþ¡£ make °ÑÕâ¸ö 'all' ×öΪËüµÄÖ÷ҪĿµÄ£¬Ã¿´ÎÖ´ÐÐʱ¶¼»á³¢ÊÔ°Ñ 'all' ¸üС£µ«¼ÈÈ»ÕâÐйæÔòÀïûÓÐÄĸöÃüÁîÀ´×÷ÓÃÔÚÒ»¸ö½Ð 'all' µÄʵ¼ÊÎļþ£¨ÊÂʵÉÏ all ²¢²»»áÔڴŵúÉÏʵ¼Ê²úÉú£©£¬ËùÒÔÕâ¸ö¹æ Ôò²¢²»ÕæµÄ¸Ä±ä 'all' µÄ״̬¡£¿É¼ÈÈ»Õâ¸öÎļþ²¢²»´æÔÚ£¬ËùÒÔ make »á³¢ÊÔ¸üРall ¹æÔò£¬Òò´Ë¾Í¼ì²éËüµÄÒÀ¿¿ exec1, exec2 ÊÇ·ñÐèÒª¸üУ¬Èç¹ûÐèÒª£¬¾Í°ÑËüÃǸüУ¬´Ó¶ø´ïµ½ÎÒÃǵÄÄ¿µÄ¡£
¼ÙÏóÄ¿µÄÒ²¿ÉÒÔÓÃÀ´ÃèÊöÒ»×é·ÇÔ¤ÉèµÄ¶¯×÷¡£ÀýÈ磬ÄãÏë°ÑËùÓÐÓÉ make ²úÉúµÄÎļþɾ³ý£¬Äã¿ÉÒÔÔÚ makefile ÀïÉèÁ¢ÕâÑùÒ»¸ö¹æÔò£º
veryclean :
rm *.o
rm myprog
Ç°ÌáÊÇûÓÐÆäËüµÄ¹æÔòÒÀ¿¿Õâ¸ö 'veryclean' Ä¿µÄ£¬Ëü½«ÓÀÔ¶ ²»»á±»Ö´ÐС£µ«ÊÇ£¬Èç¹ûÄãÃ÷È·µÄʹÓÃÃüÁî 'make veryclean' £¬ make »á°ÑÕâ¸öÄ¿µÄ×öΪËüµÄÖ÷ҪĿ±ê£¬Ö´ÐÐÄÇЩ rm ÃüÁî¡£
Èç¹ûÄãµÄ´ÅµúÉÏ´æÔÚÒ»¸ö½Ð veryclean Îļþ£¬»á·¢ÉúʲôÊ£¿Õâ ʱÒòΪÔÚÕâ¸ö¹æÔòÀïûÓÐÈκÎÒÀ¿¿Îļþ£¬ËùÒÔÕâ¸öÄ¿µÄÎļþÒ»¶¨ÊÇ×îеÄÁË£¨ËùÓеÄÒÀ¿¿Îļþ¶¼ÒѾÊÇ×îеÄÁË£©£¬ËùÒÔ¼ÈʹÓû§Ã÷ È·ÃüÁî make ÖØвúÉúËü£¬Ò²²»»áÓÐÈκÎÊÂÇé·¢Éú¡£½â¾ö·½·¨ÊDZêÃ÷ËùÓеļÙÏóÄ¿µÄ£¨Óà .PHONY£©£¬Õâ¾Í¸æËß make ²»Óüì²éËüÃÇ ÊÇ·ñ´æÔÚÓڴŵúÉÏ£¬Ò²²»ÓòéÕÒÈκÎÒþº¬¹æÔò£¬Ö±½Ó¼ÙÉèÖ¸¶¨µÄÄ¿µÄÐèÒª±»¸üС£ÔÚ makefile Àï¼ÓÈëÏÂÃæÕâÐаüº¬ÉÏÃæ¹æÔòµÄ¹æÔò£º
.PHONY : veryclean
¾Í¿ÉÒÔÁË¡£×¢Ò⣬ÕâÊÇÒ»¸öÌØÊâµÄ make ¹æÔò£¬make ÖªµÀ .PHONY ÊÇÒ»¸öÌØÊâÄ¿µÄ£¬µ±È»Äã¿ÉÒÔÔÚËüµÄÒÀ¿¿Àï¼ÓÈëÄãÏëÓõÄÈκμÙÏó Ä¿µÄ£¬¶ø make ÖªµÀËüÃǶ¼ÊǼÙÏóÄ¿µÄ¡£
2.6 º¯Êý (Functions)
makefile ÀïµÄº¯Êý¸úËüµÄ±äÁ¿ºÜÏàËÆ----ʹÓõÄʱºò£¬ÄãÓÃÒ»¸ö $ ·ûºÅ¸ú¿ªÀ¨ºÅ£¬º¯ÊýÃû£¬¿Õ¸ñºó¸úÒ»ÁÐÓɶººÅ·Ö¸ôµÄ²ÎÊý£¬×îºóÓùØÀ¨ºÅ½áÊø¡£ÀýÈ磬ÔÚ GNU Make ÀïÓÐÒ»¸ö½Ð 'wildcard' µÄº¯Êý£¬ËüÓÐÒ»¸ö²ÎÊý£¬¹¦ÄÜÊÇÕ¹¿ª³ÉÒ»ÁÐËùÓзûºÏÓÉÆä²ÎÊýÃèÊöµÄÎÄ ¼þÃû£¬Îļþ¼äÒÔ¿Õ¸ñ¼ä¸ô¡£Äã¿ÉÒÔÏñÏÂÃæËùʾʹÓÃÕâ¸öÃüÁ
SOURCES = $(wildcard *.c)
ÕâÐлá²úÉúÒ»¸öËùÓÐÒÔ '.c' ½áβµÄÎļþµÄÁÐ±í£¬È»ºó´æÈë±äÁ¿ SOURCES Àï¡£µ±È»Äã²»ÐèÒªÒ»¶¨Òª°Ñ½á¹û´æÈëÒ»¸ö±äÁ¿¡£
ÁíÒ»¸öÓÐÓõĺ¯ÊýÊÇ patsubst £¨ patten substitude, Æ¥ÅäÌæ»»µÄËõд£©º¯Êý¡£ËüÐèÒª£³¸ö²ÎÊý----µÚÒ»¸öÊÇÒ»¸öÐèҪƥÅäµÄ ʽÑù£¬µÚ¶þ¸ö±íʾÓÃʲôÀ´Ìæ»»Ëü£¬µÚÈý¸öÊÇÒ»¸öÐèÒª±»´¦ÀíµÄÓÉ¿Õ¸ñ·Ö¸ôµÄ×ÖÁС£ÀýÈ磬´¦ÀíÄǸö¾¹ýÉÏÃ涨ÒåºóµÄ±äÁ¿£¬
OBJS = $(patsubst %.c,%.o,$(SOURCES))
ÕâÐн«´¦ÀíËùÓÐÔÚ SOURCES ×ÖÁÐÖеÄ×Ö£¨Ò»ÁÐÎļþÃû£©£¬Èç¹ûËüµÄ ½áβÊÇ '.c' £¬¾ÍÓà '.o' °Ñ '.c' È¡´ú¡£×¢ÒâÕâÀïµÄ % ·ûºÅ½«Æ¥ ÅäÒ»¸ö»ò¶à¸ö×Ö·û£¬¶øËüÿ´ÎËùÆ¥ÅäµÄ×Ö´®½Ð×öÒ»¸ö¡®±ú¡¯(stem) ¡£ ÔÚµÚ¶þ¸ö²ÎÊýÀ % ±»½â¶Á³ÉÓõÚÒ»²ÎÊýËùÆ¥ÅäµÄÄǸö±ú¡£
2.7 Ò»¸ö±È½ÏÓÐЧµÄ makefile
ÀûÓÃÎÒÃÇÏÖÔÚËùѧµÄ£¬ÎÒÃÇ¿ÉÒÔ½¨Á¢Ò»¸öÏ൱ÓÐЧµÄ makefile ¡£ Õâ¸ö makefile ¿ÉÒÔÍê³É´ó²¿·ÖÎÒÃÇÐèÒªµÄÒÀ¿¿¼ì²é£¬²»ÓÃ×öÌ«´ó µÄ¸Ä±ä¾Í¿ÉÖ±½ÓÓÃÔÚ´ó¶àÊýµÄÏîÄ¿Àï¡£
Ê×ÏÈÎÒÃÇÐèÒªÒ»¸ö»ù±¾µÄ makefile À´½¨ÎÒÃǵijÌÐò¡£ÎÒÃÇ¿ÉÒÔÈà ËüËÑË÷µ±Ç°Ä¿Â¼£¬ÕÒµ½Ô´ÂëÎļþ£¬²¢ÇÒ¼ÙÉèËüÃǶ¼ÊÇÊôÓÚÎÒÃǵÄÏîÄ¿µÄ£¬·Å½øÒ»¸ö½Ð SOURCES µÄ±äÁ¿¡£ÕâÀïÈç¹ûÒ²°üº¬ËùÓÐµÄ *.cc Îļþ£¬Ò²Ðí»á¸ü±£ÏÕ£¬ÒòΪԴÂëÎļþ¿ÉÄÜÊÇ C++ ÂëµÄ¡£
SOURCES = $(wildcard *.c *.cc)
ÀûÓà patsubst £¬ÎÒÃÇ¿ÉÒÔÓÉÔ´ÂëÎļþÃû²úÉúÄ¿±êÎļþÃû£¬ÎÒÃÇÐè Òª±àÒë³öÕâЩĿ±êÎļþ¡£Èç¹ûÎÒÃǵÄÔ´ÂëÎļþ¼ÈÓÐ .c Îļþ£¬Ò²ÓÐ .cc Îļþ£¬ÎÒÃÇÐèҪʹÓÃÏàǶµÄ patsubst º¯Êýºô½Ð£º
OBJS = $(patsubst %.c,%.o,$(patsubst %.cc,%.o,$(SOURCES)))
×îÀïÃæÒ»²ã patsubst µÄºô½Ð»á¶Ô .cc Îļþ½øÐкó׺Ìæ´ú£¬²úÉúµÄ½á ¹û±»Íâ²ãµÄ patsubst ºô½Ð´¦Àí£¬½øÐÐ¶Ô .c Îļþºó׺µÄÌæ´ú¡£
ÏÖÔÚÎÒÃÇ¿ÉÒÔÉèÁ¢Ò»¸ö¹æÔòÀ´½¨¿ÉÖ´ÐÐÎļþ£º
myprog : $(OBJS)
gcc -o myprog $(OBJS)
½øÒ»²½µÄ¹æÔò²»Ò»¶¨ÐèÒª£¬ gcc ÒѾ֪µÀÔõôȥÉú³ÉÄ¿±êÎļþ (object files) ¡£ÏÂÃæÎÒÃÇ¿ÉÒÔÉ趨²úÉúÒÀ¿¿ÐÅÏ¢µÄ¹æÔò£º
depends : $(SOURCES)
gcc -M $(SOURCES) > depends
ÔÚÕâÀïÈç¹ûÒ»¸ö½Ð 'depends' µÄÎļþ²»´æÔÚ£¬»òÈκÎÒ»¸öÔ´ÂëÎļþ ±ÈÒ»¸öÒÑ´æÔÚµÄ depends ÎļþУ¬ÄÇôһ¸ö depends Îļþ»á±»Éú ³É¡£depends Îļþ½«»áº¬ÓÐÓÉ gcc ²úÉúµÄ¹ØÓÚÔ´ÂëÎļþµÄ¹æÔò£¨×¢ Òâ -M ¿ª¹Ø£©¡£ÏÖÔÚÎÒÃÇÒªÈà make °ÑÕâЩ¹æÔòµ±×ö makefile µµ µÄÒ»²¿·Ö¡£ÕâÀïʹÓõļ¼ÇɺÜÏñ C ÓïÑÔÖÐµÄ #include ϵͳ----ÎÒ ÃÇÒªÇó make °ÑÕâ¸öÎļþ include µ½ makefile ÀÈçÏ£º
include depends
GNU Make ¿´µ½Õâ¸ö£¬¼ì²é 'depends' Ä¿µÄÊÇ·ñ¸üÐÂÁË£¬Èç¹ûûÓУ¬ ËüÓÃÎÒÃǸøËüµÄÃüÁîÖØвúÉú depends µµ¡£È»ºóËü»á°ÑÕâ×飨У© ¹æÔò°üº¬½øÀ´£¬¼ÌÐø´¦Àí×îÖÕÄ¿±ê 'myprog' ¡£µ±¿´µ½ÓÐ¹Ø myprog µÄ¹æÔò£¬Ëü»á¼ì²éËùÓеÄÄ¿±êÎļþÊÇ·ñ¸üÐÂ----ÀûÓà depends Îļþ ÀïµÄ¹æÔò£¬µ±È»ÕâЩ¹æÔòÏÖÔÚÒѾÊǸüйýµÄÁË¡£
Õâ¸öϵͳÆäʵЧÂʺܵͣ¬ÒòΪÿµ±Ò»¸öÔ´ÂëÎļþ±»¸Ä¶¯£¬ËùÓеÄÔ´Âë Îļþ¶¼Òª±»Ô¤´¦ÀíÒÔ²úÉúÒ»¸öÐ嵀 'depends' Îļþ¡£¶øÇÒËüÒ²²»ÊÇ 100% µÄ°²È«£¬ÕâÊÇÒòΪµ±Ò»¸ö header µµ±»¸Ä¶¯£¬ÒÀ¿¿ÐÅÏ¢²¢²»»á±»¸üС£µ«¾Í»ù±¾¹¤×÷À´Ëµ£¬ËüÒ²ËãÏ൱ÓÐÓõÄÁË¡£
2.8 Ò»¸ö¸üºÃµÄ makefile
ÕâÊÇÒ»¸öÎÒΪÎÒ´ó¶àÊýÏîÄ¿Éè¼ÆµÄ makefile ¡£ËüÓ¦¸Ã¿ÉÒÔ²»ÐèÒªÐÞ ¸ÄµÄÓÃÔڴ󲿷ÖÏîÄ¿Àï¡£ÎÒÖ÷Òª°ÑËüÓÃÔÚ djgpp ÉÏ£¬ÄÇÊÇÒ»¸ö DOS °æµÄ gcc ±àÒëÆ÷¡£Òò´ËÄã¿ÉÒÔ¿´µ½Ö´ÐеÄÃüÁîÃû¡¢ 'alleg' ³ÌÐò°ü¡¢ ºÍ RM -F ±äÁ¿¶¼·´Ó³ÁËÕâÒ»µã¡£
=== makefile ¿ªÊ¼ ===
######################################
#
# Generic makefile
#
# by George Foot
# email: george.foot@merton.ox.ac.uk
#
# Copyright (c) 1997 George Foot
# All rights reserved.
# ±£ÁôËùÓаæȨ
#
# No warranty, no liability;
# you use this at your own risk.
# û±£ÏÕ£¬²»¸ºÔð
# ÄãÒªÓÃÕâ¸ö£¬Äã×Ô¼ºµ£·çÏÕ
#
# You are free to modify and
# distribute this without giving
# credit to the original author.
# Äã¿ÉÒÔËæ±ã¸ü¸ÄºÍÉ¢·¢Õâ¸öÎļþ
# ¶ø²»ÐèÒª¸øÔ×÷ÕßʲôÈÙÓþ¡£
# £¨ÄãºÃÒâ˼£¿£©
#
######################################
### Customising
# Óû§É趨
#
# Adjust the following if necessary; EXECUTABLE is the target
# executable's filename, and LIBS is a list of libraries to link in
# (e.g. alleg, stdcx, iostr, etc). You can override these on make's
# command line of course, if you prefer to do it that way.
#
# Èç¹ûÐèÒª£¬µ÷ÕûÏÂÃæµÄ¶«Î÷¡£ EXECUTABLE ÊÇÄ¿±êµÄ¿ÉÖ´ÐÐÎļþÃû£¬ LIBS
# ÊÇÒ»¸öÐèÒªÁ¬½ÓµÄ³ÌÐò°üÁÐ±í£¨ÀýÈç alleg, stdcx, iostr µÈµÈ£©¡£µ±È»Äã
# ¿ÉÒÔÔÚ make µÄÃüÁîÐи²¸ÇËüÃÇ£¬ÄãÔ¸Òâ¾ÍûÎÊÌâ¡£
#
EXECUTABLE := mushroom.exe
LIBS := alleg
# Now alter any implicit rules' variables if you like, e.g.:
#
# ÏÖÔÚÀ´¸Ä±äÈκÎÄãÏë¸Ä¶¯µÄÒþº¬¹æÔòÖеıäÁ¿£¬ÀýÈç
CFLAGS := -g -Wall -O3 -m486
CXXFLAGS := $(CFLAGS)
# The next bit checks to see whether rm is in your djgpp bin
# directory; if not it uses del instead, but this can cause (harmless)
# `File not found' error messages. If you are not using DOS at all,
# set the variable to something which will unquestioningly remove
# files.
#
# ÏÂÃæÏȼì²éÄãµÄ djgpp ÃüÁîĿ¼ÏÂÓÐûÓÐ rm ÃüÁÈç¹ûûÓУ¬ÎÒÃÇʹÓÃ
# del ÃüÁîÀ´´úÌ棬µ«ÓпÉÄܸøÎÒÃÇ 'File not found' Õâ¸ö´íÎóÐÅÏ¢£¬Õâû
# ʲô´ó°¡£Èç¹ûÄã²»ÊÇÓà DOS £¬°ÑËüÉ趨³ÉÒ»¸öɾÎļþ¶ø²»·Ï»°µÄÃüÁî¡£
# £¨ÆäʵÕâÒ»²½ÔÚ UNIX ÀàµÄϵͳÉÏÊǶàÓàµÄ£¬Ö»ÊÇ·½±ã DOS Óû§¡£ UNIX
# Óû§¿ÉÒÔɾ³ýÕ⣵ÐÐÃüÁî¡££©
ifneq ($(wildcard $(DJDIR)/bin/rm.exe),)
RM-F := rm -f
else
RM-F := del
endif
# You shouldn't need to change anything below this point.
#
# ´ÓÕâÀ↑ʼ£¬ÄãÓ¦¸Ã²»ÐèÒª¸Ä¶¯Èκζ«Î÷¡££¨ÎÒÊDz»Ì«ÏàÐÅ£¬Ì«£Î£ÂÁË£¡£©
SOURCE := $(wildcard *.c) $(wildcard *.cc)
OBJS := $(patsubst %.c,%.o,$(patsubst %.cc,%.o,$(SOURCE)))
DEPS := $(patsubst %.o,%.d,$(OBJS))
MISSING_DEPS := $(filter-out $(wildcard $(DEPS)),$(DEPS))
MISSING_DEPS_SOURCES := $(wildcard $(patsubst %.d,%.c,$(MISSING_DEPS)) \
$(patsubst %.d,%.cc,$(MISSING_DEPS)))
CPPFLAGS += -MD
.PHONY : everything deps objs clean veryclean rebuild
everything : $(EXECUTABLE)
deps : $(DEPS)
objs : $(OBJS)
clean :
@$(RM-F) *.o
@$(RM-F) *.d
veryclean: clean
@$(RM-F) $(EXECUTABLE)
rebuild: veryclean everything
ifneq ($(MISSING_DEPS),)
$(MISSING_DEPS) :
@$(RM-F) $(patsubst %.d,%.o,$@)
endif
-include $(DEPS)
$(EXECUTABLE) : $(OBJS)
gcc -o $(EXECUTABLE) $(OBJS) $(addprefix -l,$(LIBS))
=== makefile ½áÊø ===
Óм¸¸öµØ·½ÖµµÃ½âÊÍһϵġ£Ê×ÏÈ£¬ÎÒÔÚ¶¨Òå´ó²¿·Ö±äÁ¿µÄʱºòʹ ÓõÄÊÇ := ¶ø²»ÊÇ = ·ûºÅ¡£ËüµÄ×÷ÓÃÊÇÁ¢¼´°Ñ¶¨ÒåÖвο¼µ½µÄº¯ÊýºÍ±äÁ¿¶¼Õ¹¿ªÁË¡£Èç¹ûʹÓà = µÄ»°£¬º¯ÊýºÍ±äÁ¿²Î¿¼»áÁôÔÚÄÇ ¶ù£¬¾ÍÊÇ˵¸Ä±äÒ»¸ö±äÁ¿µÄÖµ»áµ¼ÖÂÆäËü±äÁ¿µÄÖµÒ²±»¸Ä±ä¡£ÀýÈ磺
A = foo
B = $(A)
# ÏÖÔÚ B ÊÇ $(A) £¬¶ø $(A) ÊÇ 'foo' ¡£
A = bar
# ÏÖÔÚ B ÈÔÈ»ÊÇ $(A) £¬µ«ËüµÄÖµÒÑËæ×űä³É 'bar' ÁË¡£
B := $(A)
# ÏÖÔÚ B µÄÖµÊÇ 'bar' ¡£
A = foo
# B µÄÖµÈÔÈ»ÊÇ 'bar' ¡£
make »áºöÂÔÔÚ # ·ûºÅºóÃæÖ±µ½ÄÇÒ»ÐнáÊøµÄËùÓÐÎÄ×Ö¡£
ifneg...else...endif ϵͳÊÇ makefile ÀïÈÃijһ²¿·ÖÂëÓÐÌõ¼þµÄ ʧЧ£¯ÓÐЧµÄ¹¤¾ß¡£ ifeq ʹÓÃÁ½¸ö²ÎÊý£¬Èç¹ûËüÃÇÏàͬ£¬Ëü°ÑÖ± µ½ else £¨»òÕß endif £¬Èç¹ûûÓÐ else µÄ»°£©µÄÒ»¶ÎÂë¼Ó½ø makefile ÀÈç¹û²»Í¬£¬°Ñ else µ½ endif ¼äµÄÒ»¶ÎÂë¼ÓÈë makefile £¨Èç¹ûÓÐ else £©¡£ ifneq µÄÓ÷¨¸ÕºÃÏà·´¡£
'filter-out' º¯ÊýʹÓÃÁ½¸öÓÿոñ·Ö¿ªµÄÁÐ±í£¬Ëü°ÑµÚ¶þÁбíÖÐËù ÓеĴæÔÚÓÚµÚÒ»ÁбíÖеÄÏîĿɾ³ý¡£ÎÒÓÃËüÀ´´¦Àí DEPS ÁÐ±í£¬°ÑËù ÓÐÒѾ´æÔÚµÄÏîÄ¿¶¼É¾³ý£¬¶øÖ»±£ÁôȱÉÙµÄÄÇЩ¡£
ÎÒÇ°Ãæ˵¹ý£¬ CPPFLAGS ´æÓÐÓÃÓÚÒþº¬¹æÔòÖд«¸øÔ¤´¦ÀíÆ÷µÄһЩ Æì±ê¡£¶ø -MD ¿ª¹ØÀàËÆ -M ¿ª¹Ø£¬µ«ÊÇ´ÓÔ´ÂëÎļþ .c »ò .cc ÖÐ ÐγɵÄÎļþÃûÊÇʹÓúó׺ .d µÄ£¨Õâ¾Í½âÊÍÁËÎÒÐÎ³É DEPS ±äÁ¿µÄ ²½Ö裩¡£DEPS ÀïÌáµ½µÄÎļþºóÀ´Óà '-include' ¼Ó½øÁË makefile ÀËüÒþ²ØÁËËùÓÐÒòÎļþ²»´æÔÚ¶ø²úÉúµÄ´íÎóÐÅÏ¢¡£
Èç¹ûÈκÎÒÀ¿¿Îļþ²»´æÔÚ£¬ makefile »á°ÑÏàÓ¦µÄ .o Îļþ´Ó´Åµú ÉÏɾ³ý£¬´Ó¶øʹµÃ make Öؽ¨Ëü¡£ÒòΪ CPPFLAGS Ö¸¶¨ÁË -MD £¬ ËüµÄ .d ÎļþÒ²±»ÖØвúÉú¡£
×îºó£¬ 'addprefix' º¯Êý°ÑµÚ¶þ¸ö²ÎÊýÁбíµÄÿһÏîǰ׺ÉϵÚÒ» ¸ö²ÎÊýÖµ¡£
Õâ¸ö makefile µÄÄÇЩĿµÄÊÇ£¨ÕâЩĿµÄ¿ÉÒÔ´«¸ø make µÄÃüÁîÐÐ À´Ö±½ÓÑ¡Óã©£º
everything:£¨Ô¤É裩 ¸üÐÂÖ÷ÒªµÄ¿ÉÖ´ÐгÌÐò£¬²¢ÇÒΪÿһ¸ö Ô´ÂëÎļþÉú³É»ò¸üÐÂÒ»¸ö '.d' ÎļþºÍÒ»¸ö '.o' Îļþ¡£
deps: Ö»ÊÇΪÿһ¸öÔ´Âë³ÌÐò²úÉú»ò¸üÐÂÒ»¸ö '.d' Îļþ¡£
objs: Ϊÿһ¸öÔ´Âë³ÌÐòÉú³É»ò¸üР'.d' ÎļþºÍÄ¿±êÎļþ¡£
clean: ɾ³ýËùÓÐÖн飯ÒÀ¿¿Îļþ£¨ *.d ºÍ *.o £©¡£
veryclean: ×ö `clean' ºÍɾ³ý¿ÉÖ´ÐÐÎļþ¡£
rebuild: ÏÈ×ö `veryclean' È»ºó `everything' £»¼ÈÍêÈ«Öؽ¨¡£
³ýÁËÔ¤ÉèµÄ everything ÒÔÍ⣬ÕâÀïÍ·Ö»ÓÐ clean £¬ veryclean £¬ ºÍ rebuild ¶ÔÓû§ÊÇÓÐÒâÒåµÄ¡£
ÎÒ»¹Ã»Óз¢ÏÖµ±¸ø³öÒ»¸öÔ´ÂëÎļþµÄĿ¼£¬Õâ¸ö makefile »áʧ°ÜµÄ Çé¿ö£¬³ý·ÇÒÀ¿¿Îļþ±»ÅªÂÒ¡£Èç¹ûÕâÖÖŪÂÒµÄÇé¿ö·¢ÉúÁË£¬Ö»ÒªÊäÈë `make clean' £¬ËùÓеÄÄ¿±êÎļþºÍÒÀ¿¿Îļþ»á±»É¾³ý£¬ÎÊÌâ¾ÍÓ¦¸Ã±»½â¾öÁË¡£µ±È»£¬×îºÃ²»Òª°ÑËüÃÇŪÂÒ¡£Èç¹ûÄã·¢ÏÖÔÚijÖÖÇé¿öÏÂÕâ ¸ö makefile Îļþ²»ÄÜÍê³ÉËüµÄ¹¤×÷£¬Çë¸æËßÎÒ£¬ÎÒ»á°ÑËüÕûºÃµÄ¡£
3 ×ܽá
~~~~~~~~~~~~~~~
ÎÒÏ£ÍûÕâƪÎÄÕÂ×ã¹»ÏêϸµÄ½âÊÍÁ˶àÎļþÏîÄ¿ÊÇÔõôÔË×÷µÄ£¬Ò²ËµÃ÷ÁË ÔõÑù°²È«¶øºÏÀíµÄʹÓÃËü¡£µ½´Ë£¬ÄãÓ¦¸Ã¿ÉÒÔÇáËɵÄÀûÓà GNU Make ¹¤ ¾ßÀ´¹ÜÀíСÐ͵ÄÏîÄ¿£¬Èç¹ûÄãÍêÈ«Àí½âÁ˺óÃ漸¸ö²¿·ÖµÄ»°£¬ÕâЩ¶ÔÓÚ ÄãÀ´ËµÓ¦¸ÃûʲôÀ§ÄÑ¡£
GNU Make ÊÇÒ»¼þÇ¿´óµÄ¹¤¾ß£¬ËäÈ»ËüÖ÷ÒªÊÇÓÃÀ´½¨Á¢³ÌÐò£¬Ëü»¹Óкܶà±ðµÄÓô¦¡£Èç¹ûÏëÒªÖªµÀ¸ü¶àÓйØÕâ¸ö¹¤¾ßµÄ֪ʶ£¬ËüµÄ¾ä·¨£¬º¯Êý£¬ ºÍÐí¶à±ðµÄÌص㣬ÄãÓ¦¸Ã²Î¿´ËüµÄ²Î¿¼Îļþ (info pages, ±ðµÄ GNU ¹¤¾ßÒ²Ò»Ñù£¬¿´ËüÃÇµÄ info pages. )¡£
--------------------------------------------------------------------------------
C Scene ¹Ù·½ÍøÕ¾£º http://cscene.differnet.org
C Scene ¹Ù·½µçÓÊ£º cscene@mindless.com
--------------------------------------------------------------------------------
This page is Copyright ? 1997 By C Scene. All Rights Reserved
tigernihao ÓÚ 2008-10-02 20:54:46·¢±í:
ding
wstoneh ÓÚ 2008-09-16 21:48:56·¢±í:
¶¥ÉÏÈ¥¡£:0)1