{"id":814,"date":"2017-12-14T16:17:54","date_gmt":"2017-12-14T08:17:54","guid":{"rendered":"https:\/\/damogame.cn:15443\/wordpress\/?p=814"},"modified":"2017-12-14T16:17:54","modified_gmt":"2017-12-14T08:17:54","slug":"makefile%e4%b8%ad%e7%9a%84wildcard%e7%94%a8%e6%b3%95","status":"publish","type":"post","link":"https:\/\/i007.cc\/wordpress\/archives\/814","title":{"rendered":"Makefile\u4e2d\u7684wildcard\u7528\u6cd5"},"content":{"rendered":"<p style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">\u539f\u521b\u00a0<\/span><span style=\"font-weight: 400;\">2011\u5e7403\u670822\u65e5 11:33:00<\/span><\/p>\n<ul style=\"font-weight: 400;\" data-mod=\"popu_377\">\n<li style=\"font-weight: 400;\">\u6807\u7b7e\uff1a<\/li>\n<li style=\"font-weight: 400;\"><a style=\"font-weight: 400;\" href=\"http:\/\/so.csdn.net\/so\/search\/s.do?q=wildcard&amp;t=blog\">wildcard<\/a>\u00a0<span style=\"font-weight: 400;\">\/<\/span><\/li>\n<li style=\"font-weight: 400;\"><a style=\"font-weight: 400;\" href=\"http:\/\/so.csdn.net\/so\/search\/s.do?q=makefile&amp;t=blog\">makefile<\/a>\u00a0<span style=\"font-weight: 400;\">\/<\/span><\/li>\n<li style=\"font-weight: 400;\"><a style=\"font-weight: 400;\" href=\"http:\/\/so.csdn.net\/so\/search\/s.do?q=%E5%B7%A5%E4%BD%9C&amp;t=blog\">\u5de5\u4f5c<\/a>\u00a0<span style=\"font-weight: 400;\">\/<\/span><\/li>\n<li style=\"font-weight: 400;\"><a style=\"font-weight: 400;\" href=\"http:\/\/so.csdn.net\/so\/search\/s.do?q=%E6%B5%8B%E8%AF%95&amp;t=blog\">\u6d4b\u8bd5<\/a>\u00a0<span style=\"font-weight: 400;\">\/<\/span><\/li>\n<li style=\"font-weight: 400;\"><a style=\"font-weight: 400;\" href=\"http:\/\/so.csdn.net\/so\/search\/s.do?q=%E6%B1%87%E7%BC%96&amp;t=blog\">\u6c47\u7f16<\/a>\u00a0<span style=\"font-weight: 400;\">\/<\/span><\/li>\n<li style=\"font-weight: 400;\"><a style=\"font-weight: 400;\" href=\"http:\/\/so.csdn.net\/so\/search\/s.do?q=%E6%89%A9%E5%B1%95&amp;t=blog\">\u6269\u5c55<\/a><\/li>\n<\/ul>\n<ul style=\"font-weight: 400;\">\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">104756<\/span><\/li>\n<\/ul>\n<p style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">\u5728Makefile\u89c4\u5219\u4e2d\uff0c\u901a\u914d\u7b26\u4f1a\u88ab\u81ea\u52a8\u5c55\u5f00\u3002\u4f46\u5728\u53d8\u91cf\u7684\u5b9a\u4e49\u548c\u51fd\u6570\u5f15\u7528\u65f6\uff0c\u901a\u914d\u7b26\u5c06\u5931\u6548\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\u5982\u679c\u9700\u8981\u901a\u914d\u7b26\u6709\u6548\uff0c\u5c31\u9700\u8981\u4f7f\u7528\u51fd\u6570\u201cwildcard<\/span><span style=\"font-weight: 400;\">\u201d\uff0c\u5b83\u7684\u7528\u6cd5\u662f\uff1a<\/span><span style=\"font-weight: 400;\">$(wildcard PATTERN&#8230;)<\/span><span style=\"font-weight: 400;\">\u00a0<\/span><span style=\"font-weight: 400;\">\u3002\u5728<\/span><span style=\"font-weight: 400;\">Makefile<\/span><span style=\"font-weight: 400;\">\u4e2d\uff0c\u5b83\u88ab\u5c55\u5f00\u4e3a\u5df2\u7ecf\u5b58\u5728\u7684\u3001\u4f7f\u7528\u7a7a\u683c\u5206\u5f00\u7684\u3001\u5339\u914d\u6b64\u6a21\u5f0f\u7684\u6240\u6709\u6587\u4ef6\u5217\u8868\u3002\u5982\u679c\u4e0d\u5b58\u5728\u4efb\u4f55\u7b26\u5408\u6b64\u6a21\u5f0f\u7684\u6587\u4ef6\uff0c\u51fd\u6570\u4f1a\u5ffd\u7565\u6a21\u5f0f\u5b57\u7b26\u5e76\u8fd4\u56de\u7a7a\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff1a\u8fd9\u79cd\u60c5\u51b5\u4e0b\u89c4\u5219\u4e2d\u901a\u914d\u7b26\u7684\u5c55\u5f00\u548c\u4e0a\u4e00\u5c0f\u8282\u5339\u914d\u901a\u914d\u7b26\u7684\u533a\u522b\u3002<\/span><\/p>\n<p style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">\u4e00\u822c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u201c<\/span><span style=\"font-weight: 400;\">$(wildcard *.c)\u201d\u6765\u83b7\u53d6\u5de5\u4f5c\u76ee\u5f55\u4e0b\u7684\u6240\u6709\u7684.c<\/span><span style=\"font-weight: 400;\">\u6587\u4ef6\u5217\u8868\u3002\u590d\u6742\u4e00\u4e9b\u7528\u6cd5\uff1b\u53ef\u4ee5\u4f7f\u7528\u201c<\/span><span style=\"font-weight: 400;\">$(patsubst %.c,%.o,$(wildcard *.c))<\/span><span style=\"font-weight: 400;\">\u201d\uff0c\u9996\u5148\u4f7f\u7528\u201c<\/span><span style=\"font-weight: 400;\">wildcard<\/span><span style=\"font-weight: 400;\">\u201d\u51fd\u6570\u83b7\u53d6\u5de5\u4f5c\u76ee\u5f55\u4e0b\u7684<\/span><span style=\"font-weight: 400;\">.c<\/span><span style=\"font-weight: 400;\">\u6587\u4ef6\u5217\u8868\uff1b\u4e4b\u540e\u5c06\u5217\u8868\u4e2d\u6240\u6709\u6587\u4ef6\u540d\u7684\u540e\u7f00<\/span><span style=\"font-weight: 400;\">.c<\/span><span style=\"font-weight: 400;\">\u66ff\u6362\u4e3a<\/span><span style=\"font-weight: 400;\">.o<\/span><span style=\"font-weight: 400;\">\u3002\u8fd9\u6837\u6211\u4eec\u5c31\u53ef\u4ee5\u5f97\u5230\u5728\u5f53\u524d\u76ee\u5f55\u53ef\u751f\u6210\u7684<\/span><span style=\"font-weight: 400;\">.o<\/span><span style=\"font-weight: 400;\">\u6587\u4ef6\u5217\u8868\u3002\u56e0\u6b64\u5728\u4e00\u4e2a\u76ee\u5f55\u4e0b\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u5185\u5bb9\u7684<\/span><span style=\"font-weight: 400;\">Makefile<\/span><span style=\"font-weight: 400;\">\u6765\u5c06\u5de5\u4f5c\u76ee\u5f55\u4e0b\u7684\u6240\u6709\u7684<\/span><span style=\"font-weight: 400;\">.c<\/span><span style=\"font-weight: 400;\">\u6587\u4ef6\u8fdb\u884c\u7f16\u8bd1\u5e76\u6700\u540e\u8fde\u63a5\u6210\u4e3a\u4e00\u4e2a\u53ef\u6267\u884c\u6587\u4ef6\uff1a<\/span><\/p>\n<p style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">\u00a0<\/span><\/p>\n<p style=\"font-weight: 400;\"><b><strong><i><em><span style=\"font-weight: 400;\">#sample Makefile<\/span><\/em><\/i><\/strong><\/b><\/p>\n<p style=\"font-weight: 400;\"><b><strong><i><em><span style=\"font-weight: 400;\">objects := $(patsubst %.c,%.o,$(wildcard *.c))<\/span><\/em><\/i><\/strong><\/b><\/p>\n<p style=\"font-weight: 400;\"><b><strong><i><em><span style=\"font-weight: 400;\">\u00a0<\/span><\/em><\/i><\/strong><\/b><\/p>\n<p style=\"font-weight: 400;\"><b><strong><i><em><span style=\"font-weight: 400;\">foo : $(objects)<\/span><\/em><\/i><\/strong><\/b><\/p>\n<p style=\"font-weight: 400;\"><b><strong><i><em><span style=\"font-weight: 400;\">cc -o foo $(objects)<\/span><\/em><\/i><\/strong><\/b><\/p>\n<p style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">\u00a0<\/span><\/p>\n<p style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">\u8fd9\u91cc\u6211\u4eec\u4f7f\u7528\u4e86<\/span><span style=\"font-weight: 400;\">make<\/span><span style=\"font-weight: 400;\">\u7684\u9690\u542b\u89c4\u5219\u6765\u7f16\u8bd1<\/span><span style=\"font-weight: 400;\">.c<\/span><span style=\"font-weight: 400;\">\u7684\u6e90\u6587\u4ef6\u3002\u5bf9\u53d8\u91cf\u7684\u8d4b\u503c\u4e5f\u7528\u5230\u4e86\u4e00\u4e2a\u7279\u6b8a\u7684\u7b26\u53f7\uff08<\/span><span style=\"font-weight: 400;\">:=<\/span><span style=\"font-weight: 400;\">\uff09\u3002<\/span><\/p>\n<p style=\"font-weight: 400;\">\n<p style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">1\u3001wildcard : \u6269\u5c55\u901a\u914d\u7b26<br \/>\n2\u3001notdir \uff1a \u53bb\u9664\u8def\u5f84<br \/>\n3\u3001patsubst \uff1a\u66ff\u6362\u901a\u914d\u7b26<\/span><\/p>\n<p style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">\u4f8b\u5b50\uff1a<br \/>\n\u5efa\u7acb\u4e00\u4e2a\u6d4b\u8bd5\u76ee\u5f55\uff0c\u5728\u6d4b\u8bd5\u76ee\u5f55\u4e0b\u5efa\u7acb\u4e00\u4e2a\u540d\u4e3asub\u7684\u5b50\u76ee\u5f55<br \/>\n$ mkdir test<br \/>\n$ cd test<br \/>\n$ mkdir sub<\/span><\/p>\n<p style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">\u5728test\u4e0b\uff0c\u5efa\u7acba.c\u548cb.c2\u4e2a\u6587\u4ef6\uff0c\u5728sub\u76ee\u5f55\u4e0b\uff0c\u5efa\u7acbsa.c\u548csb.c2 \u4e2a\u6587\u4ef6<\/span><\/p>\n<p style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">\u5efa\u7acb\u4e00\u4e2a\u7b80\u5355\u7684Makefile<br \/>\nsrc=$(wildcard *.c .\/sub\/*.c)<br \/>\ndir=$(notdir $(src))<br \/>\nobj=$(patsubst %.c,%.o,$(dir) )<\/span><\/p>\n<p style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">all:<br \/>\n@echo $(src)<br \/>\n@echo $(dir)<br \/>\n@echo $(obj)<br \/>\n@echo &#8220;end&#8221;<\/p>\n<p>\u6267\u884c\u7ed3\u679c\u5206\u6790\uff1a<br \/>\n\u7b2c\u4e00\u884c\u8f93\u51fa\uff1a<br \/>\na.c b.c .\/sub\/sa.c .\/sub\/sb.c<\/span><\/p>\n<p style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">wildcard\u628a \u6307\u5b9a\u76ee\u5f55 .\/ \u548c .\/sub\/ \u4e0b\u7684\u6240\u6709\u540e\u7f00\u662fc\u7684\u6587\u4ef6\u5168\u90e8\u5c55\u5f00\u3002<\/span><\/p>\n<p style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">\u7b2c\u4e8c\u884c\u8f93\u51fa\uff1a<br \/>\na.c b.c sa.c sb.c<br \/>\nnotdir\u628a\u5c55\u5f00\u7684\u6587\u4ef6\u53bb\u9664\u6389\u8def\u5f84\u4fe1\u606f<\/span><\/p>\n<p style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">\u7b2c\u4e09\u884c\u8f93\u51fa\uff1a<br \/>\na.o b.o sa.o sb.o<\/span><\/p>\n<p style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">\u5728$(patsubst %.c,%.o,$(dir) )\u4e2d\uff0cpatsubst\u628a$(dir)\u4e2d\u7684\u53d8\u91cf\u7b26\u5408\u540e\u7f00\u662f.c\u7684\u5168\u90e8\u66ff\u6362\u6210.o\uff0c<br \/>\n\u4efb\u4f55\u8f93\u51fa\u3002<br \/>\n\u6216\u8005\u53ef\u4ee5\u4f7f\u7528<br \/>\nobj=$(dir:%.c=%.o)<br \/>\n\u6548\u679c\u4e5f\u662f\u4e00\u6837\u7684\u3002<\/span><\/p>\n<p style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">\u8fd9\u91cc\u7528\u5230makefile\u91cc\u7684\u66ff\u6362\u5f15\u7528\u89c4\u5219\uff0c\u5373\u7528\u60a8\u6307\u5b9a\u7684\u53d8\u91cf\u66ff\u6362\u53e6\u4e00\u4e2a\u53d8\u91cf\u3002<br \/>\n\u5b83\u7684\u6807\u51c6\u683c\u5f0f\u662f<br \/>\n$(var:a=b) \u6216 ${var:a=b}<br \/>\n\u5b83\u7684\u542b\u4e49\u662f\u628a\u53d8\u91cfvar\u4e2d\u7684\u6bcf\u4e00\u4e2a\u503c\u7ed3\u5c3e\u7528b\u66ff\u6362\u6389a<\/span><\/p>\n<p style=\"font-weight: 400;\">\n<p style=\"font-weight: 400;\">\n\u4eca\u5929\u5728\u7814\u7a76makefile\u65f6\u5728\u7f51\u4e0a\u770b\u5230\u4e00\u7bc7\u6587\u7ae0\uff0c\u4ecb\u7ecd\u4e86\u4f7f\u7528\u51fd\u6570wildcard\u5f97\u5230\u6307\u5b9a\u76ee\u5f55\u4e0b\u6240\u6709\u7684C\u8bed\u8a00\u6e90\u7a0b\u5e8f\u6587\u4ef6\u540d\u7684\u65b9\u6cd5\uff0c\u8fd9\u4e0b\u597d\u4e86\uff0c\u4e0d\u7528\u624b\u5de5\u4e00\u4e2a\u4e00\u4e2a\u6307\u5b9a\u9700\u8981\u7f16\u8bd1\u7684.c\u6587\u4ef6\u4e86\uff0c\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<p style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">SRC = $(wildcard *.c)<\/span><\/p>\n<p style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">\u7b49\u4e8e\u6307\u5b9a\u7f16\u8bd1\u5f53\u524d\u76ee\u5f55\u4e0b\u6240\u6709.c\u6587\u4ef6\uff0c\u5982\u679c\u8fd8\u6709\u5b50\u76ee\u5f55\uff0c\u6bd4\u5982\u5b50\u76ee\u5f55\u4e3ainc\uff0c\u5219\u518d\u589e\u52a0\u4e00\u4e2awildcard\u51fd\u6570\uff0c\u8c61\u8fd9\u6837\uff1a<\/span><\/p>\n<p style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">SRC = $(wildcard *.c) $(wildcard inc\/*.c)<\/span><\/p>\n<p style=\"font-weight: 400;\">\u4e5f\u53ef\u4ee5\u6307\u5b9a\u6c47\u7f16\u6e90\u7a0b\u5e8f\uff1a<br \/>\nASRC = $(wildcard *.S)<\/p>\n<p style=\"font-weight: 400;\">\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u521b\u00a02011\u5e7403\u670822\u65e5 11:33<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"colormag_page_container_layout":"default_layout","colormag_page_sidebar_layout":"default_layout","footnotes":""},"categories":[],"tags":[118],"class_list":["post-814","post","type-post","status-publish","format-standard","hentry","tag-02-"],"_links":{"self":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/814","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/comments?post=814"}],"version-history":[{"count":0,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/814\/revisions"}],"wp:attachment":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/media?parent=814"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/categories?post=814"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/tags?post=814"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}