{"id":737,"date":"2016-04-20T22:50:22","date_gmt":"2016-04-21T03:50:22","guid":{"rendered":"http:\/\/zhanxw.com\/blog\/?p=737"},"modified":"2016-04-20T22:50:22","modified_gmt":"2016-04-21T03:50:22","slug":"makefile-%e7%ac%94%e8%ae%b0","status":"publish","type":"post","link":"https:\/\/zhanxw.com\/blog\/2016\/04\/makefile-%e7%ac%94%e8%ae%b0\/","title":{"rendered":"Makefile \u7b14\u8bb0"},"content":{"rendered":"<p>Some Notes about Makefile<\/p>\n<p>\u8bb0\u5f55\u4e00\u4e0b\u6700\u8fd1\u7528Makefile\u7684\u7ecf\u9a8c\u548c\u6559\u8bad<br \/>\n\u5185\u5bb9\u6bd4\u8f83\u51cc\u4e71\u3002\u653e\u5728\u8fd9\u91cc\u4f9b\u4ee5\u540e\u53c2\u8003\u3002<\/p>\n<p>1. Target-specific variable\u7684\u4f5c\u7528\u57df<\/p>\n<p>Target-specific variable\u6307\u7684\u662f\u53d8\u91cf\u5728\u67d0\u4e9b\u7279\u5b9a\u76ee\u6807\u4e0b\u7684\u503c\u3002\u7528\u6cd5\u662f\uff1a<\/p>\n<p><code>target \u2026 : variable-assignment<br \/>\n<\/code><br \/>\n\u6ce8\u610f\u8fd9\u91cc\u7684\u53d8\u91cf\u4f5c\u7528\u57df\u53ea\u662f\u5728Recipe\u90e8\u5206\uff0c\u800c\u4e0d\u80fd\u5728\u76ee\u6807(Target)\u6216\u8005\u4f9d\u8d56(Prerequisites)\u90e8\u5206\u3002<\/p>\n<p><code>target \u2026 : prerequisites \u2026<br \/>\n        recipe<br \/>\n        \u2026<br \/>\n        \u2026<\/code><\/p>\n<p>\u5c31\u662f\u8bf4\u8fd9\u4e2aTarget-specific\u53d8\u91cf\u7684\u503c\u4ec5\u5728recipe\u4e2d\u6709\u6548\u3002<\/p>\n<p>2. \u9664\u9519\u7684\u65b9\u6cd5<\/p>\n<p>\u7528warning \u51fd\u6570\uff0c\u6bd4\u5982$(warning msg)\u3002\u5b83\u53ef\u4ee5\u51fa\u73b0\u5728\u5404\u4e2a\u90e8\u5206\uff1a \u76ee\u6807\uff0c \u4f9d\u8d56\uff0crecipe\uff0c\u9876\u7ea7\u53d8\u91cf\uff0c \u90fd\u884c \uff08\u53c2\u89c1\uff3b2\uff3d\uff09\uff1a<\/p>\n<p><code>$(warning A top-level warning)<\/p>\n<p>FOO := $(warning Right-hand side of a simple variable)bar<br \/>\nBAZ = $(warning Right-hand side of a recursive variable)boo<\/p>\n<p>$(warning A target)target: $(warning In a prerequisite list)makefile $(BAZ)<br \/>\n        $(warning In a command script)<br \/>\n        ls<br \/>\n$(BAZ):<br \/>\n<\/code><br \/>\n\u8f93\u51fa\uff1a<br \/>\n<code>$ make<br \/>\nmakefile:1: A top-level warning<br \/>\nmakefile:2: Right-hand side of a simple variable<br \/>\nmakefile:5: A target<br \/>\nmakefile:5: In a prerequisite list<br \/>\nmakefile:5: Right-hand side of a recursive variable<br \/>\nmakefile:8: Right-hand side of a recursive variable<br \/>\nmakefile:6: In a command script<br \/>\nls<br \/>\nmakefile<\/code><\/p>\n<p>\u8fd8\u53ef\u4ee5\u914d\u5408\u8fd9\u4e9b\u53c2\u6570&#8211;just-print (-n), &#8211;print-data-base (-p), and &#8211;warn-undefined-variables<\/p>\n<p>3. \u7ba1\u7406\u5927\u578b\u9879\u76ee<\/p>\n<p>\u5927\u578b\u9879\u76ee\u7684\u4ee3\u7801\u6709\u5c42\u7ea7\u7ed3\u6784\u3002xieMakefile\u8981\u6ce8\u610f\u4ee3\u7801\u7cbe\u7b80\uff0c\u5584\u7528\u51fd\u6570\uff0c\u5c3d\u91cf\u81ea\u52a8\u5316\uff0c\u907f\u514d\u91cd\u590d\u4ee3\u7801\uff0c\u68c0\u67e5\u53d8\u91cf\u7684\u6709\u6548\u6027\u3002<\/p>\n<p>\u4f8b\u5982\uff1a<br \/>\n<code>create-output-directories :=                            \\<br \/>\n        $(shell for f in $(modules);                    \\<br \/>\n                do                                      \\<br \/>\n                  $(TEST) -d $$f || $(MKDIR) $$f;       \\<br \/>\n                done)<br \/>\n<\/code><br \/>\n\u4f8b\u5982\u7528call, eval, foreach\u7b49\u51fd\u6570\uff1a <\/p>\n<p><code># use foreach and test<br \/>\n.PHONY: validate_build<br \/>\nvalidate_build:<br \/>\n        test $(foreach f,$(RELEASE_FILES),-s $f -a) -e .<\/p>\n<p>local_src := $(addprefix $(subdirectory)\/,playlist.y scanner.l)<\/p>\n<p># create library<br \/>\ndefine make-library<br \/>\n  libraries += $(BINARY_DIR)\/$1<br \/>\n  sources   += $2<\/p>\n<p>  $(BINARY_DIR)\/$1: $(call source-dir-to-binary-dir,    \\<br \/>\n                      $(subst .c,.o,$(filter %.c,$2))   \\<br \/>\n                      $(subst .y,.o,$(filter %.y,$2))   \\<br \/>\n                      $(subst .l,.o,$(filter %.l,$2)))<br \/>\n        $(AR) $(ARFLAGS) $$@ $$^<br \/>\nendef<br \/>\n$(eval $(call make-library, $(subdirectory)\/libdb.a, $(local_src)))<\/p>\n<p><\/code><br \/>\n\u53c2\u8003<br \/>\n\uff3b1\uff3dTarget-specific variables. GNU make. (<a href=\"https:\/\/www.gnu.org\/software\/make\/manual\/html_node\/Target_002dspecific.html\" target=\"_blank\">link<\/a>)<br \/>\n\uff3b2\uff3dDebugging Makefiles, Managing Projects with GNU Make, 3rd Edition, Chapter 12. (<a href=\"https:\/\/www.safaribooksonline.com\/library\/view\/managing-projects-with\/0596006101\/ch12.html\" target=\"_blank\">link<\/a>)<br \/>\n\uff3b3\uff3dC and C++, Managing Projects with GNU Make, 3rd Edition, Chapter 8. (<a href=\"https:\/\/www.safaribooksonline.com\/library\/view\/managing-projects-with\/0596006101\/ch08.html\" target=\"_blank\">Link<\/a>)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Some Notes about Makefile \u8bb0\u5f55\u4e00\u4e0b\u6700\u8fd1\u7528Makefile\u7684\u7ecf\u9a8c\u548c\u6559\u8bad \u5185\u5bb9\u6bd4\u8f83\u51cc\u4e71\u3002\u653e\u5728\u8fd9\u91cc\u4f9b\u4ee5\u540e\u53c2\u8003\u3002 1. Target-specific variable\u7684\u4f5c\u7528\u57df Target-specific variable\u6307\u7684\u662f\u53d8\u91cf\u5728\u67d0\u4e9b\u7279\u5b9a\u76ee\u6807\u4e0b\u7684\u503c\u3002\u7528\u6cd5\u662f\uff1a target \u2026 : variable-assignment \u6ce8\u610f\u8fd9\u91cc\u7684\u53d8\u91cf\u4f5c\u7528\u57df\u53ea\u662f\u5728Recipe\u90e8\u5206\uff0c\u800c\u4e0d\u80fd\u5728\u76ee\u6807(Target)\u6216\u8005\u4f9d\u8d56(Prerequisites)\u90e8\u5206\u3002 target \u2026 : prerequisites \u2026 recipe \u2026 \u2026 \u5c31\u662f\u8bf4\u8fd9\u4e2aTarget-specific\u53d8\u91cf\u7684\u503c\u4ec5\u5728recipe\u4e2d\u6709\u6548\u3002 2. \u9664\u9519\u7684\u65b9\u6cd5 \u7528warning \u51fd\u6570\uff0c\u6bd4\u5982$(warning msg)\u3002\u5b83\u53ef\u4ee5\u51fa\u73b0\u5728\u5404\u4e2a\u90e8\u5206\uff1a \u76ee\u6807\uff0c \u4f9d\u8d56\uff0crecipe\uff0c\u9876\u7ea7\u53d8\u91cf\uff0c \u90fd\u884c \uff08\u53c2\u89c1\uff3b2\uff3d\uff09\uff1a $(warning A top-level warning) FOO := $(warning Right-hand side of a simple variable)bar BAZ = $(warning Right-hand side of a recursive [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[150,72],"class_list":["post-737","post","type-post","status-publish","format-standard","hentry","category-code","tag-make","tag-makefile"],"_links":{"self":[{"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/posts\/737","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/comments?post=737"}],"version-history":[{"count":0,"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/posts\/737\/revisions"}],"wp:attachment":[{"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/media?parent=737"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/categories?post=737"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/tags?post=737"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}