编程小贴士

给你的编程提供小点子


Makefile中的%标记和系统通配符*的区别

Makefile中的%标记和系统通配符*的区别在于,*是应用在系统中的,%是应用在这个Makefile文件中的。

(本文的测试环境是Windows7下使用MinGW提供的make.exe)

例如,如果你想编译一个文件夹下的所有.c文件,你可能会这样写:

1 %.o:%.c
2     gcc -o $@ $<

但是如果整个文件只有这两行的话,就会出现这样的错误:

Make: *** target not found. stop.

要知道原因,我们先来看看另一个makefile的运行过程,例如有Makefile如下:

 1 test1.o:test1.c
 2 
 3     gcc -o test1.o test1.c
 4 
 5  
 6 test2.o:test2.c
 7 
 8     gcc -o test2.o test2.c
 9 
10  
11 all:test1.o test2.o

如果没有指定输出项目的时候Make会自动找到makefile中第一个目标中没有通配符的目标进行构造,所以步骤是:

  1. 构造all,发现需要test1.o和test2.o
  2. 这个时候他就会在Makefile文件中找到目标能匹配test1.o和test2.o的规则。
  3. 找到test1.o的规则并且知道test1.c存在,运行下面的命令。
  4. 同步骤三构造出test2.o
  5. 现在构造all的源文件已经齐全,构建all

其中最重要的是第2步。

Makefile的通配符是在带着目的(如“寻找test1.o”)的时候才会把他要寻找的目标套用通配符%中。

所以通配符%的意思是:

  • 要找test1.o的构造规则,看看Makefile中那个规则符合。
  • 然后找到了%.o:%.c,
  • 来套一下来套一下:
  • %.o 和我要找的 test1.o 匹配
  • 套上了,得到%=test1。
  • 所以在后面的%.c就表示test1.c了。
  • OK进行构造

而通配符*的意思是:

  • 不知道目标的名字系统该目录下中所有后缀为.c的文件都是我要找的。
  • 然后遍历目录的文件,看是否匹配。找出所有匹配的项目。

所以虽然连个符号的意思有点沾边,但是他们的工作方式时完全不一样。


现在知道了为什么文件中只有

1 %.o:%.c
2     gcc -o $@ $<

会找不到目标了吧。因为没有-f参数时Make会自动找到makefile中第一个目标中没有通配符的目标进行构造,所以就等于找不到目标了。它的意思并不会自动把文件中所有的文件都编译。

所以正确的代码应该是:

1 all:$(subst .c,.o,$(wildcard *.c))
2 
3 %.o:%.c
4     gcc -o $@ $<

这才是把目录下所有文件都编译的命令。

下面是几个特舒符号的意思:

$@:目标的名字

$^:构造所需文件列表所有所有文件的名字

$<:构造所需文件列表的第一个文件的名字

$?:构造所需文件列表中更新过的文件

例如:

1 test1.o:test1.c
2     gcc -o $@ $<

$@:就是test1.o

$<:就是test1.c

1 test1.o:test1.c head.c
2     gcc -o $@ $^

$^:就是test1.c head.c

$(subst 要被替换的字符串,用来替换的字符串,被处理的字符串)

用“用来替换的字符串”替换“被处理的字符串”中的“要被替换的字符串”

所以:

$(subst .c,.o,test1.c test2.c)

就会得到test1.o test2.o

$(wildcard 寻找的文件)

在系统中寻找文件

例如:

$(wildcard *.c)

就等于找到系统中所有后缀为.c的文件,返回成以空格隔开的一整行字符

例如:test1.c test2.c test3.c 这样

$(basename 文件名)

取得文件的名字(去掉后缀的意思)

例如:

$(basename test1.c)

就会取得test1

转自博客园 http://www.cnblogs.com/warren-wong/p/3979270.html

