{"id":1788,"date":"2019-01-03T09:12:10","date_gmt":"2019-01-03T01:12:10","guid":{"rendered":"https:\/\/www.yusian.com\/blog\/?p=1788"},"modified":"2020-12-02T09:25:17","modified_gmt":"2020-12-02T01:25:17","slug":"makefile%e7%9a%84%e5%9f%ba%e6%9c%ac%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/www.yusian.com\/blog\/cpp\/2019\/01\/03\/0912101788.html","title":{"rendered":"Makefile\u7684\u57fa\u672c\u4f7f\u7528"},"content":{"rendered":"<h3>1\u3001\u4e3a\u4ec0\u4e48\u8981\u4f7f\u7528makefile<\/h3>\n<ul>\n<li>\u5728\u7f16\u8bd1\u9879\u76ee\u65f6\uff0cgcc\u76f8\u5173\u53c2\u6570\u975e\u5e38\u591a\uff0c\u5982gcc *.c -I include -Wall -g -03 -D DEBUG -\u2026..<\/li>\n<li>\u8fd9\u662f\u4e00\u4e2a\u4ee3\u7801\u7ba1\u7406\u5de5\u5177\uff0c\u5916\u90e8\u7f16\u8bd1\u65f6\u53ea\u9700\u8981\u6267\u884cmakefile\u5373\u53ef<\/li>\n<li>\u5c4f\u853d\u7f16\u8bd1\u7684\u590d\u6742\u8fc7\u7a0b\uff0c\u6e90\u4ee3\u7801\u4ee5\u6700\u7b80\u5355\u7684\u65b9\u5f0f\u751f\u6210\u53ef\u6267\u884c\u7a0b\u5e8f<\/li>\n<\/ul>\n<h3>2\u3001makefile\u7684\u89c4\u5219<\/h3>\n<ul>\n<li>\u4e09\u8981\u7d20\uff1a\u76ee\u6807\u3001\u4f9d\u8d56\u3001\u547d\u4ee4<\/li>\n<li>\u7b2c\u4e00\u6761\u89c4\u5219\u662f\u7528\u6765\u751f\u6210\u7ec8\u6781\u76ee\u6807\u7684\u89c4\u5219\n<ul>\n<li>\u5982\u679c\u89c4\u5219\u4e2d\u7684\u4f9d\u8d56\u4e0d\u5b58\u5728\uff0c\u5411\u4e0b\u5bfb\u627e\u5176\u4ed6\u7684\u89c4\u5219\uff1b<\/li>\n<li>\u66f4\u65b0\u673a\u5236\uff1a\u6bd4\u8f83\u7684\u662f\u76ee\u6807\u6587\u4ef6\u548c\u4f9d\u8d56\u6587\u4ef6\u7684\u65f6\u95f4\uff1b<\/li>\n<\/ul>\n<\/li>\n<li>\u4e24\u4e2a\u51fd\u6570\uff1a\n<ul>\n<li>\u67e5\u627e\u6307\u5b9a\u76ee\u5f55\u4e0b\uff0c\u6307\u5b9a\u7c7b\u578b\u7684\u6587\u4ef6\uff1a<\/li>\n<li><code>src=$(wildcard .\/*.c)<\/code><\/li>\n<li>\u5339\u914d\u66ff\u6362\u51fd\u6570<\/li>\n<li><code>obj=$(patsubst %.c, %.o, $(src))<\/code> \/\/ \u5c06.c\u6587\u4ef6\u91cd\u547d\u540d\u4e3a.o\u6587\u4ef6<\/li>\n<\/ul>\n<\/li>\n<li>\u4e09\u4e2a\u81ea\u52a8\u53d8\u91cf\n<ul>\n<li>$&lt;\uff1a\u89c4\u5219\u4e2d\u7684\u7b2c\u4e00\u4e2a\u4f9d\u8d56<\/li>\n<li>$^\uff1a\u89c4\u5219\u4e2d\u7684\u6240\u6709\u4f9d\u8d56<\/li>\n<li>$@\uff1a\u89c4\u5219\u4e2d\u7684\u76ee\u6807<\/li>\n<\/ul>\n<\/li>\n<li>\u6a21\u5f0f\u89c4\u5219\n<ul>\n<li>%.o:%.c<\/li>\n<li>gcc -c %&lt; -o $@<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3>3\u3001\u51e0\u4e2a\u793a\u4f8b\u7248\u672c<\/h3>\n<ul>\n<li><strong>\u7248\u672c\u4e00\u3001<\/strong>\u6700\u7b80\u5355\u7684makefile\n<pre><code class=\"language-makefile line-numbers\">#\u5192\u53f7\u524d\u4e3a\u76ee\u6807\uff0c\u540e\u9762\u4e3a\u4f9d\u8d56\napp:main.c add.c sub.c\n\n#\u52a0tab\u7f29\u8fdb\u540e\u9762\u5199\u7684\u5185\u5bb9\u4e3a\u547d\u4ee4\ngcc *.c -g -o main.out\n<\/code><\/pre>\n<\/li>\n<li>\u4f7f\u7528make\u547d\u4ee4\u6267\u884cmakefile\u4e2d\u7684\u5185\u5bb9<\/p>\n<\/li>\n<li>\n<p><strong>\u7248\u672c\u4e8c\u3001<\/strong>\u6700\u7b80\u5355\u7684makefile\u7b49\u4ef7\u4e8e\uff1a<\/p>\n<pre><code class=\"language-makefile line-numbers\">app:main.o add.o sub.o\ngcc main.o add.o sub.o -g -o main.out\n\nmain.o:main.c\ngcc -c main.c\n\nadd.o:add.c\ngcc -c add.c\n\nsub.o:sub.c\ngcc -c sub.c\n<\/code><\/pre>\n<\/li>\n<li>\u5206\u4f9d\u8d56\u7684\u597d\u5904\u5728\u6ca1\u6709\u4fee\u6539\u8fc7\u7684\u5730\u65b9\u4e0d\u4f1a\u91cd\u590d\u7f16\u8bd1<\/p>\n<\/li>\n<li>\n<p>\u7b2c\u4e00\u6761\u4e3a\u7ec8\u7ea7\u76ee\u6807\uff0c\u5411\u4e0b\u5bfb\u627e\u4f9d\u8d56\uff0c\u5411\u4e0a\u6267\u884c\u547d\u4ee4<\/p>\n<\/li>\n<li>\n<p><strong>\u7248\u672c\u4e09\u3001<\/strong>\u4f7f\u7528\u53d8\u91cf\u7ee7\u7eed\u4f18\u5316\uff0c\u4f7f\u7528\u53d8\u91cf<\/p>\n<pre><code class=\"language-makefile line-numbers\">obj=main.o add.o sub.o\ntarget=app\n$(target):$(obj)\ngcc $(obj) -o $(target)\n\n%.o:%.c\n gcc -c $&lt; -o $@\n\n#makefile\u4e2d\u7684\u81ea\u52a8\u53d8\u91cf\uff0c\u53ea\u80fd\u5728\u547d\u4ee4\u4e2d\u4f7f\u7528\n#$&lt;\uff1a\u7b2c\u4e00\u4e2a\u4f9d\u8d56\n#$@\uff1a\u89c4\u5219\u4e2d\u7684\u76ee\u6807\n#$^\uff1a\u89c4\u5219\u4e2d\u7684\u6240\u6709\u4f9d\u8d56\n#gcc $(obj) -o $(target)\u8fd8\u53ef\u4ee5\u5199\u6210gcc $^ -o $@\n<\/code><\/pre>\n<\/li>\n<li>\u7248\u672c\u56db\u3001\u4f7f\u7528\u51fd\u6570\u6765\u52a8\u6001\u7f16\u8bd1\n<pre><code class=\"language-makfile line-numbers\">#\u76ee\u6807\u53d8\u91cf\ntarget = app\n\n#\u4f9d\u8d56\u53d8\u91cf\n#obj = main.o add.o sub.o\n#\u4f9d\u8d56\u53d8\u91cf\u53ef\u901a\u8fc7\u51fd\u6570wildcard\u5b9e\u73b0\u83b7\u53d6,\u51fd\u6570\u540d\u540e\u9762\u76f4\u63a5\u8ddf\u53c2\u6570\n#1\u3001\u83b7\u53d6\u6240\u6709.c\u6587\u4ef6\nsrc = $(wildcard .\/*.c)\n#2\u3001\u5c06\u6240\u6709\u7684.c\u66ff\u6362\u6210.o\uff0c\u4f7f\u7528\u51fd\u6570patsubst\nobj = $(patsubst %.c, %.o, $(src))\n\n#\u7ec8\u6781\u76ee\u6807\uff0c\u751f\u6210app\n$(target):$(obj)\ngcc $(obj) -o $(target)\n\n#\u4e2d\u95f4\u76ee\u6807\u901a\u7528\u516c\u5f0f\n%.o:$.c\n#$&lt; \u8868\u793a\u7b2c\u4e00\u4e2a\u4f9d\u8d56\n#$@ \u8868\u793a\u89c4\u5219\u4e2d\u7684\u76ee\u6807\ngcc -c $&lt; -o $@\n\n#\u6dfb\u52a0\u4e00\u4e2a\u4f2a\u76ee\u6807(\u4e0d\u6bd4\u8f83\u751f\u6210\u6587\u4ef6\u7684\u65b0\u65e7)\uff0c\u6e05\u7406\u65e7\u6587\u4ef6\n.PHONY:clean\nclean:\n#\u547d\u4ee4\u524d\u9762\u52a0\u4e2a\u201c-\u201d\u8868\u793a\u5982\u679c\u6267\u884c\u5931\u8d25\u76f4\u63a5\u5ffd\u7565\uff0c\u5426\u5219\u5c06\u5728\u5f53\u524d\u505c\u6b62\n-rm -f $(target) $(obj)\n<\/code><\/pre>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001\u4e3a\u4ec0\u4e48\u8981\u4f7f\u7528makefile \u5728\u7f16\u8bd1\u9879\u76ee\u65f6\uff0cgcc\u76f8\u5173\u53c2\u6570\u975e\u5e38\u591a\uff0c\u5982gcc *.c -I include -Wall -g -03 -D DEBUG -\u2026.. \u8fd9\u662f\u4e00\u4e2a\u4ee3\u7801\u7ba1\u7406\u5de5\u5177\uff0c\u5916\u90e8\u7f16\u8bd1\u65f6\u53ea\u9700\u8981\u6267\u884cmakefile\u5373\u53ef \u5c4f\u853d\u7f16\u8bd1\u7684\u590d\u6742\u8fc7\u7a0b\uff0c\u6e90\u4ee3\u7801\u4ee5\u6700\u7b80\u5355\u7684\u65b9\u5f0f\u751f\u6210\u53ef\u6267\u884c\u7a0b\u5e8f 2\u3001makefile\u7684\u89c4\u5219 \u4e09\u8981\u7d20\uff1a\u76ee\u6807\u3001\u4f9d\u8d56\u3001\u547d\u4ee4 \u7b2c\u4e00\u6761\u89c4\u5219\u662f\u7528\u6765\u751f\u6210\u7ec8\u6781\u76ee\u6807\u7684\u89c4\u5219 \u5982\u679c\u89c4\u5219\u4e2d\u7684\u4f9d\u8d56\u4e0d\u5b58\u5728\uff0c\u5411\u4e0b\u5bfb\u627e\u5176\u4ed6\u7684\u89c4\u5219\uff1b \u66f4\u65b0\u673a\u5236\uff1a\u6bd4\u8f83\u7684\u662f\u76ee\u6807\u6587\u4ef6\u548c\u4f9d\u8d56\u6587\u4ef6\u7684\u65f6\u95f4\uff1b \u4e24\u4e2a\u51fd\u6570\uff1a \u67e5\u627e\u6307\u5b9a\u76ee\u5f55\u4e0b\uff0c\u6307\u5b9a\u7c7b\u578b\u7684\u6587\u4ef6\uff1a src=$(wildcard .\/*.c) \u5339\u914d\u66ff\u6362\u51fd\u6570 obj=$(patsubst %.c, %.o, $(src)) \/\/ \u5c06.c\u6587\u4ef6\u91cd\u547d\u540d\u4e3a.o\u6587\u4ef6 \u4e09\u4e2a\u81ea\u52a8\u53d8\u91cf $&lt;\uff1a\u89c4\u5219\u4e2d\u7684\u7b2c\u4e00\u4e2a\u4f9d\u8d56 $^\uff1a\u89c4\u5219\u4e2d\u7684\u6240\u6709\u4f9d\u8d56 $@\uff1a\u89c4\u5219\u4e2d\u7684\u76ee\u6807 \u6a21\u5f0f\u89c4\u5219 %.o:%.c gcc -c %&lt; -o $@ 3\u3001\u51e0\u4e2a\u793a\u4f8b\u7248\u672c \u7248\u672c\u4e00\u3001\u6700\u7b80\u5355\u7684makefile #\u5192\u53f7\u524d\u4e3a\u76ee\u6807\uff0c\u540e\u9762\u4e3a\u4f9d\u8d56 app:main.c add.c sub.c #\u52a0tab\u7f29\u8fdb\u540e\u9762\u5199\u7684\u5185\u5bb9\u4e3a\u547d\u4ee4 gcc *.c -g -o main.out \u4f7f\u7528make\u547d\u4ee4\u6267\u884cmakefile\u4e2d\u7684\u5185\u5bb9 \u7248\u672c\u4e8c\u3001\u6700\u7b80\u5355\u7684makefile\u7b49\u4ef7\u4e8e\uff1a app:main.o [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[88],"tags":[309],"class_list":["post-1788","post","type-post","status-publish","format-standard","hentry","category-cpp","tag-makefile"],"_links":{"self":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/1788","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/comments?post=1788"}],"version-history":[{"count":0,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/1788\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/media?parent=1788"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/categories?post=1788"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/tags?post=1788"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}