108 Responses to “ Makefile中的%标记和系统通配符*的区别 ”

  1. see pron说道:

    Ucylot isabel marant sneakers pas cher isabel marant sneakers pas cher

  2. coloring page说道:

    I think this is a real great blog post.Really thank you! Really Great.

  3. Lung Detox说道:

    Really informative article.Thanks Again. Really Cool.

  4. This is a really good tip particularly to those new to the blogosphere. Short but very accurate information Thank you for sharing this one. A must read article!

  5. Poker Bonuses I really appreciate this post. I have been looking all over for this! Thank God I found it on Bing. You have made my day! Thanks again.

  6. This is one awesome blog post.Really thank you! Fantastic.

  7. Click to read说道:

    This website has lots of extremely useful info on it. Thanks for sharing it with me!

  8. Thanks for such a good blog. It was what I looked for.

  9. Continued说道:

    stuff prior to and you are just extremely fantastic. I actually like what you ave received

  10. website说道:

    Really appreciate you sharing this article.Really thank you! Will read on…

  11. Sweet blog! I found it while surfing around on Yahoo News. Do you have any suggestions on how to get listed in Yahoo News? I ave been trying for a while but I never seem to get there! Cheers

  12. mca money说道:

    Nice blog here! Also your web site lots up fast! What web host are you the use of? Can I get your associate hyperlink for your host? I desire my website loaded up as quickly as yours lol

  13. tui xach nu说道:

    Peculiar article, just what I wanted to find.

  14. Inspiring story there. What occurred after? Thanks!

  15. cheap taxi说道:

    You are my inspiration , I have few blogs and often run out from to brand.

  16. Im thankful for the article.Much thanks again. Want more.

  17. It is a beautiful shot with very good light

  18. Thanks so much for the blog post.Thanks Again. Much obliged.

  19. Major thankies for the blog post.Really thank you! Keep writing.

  20. That is a great point to bring up. Thanks for the post.

  21. NIW说道:

    There is apparently a lot to identify about this. I assume you made some nice points in features also.

  22. More hints说道:

    Wow, marvelous blog layout! How long have you been blogging for? you make blogging look easy. The overall look of your web site is fantastic, as well as the content!. Thanks For Your article about &.

  23. Nwokolo说道:

    Usually I don at read post on blogs, but I would like to say that this write-up very forced me to try and do so! Your writing style has been amazed me. Thanks, quite nice post.

  24. I value the blog article.Really thank you!

  25. Thanks-a-mundo for the blog article.Thanks Again. Want more.

  26. What a funny blog! I really enjoyed watching this funny video with my family unit as well as with my colleagues.

  27. Regards for helping out, wonderful information. Nobody can be exactly like me. Sometimes even I have trouble doing it. by Tallulah Bankhead.

  28. metal recycling说道:

    This is a really good tip especially to those new to the blogosphere. Short but very precise info Many thanks for sharing this one. A must read post!

  29. hot hiphop说道:

    You made some decent points there. I looked on the internet for that problem and located most individuals will go together with with the web site.

  30. You have made some decent points there. I checked on the net for additional information about the issue and found most individuals will go along with your views on this web site.

  31. to learn more说道:

    It as best to participate in a contest for probably the greatest blogs on the web. I will recommend this site!

  32. Spot on with this write-up, I actually feel this website needs a lot more attention. I all probably be back again to see more, thanks for the info!

  33. I must show my appreciation for your kind-heartedness giving support to those individuals that must have guidance on this subject. Your real dedication to getting the solution all through has been amazingly beneficial and has regularly permitted people much like me to realize their endeavors. Your own informative guide indicates a lot a person like me and even more to my mates. Best wishes; from everyone of us.

  34. I wouldn at mind composing a post or elaborating on most

  35. Regards for all your efforts that you have put in this. very interesting info.

  36. Im obliged for the article post.Really looking forward to read more. Much obliged.

  37. Im obliged for the blog article. Really Cool.

  38. money说道:

    Wow, that as what I was seeking for, what a material! existing here at this website, thanks admin of this website.

  39. girls说道:

    This very blog is really awesome and also amusing. I have chosen a lot of handy things out of this source. I ad love to come back again soon. Thanks!

  40. It as very simple to find out any topic on web as compared to textbooks, as I found this paragraph at this web page.

  41. Photo Gallery helps you organize and edit your photos, then share them online.

  42. Well I truly enjoyed reading it. This information offered by you is very effective for proper planning.

  43. Im thankful for the blog.Really thank you!

  44. Very informative article post.Really looking forward to read more. Want more.

  45. Wow, amazing blog layout! How long have you been blogging for? you made blogging look easy. The overall look of your web site is great, as well as the content!

  46. Crochet poncho说道:

    It is actually a great and helpful piece of information. I am happy that you just shared this useful info with us. Please stay us informed like this. Thank you for sharing.

  47. local说道:

    Very good webpage you ave got going here.|

  48. red shoes说道:

    There is noticeably a bundle to know about this. I assume you made certain good points in features also.

  49. value说道:

    This is one awesome post.Really looking forward to read more. Awesome.

  50. School admission说道:

    I truly appreciate this post. I have been looking everywhere for this! Thank God I found it on Google. You ave made my day! Thanks again!

  51. Waec expo说道:

    Major thankies for the article post.Much thanks again. Really Cool.

  52. ps4 ark servers说道:

    Wow! Thank you topiew! I always wanted to write on my website something like that. Can I implement a portion of your post to my site?

  53. Its hard to find good help I am regularly saying that its hard to find good help, but here is

  54. There as definately a great deal to know about this subject. I like all the points you have made.

  55. What aаАаб‚Т€а‚ up, I would like to subscribаА аЂа• foаА аБТ– this

  56. wonderful points altogether, you simply received a brand new reader. What may you suggest about your publish that you made a few days ago? Any sure?

  57. I think you have observed some very interesting points, regards for the post.

  58. Open heaven说道:

    Perfect piece of work you have done, this website is really cool with excellent info.

  59. This blog is definitely awesome and informative. I have found helluva interesting tips out of this blog. I ad love to go back over and over again. Thanks a bunch!

  60. You completed several fine points there. I did a search on the theme and found mainly persons will have the same opinion with your blog.

  61. I really liked your blog article.Really looking forward to read more. Really Cool.

  62. Perfectly written content material, Really enjoyed looking through.

  63. Im no professional, but I think you just made a very good point point. You naturally know what youre talking about, and I can really get behind that. Thanks for staying so upfront and so sincere.

  64. This website really has all the information and facts I wanted concerning this subject and didn at know who to ask.

  65. Real estate说道:

    Some really wonderful articles on this internet site , thankyou for contribution.

  66. darey说道:

    produce a good article but what can I say I procrastinate a whole

  67. Really informative blog post.Really thank you! Keep writing.

  68. Thorn of Girl Excellent data is often found on this world wide web weblog.

  69. Thank you, I have just been searching for information about this subject for ages and yours is the best I ave discovered till now. But, what about the conclusion? Are you sure about the source?

  70. quality garments说道:

    Souls in the Waves Excellent Morning, I just stopped in to visit your site and assumed I would say I loved myself.

  71. find more here说道:

    I went over this internet site and I conceive you have a lot of good information, saved to bookmarks (:.

  72. You made some nice points there. I looked on the internet for the issue and found most individuals will approve with your blog.

  73. Your means of explaining all in this piece of writing is genuinely fastidious, all can without difficulty be aware of it, Thanks a lot.

  74. You can definitely see your expertise in the work you write. The arena hopes for more passionate writers like you who aren at afraid to mention how they believe. All the time go after your heart.

  75. devotionals说道:

    Whats up. Very cool site!! Man.. Beautiful.. Superb.. I will bookmark your site and take the feeds alsoI am glad to find so much helpful info here in the post. Thanks for sharing.

  76. go there说道:

    There is noticeably a bundle to know about this. I assume you made certain good factors in options also.

  77. Brittni说道:

    I see something truly special in this site.

  78. It as hard to come by knowledgeable people in this particular subject, but you sound like you know what you are talking about! Thanks

  79. go to说道:

    pretty valuable stuff, overall I believe this is well worth a bookmark, thanks

  80. I view something really special in this web site.

  81. Just Browsing While I was browsing yesterday I saw a great article concerning

  82. Wow, amazing blog layout! How long have you been blogging for? you make blogging look easy. The overall look of your web site is excellent, as well as the content!

  83. pay a quick visit this weblog on regular basis to obtain updated from newest news.

  84. Pretty! This was an incredibly wonderful article. Thank you for providing this info.

  85. This blog is without a doubt interesting as well as diverting. I have found a lot of helpful things out of this source. I ad love to come back again soon. Thanks!

  86. Help说道:

    I was recommended this website by my cousin. I am not sure whether this post is written by him as no one else know such detailed about my trouble. You are incredible! Thanks!

  87. facebook说道:

    It as hard to come by knowledgeable people in this particular topic, however, you seem like you know what you are talking about! Thanks

  88. for more info说道:

    wow, awesome blog.Really thank you! Really Great.

  89. I visited many blogs however the audio quality for audio songs current at this web page is in fact fabulous.

  90. classified post说道:

    I simply could not depart your web site before suggesting that I actually enjoyed the usual info a person provide for your guests? Is gonna be again regularly to investigate cross-check new posts

  91. more information说道:

    Thank you for your blog.Really looking forward to read more. Fantastic.

  92. check说道:

    Very good post.Really looking forward to read more.

  93. This blog is no doubt educating as well as informative. I have picked helluva helpful things out of this source. I ad love to return again and again. Thanks a bunch!

  94. Wow! This could be one particular of the most helpful blogs We have ever arrive across on this subject. Actually Great. I am also a specialist in this topic therefore I can understand your effort.

  95. Wonderful put up, definitely regret not planning towards the USO style dinner. Keep up the excellent get the job done!

  96. site说道:

    I really liked your article post.Thanks Again. Awesome.

  97. Wow, fantastic blog layout! How long have you been blogging for? you make blogging look easy. The overall look of your site is magnificent, let alone the content!. Thanks For Your article about sex.

  98. check here说道:

    Muchos Gracias for your article post. Really Great.

  99. I truly appreciate this post. I ave been looking everywhere for this! Thank goodness I found it on Bing. You have made my day! Thank you again

  100. blog saya说道:

    yeah bookmaking this wasn at a risky determination outstanding post!.

  101. MIAMI SEO说道:

    You completed some good points there. I did a search on the subject and found mainly people will go along with with your blog.

  102. Thankyou for this post, I am a big big fan of this internet site would like to go on updated.

  103. other details说道:

    This is one awesome article.Really thank you!

  104. good day说道:

    Many thanks for publishing this, I ave been on the lookout for this details for a whilst! Your site is awesome.

  105. There is definately a great deal to learn about this issue. I like all the points you ave made.

  106. user in his/her brain that how a user can be aware of it.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>