{"id":115419,"date":"2018-08-31T08:00:32","date_gmt":"2018-08-31T00:00:32","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=115419"},"modified":"2018-08-24T09:28:24","modified_gmt":"2018-08-24T01:28:24","slug":"magic-make-work","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/magic-make-work.html","title":{"rendered":"\u795e\u5947\u7684Make \u5982\u4f55\u5de5\u4f5c\u7684"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nLinux \u4e0b\u00a0make\u00a0\u547d\u4ee4\u662f\u7cfb\u7edf\u7ba1\u7406\u5458\u548c\u7a0b\u5e8f\u5458\u7528\u7684\u6700\u9891\u7e41\u7684\u547d\u4ee4\u4e4b\u4e00\u3002\u7ba1\u7406\u5458\u7528\u5b83\u901a\u8fc7\u547d\u4ee4\u884c\u6765\u7f16\u8bd1\u548c\u5b89\u88c5\u5f88\u591a\u5f00\u6e90\u7684\u5de5\u5177\uff0c\u7a0b\u5e8f\u5458\u7528\u5b83\u6765\u7ba1\u7406\u4ed6\u4eec\u5927\u578b\u590d\u6742\u7684\u9879\u76ee\u7f16\u8bd1\u95ee\u9898\u3002\u672c\u6587\u6211\u4eec\u5c06\u7528\u4e00\u4e9b\u5b9e\u4f8b\u6765\u8ba8\u8bba make \u547d\u4ee4\u80cc\u540e\u7684\u5de5\u4f5c\u673a\u5236\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u5bf9\u4e8e\u4e0d\u77e5\u9053\u80cc\u540e\u673a\u7406\u7684\u4eba\u6765\u8bf4\uff0cmake \u547d\u4ee4\u50cf\u547d\u4ee4\u884c\u53c2\u6570\u4e00\u6837\u63a5\u6536\u76ee\u6807\u3002\u8fd9\u4e9b\u76ee\u6807\u901a\u5e38\u5b58\u653e\u5728\u4ee5 \u201cMakefile\u201d \u6765\u547d\u540d\u7684\u7279\u6b8a\u6587\u4ef6\u4e2d\uff0c\u540c\u65f6\u6587\u4ef6\u4e5f\u5305\u542b\u4e0e\u76ee\u6807\u76f8\u5bf9\u5e94\u7684\u64cd\u4f5c\u3002\u66f4\u591a\u4fe1\u606f\uff0c\u9605\u8bfb\u5173\u4e8e Makefiles \u5982\u4f55\u5de5\u4f5c\u7684\u7cfb\u5217\u6587\u7ae0\u3002<\/p>\n

\u5f53 make \u547d\u4ee4\u7b2c\u4e00\u6b21\u6267\u884c\u65f6\uff0c\u5b83\u626b\u63cf Makefile \u627e\u5230\u76ee\u6807\u4ee5\u53ca\u5176\u4f9d\u8d56\u3002\u5982\u679c\u8fd9\u4e9b\u4f9d\u8d56\u81ea\u8eab\u4e5f\u662f\u76ee\u6807\uff0c\u7ee7\u7eed\u4e3a\u8fd9\u4e9b\u4f9d\u8d56\u626b\u63cf Makefile \u5efa\u7acb\u5176\u4f9d\u8d56\u5173\u7cfb\uff0c\u7136\u540e\u7f16\u8bd1\u5b83\u4eec\u3002\u4e00\u65e6\u4e3b\u4f9d\u8d56\u7f16\u8bd1\u4e4b\u540e\uff0c\u7136\u540e\u5c31\u7f16\u8bd1\u4e3b\u76ee\u6807\uff08\u8fd9\u662f\u901a\u8fc7 make \u547d\u4ee4\u4f20\u5165\u7684\uff09\u3002<\/p>\n

\u73b0\u5728\uff0c\u5047\u8bbe\u4f60\u5bf9\u67d0\u4e2a\u6e90\u6587\u4ef6\u8fdb\u884c\u4e86\u4fee\u6539\uff0c\u4f60\u518d\u6b21\u6267\u884c make \u547d\u4ee4\uff0c\u5b83\u5c06\u53ea\u7f16\u8bd1\u4e0e\u8be5\u6e90\u6587\u4ef6\u76f8\u5173\u7684\u76ee\u6807\u6587\u4ef6\uff0c\u56e0\u6b64\uff0c\u7f16\u8bd1\u5b8c\u6700\u7ec8\u7684\u53ef\u6267\u884c\u6587\u4ef6\u8282\u7701\u4e86\u5927\u91cf\u7684\u65f6\u95f4\u3002<\/p>\n

Make \u5982\u4f55\u5de5\u4f5c\u7684<\/strong><\/div>\n

\u4e0b\u9762\u662f\u672c\u6587\u6240\u4f7f\u7528\u7684\u6d4b\u8bd5\u73af\u5883\uff1a<\/p>\n

OS \u2014\u2014 Ubunut 13.04\r\nShell \u2014\u2014 Bash 4.2.45\r\nApplication \u2014\u2014 GNU Make 3.81\r\n<\/pre>\n

\u4e0b\u9762\u662f\u5de5\u7a0b\u7684\u5185\u5bb9\uff1a<\/p>\n

\n
$ <\/span>ls \r\nanotherTest.c Makefile test.c test.h\r\n<\/pre>\n<\/div>\n

\u4e0b\u9762\u662f Makefile \u7684\u5185\u5bb9\uff1a<\/p>\n

\n
all<\/span>:<\/span> test <\/span>\r\n\r\ntest<\/span>:<\/span> test.o anotherTest.o <\/span>\r\n    gcc -Wall test.o anotherTest.o -o test<\/span>\r\n\r\ntest.o<\/span>:<\/span> test.c <\/span>\r\n    gcc -c -Wall test.c \r\n\r\nanotherTest.o<\/span>:<\/span> anotherTest.c <\/span>\r\n    gcc -c -Wall anotherTest.c \r\n\r\nclean<\/span>:<\/span> \r\n    rm -rf *.o test<\/span>\r\n<\/pre>\n<\/div>\n

\u73b0\u5728\u6211\u4eec\u6765\u770b Linux \u4e0b\u4e00\u4e9b make \u547d\u4ee4\u5e94\u7528\u7684\u5b9e\u4f8b\uff1a<\/p>\n

1. \u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50<\/strong><\/div>\n

\u4e3a\u4e86\u7f16\u8bd1\u6574\u4e2a\u5de5\u7a0b\uff0c\u4f60\u53ef\u4ee5\u7b80\u5355\u7684\u4f7f\u7528make\u6216\u8005\u5728make\u547d\u4ee4\u540e\u5e26\u4e0a\u76ee\u6807all\u3002<\/p>\n

\n
$ <\/span>make \r\ngcc -c -Wall test.c \r\ngcc -c -Wall anotherTest.c \r\ngcc -Wall test.o anotherTest.o -o test<\/span>\r\n<\/pre>\n<\/div>\n

\u4f60\u80fd\u770b\u5230 make \u547d\u4ee4\u7b2c\u4e00\u6b21\u521b\u5efa\u7684\u4f9d\u8d56\u4ee5\u53ca\u5b9e\u9645\u7684\u76ee\u6807\u3002<\/p>\n

\u5982\u679c\u4f60\u518d\u6b21\u67e5\u770b\u76ee\u5f55\u5185\u5bb9\uff0c\u91cc\u9762\u591a\u4e86\u4e00\u4e9b .o \u6587\u4ef6\u548c\u6267\u884c\u6587\u4ef6\uff1a<\/p>\n

\n
$ <\/span>ls \r\nanotherTest.c anotherTest.o Makefile test <\/span>test.c test.h test.o\r\n<\/pre>\n<\/div>\n

\u73b0\u5728\uff0c\u5047\u8bbe\u4f60\u5bf9 test.c \u6587\u4ef6\u505a\u4e86\u4e00\u4e9b\u4fee\u6539\uff0c\u91cd\u65b0\u4f7f\u7528 make \u7f16\u8bd1\u5de5\u7a0b\uff1a<\/p>\n

\n
$ <\/span>make \r\ngcc -c -Wall test.c \r\ngcc -Wall test.o anotherTest.o -o test<\/span>\r\n<\/pre>\n<\/div>\n

\u4f60\u53ef\u4ee5\u770b\u5230\u53ea\u6709 test.o \u91cd\u65b0\u7f16\u8bd1\u4e86\uff0c\u7136\u800c\u53e6\u4e00\u4e2a Test.o \u6ca1\u6709\u91cd\u65b0\u7f16\u8bd1\u3002<\/p>\n

\u73b0\u5728\u6e05\u7406\u6240\u6709\u7684\u76ee\u6807\u6587\u4ef6\u548c\u53ef\u6267\u884c\u6587\u4ef6 test\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u76ee\u6807clean:<\/p>\n

\n
$ <\/span>make clean\r\nrm -rf *.o test<\/span>\r\n\r\n$ <\/span>ls\r\nanotherTest.c Makefile test.c test.h\r\n<\/pre>\n<\/div>\n

\u4f60\u53ef\u4ee5\u770b\u5230\u6240\u6709\u7684 .o \u6587\u4ef6\u548c\u6267\u884c\u6587\u4ef6 test \u90fd\u88ab\u5220\u9664\u4e86\u3002<\/p>\n

2. \u901a\u8fc7 -B \u9009\u9879\u8ba9\u6240\u6709\u76ee\u6807\u603b\u662f\u91cd\u65b0\u5efa\u7acb<\/strong><\/div>\n

\u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u4f60\u53ef\u80fd\u6ce8\u610f\u5230 make \u547d\u4ee4\u4e0d\u4f1a\u7f16\u8bd1\u90a3\u4e9b\u81ea\u4ece\u4e0a\u6b21\u7f16\u8bd1\u4e4b\u540e\u5c31\u6ca1\u6709\u66f4\u6539\u7684\u6587\u4ef6\uff0c\u4f46\u662f\uff0c\u5982\u679c\u4f60\u60f3\u8986\u76d6 make \u8fd9\u79cd\u9ed8\u8ba4\u7684\u884c\u4e3a\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 -B \u9009\u9879\u3002<\/p>\n

\u4e0b\u9762\u662f\u4e2a\u4f8b\u5b50\uff1a<\/p>\n

\n
$ <\/span>make\r\nmake: Nothing to be done for<\/span> `<\/span>all\u2019.\r\n\r\n$ <\/span>make -B\r\ngcc -c -Wall test.c\r\ngcc -c -Wall anotherTest.c\r\ngcc -Wall test.o anotherTest.o -o test<\/span>\r\n<\/pre>\n<\/div>\n

\u4f60\u53ef\u4ee5\u770b\u5230\u5c3d\u7ba1 make \u547d\u4ee4\u4e0d\u4f1a\u7f16\u8bd1\u4efb\u4f55\u6587\u4ef6\uff0c\u7136\u800cmake -B\u4f1a\u5f3a\u5236\u7f16\u8bd1\u6240\u6709\u7684\u76ee\u6807\u6587\u4ef6\u4ee5\u53ca\u6700\u7ec8\u7684\u6267\u884c\u6587\u4ef6\u3002<\/p>\n

3. \u4f7f\u7528 -d \u9009\u9879\u6253\u5370\u8c03\u8bd5\u4fe1\u606f<\/strong><\/div>\n

\u5982\u679c\u4f60\u60f3\u77e5\u9053 make \u6267\u884c\u65f6\u5b9e\u9645\u505a\u4e86\u4ec0\u4e48\uff0c\u4f7f\u7528 -d \u9009\u9879\u3002<\/p>\n

\u8fd9\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n

\n
$ <\/span>make -d |<\/span> more\r\nGNU Make 3.81\r\nCopyright (<\/span>C)<\/span> 2006 Free Software Foundation, Inc.\r\nThis is free software;<\/span> see the source <\/span>for <\/span>copying conditions.\r\nThere is NO warranty;<\/span> not even for <\/span>MERCHANTABILITY or FITNESS FOR A\r\nPARTICULAR PURPOSE.\r\n\r\nThis program built for <\/span>x86_64-pc-linux-gnu\r\nReading makefiles\u2026\r\nReading makefile `<\/span>Makefile\u2019\u2026\r\nUpdating makefiles\u2026.\r\nConsidering target file `<\/span>Makefile\u2019.\r\nLooking for <\/span>an implicit rule for<\/span> `<\/span>Makefile\u2019.\r\nTrying pattern rule with stem `<\/span>Makefile\u2019.\r\nTrying implicit prerequisite `<\/span>Makefile.o\u2019.\r\nTrying pattern rule with stem `<\/span>Makefile\u2019.\r\nTrying implicit prerequisite `<\/span>Makefile.c\u2019.\r\nTrying pattern rule with stem `<\/span>Makefile\u2019.\r\nTrying implicit prerequisite `<\/span>Makefile.cc\u2019.\r\nTrying pattern rule with stem `<\/span>Makefile\u2019.\r\nTrying implicit prerequisite `<\/span>Makefile.C\u2019.\r\nTrying pattern rule with stem `<\/span>Makefile\u2019.\r\nTrying implicit prerequisite `<\/span>Makefile.cpp\u2019.\r\nTrying pattern rule with stem `<\/span>Makefile\u2019.\r\n--More--\r\n\r\n<\/pre>\n<\/div>\n

\u8fd9\u662f\u5f88\u957f\u7684\u8f93\u51fa\uff0c\u4f60\u4e5f\u770b\u5230\u6211\u4f7f\u7528\u4e86more\u547d\u4ee4\u6765\u4e00\u9875\u4e00\u9875\u663e\u793a\u8f93\u51fa\u3002<\/p>\n

4. \u4f7f\u7528 -C \u9009\u9879\u6539\u53d8\u76ee\u5f55<\/strong><\/div>\n

\u4f60\u53ef\u4ee5\u4e3a make \u547d\u4ee4\u63d0\u4f9b\u4e0d\u540c\u7684\u76ee\u5f55\u8def\u5f84\uff0c\u5728\u5bfb\u627e Makefile \u4e4b\u524d\u4f1a\u5207\u6362\u76ee\u5f55\u7684\u3002<\/p>\n

\u8fd9\u662f\u4e00\u4e2a\u76ee\u5f55\uff0c\u5047\u8bbe\u4f60\u5c31\u5728\u5f53\u524d\u76ee\u5f55\u4e0b:<\/p>\n

$ ls \r\nfile file2 frnd frnd1.cpp log1.txt log3.txt log5.txt\r\nfile1 file name with spaces frnd1 frnd.cpp log2.txt log4.txt\r\n<\/pre>\n

\u4f46\u662f\u4f60\u60f3\u8fd0\u884c\u7684 make \u547d\u4ee4\u7684 Makefile \u6587\u4ef6\u4fdd\u5b58\u5728 ..\/make-dir\/ \u76ee\u5f55\u4e0b\uff0c\u4f60\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<\/p>\n

\n
$ <\/span>make -C ..\/make-dir\/ \r\nmake: Entering directory `<\/span>\/home\/himanshu\/practice\/make-dir\u2019 \r\nmake: Nothing to be done for<\/span> `<\/span>all\u2019. \r\nmake: Leaving directory `<\/span>\/home\/himanshu\/practice\/make-dir\r\n<\/pre>\n<\/div>\n

\u4f60\u80fd\u770b\u5230 make \u547d\u4ee4\u9996\u5148\u5207\u5230\u7279\u5b9a\u7684\u76ee\u5f55\u4e0b\uff0c\u5728\u90a3\u6267\u884c\uff0c\u7136\u540e\u518d\u5207\u6362\u56de\u6765\u3002<\/p>\n

5. \u901a\u8fc7 -f \u9009\u9879\u5c06\u5176\u5b83\u6587\u4ef6\u770b\u4f5c Makefile<\/strong><\/div>\n

\u5982\u679c\u4f60\u60f3\u5c06\u91cd\u547d\u540d Makefile \u6587\u4ef6\uff0c\u6bd4\u5982\u53d6\u540d\u4e3a my_makefile \u6216\u8005\u5176\u5b83\u7684\u540d\u5b57\uff0c\u6211\u4eec\u60f3\u8ba9 make \u5c06\u5b83\u4e5f\u5f53\u6210 Makefile\uff0c\u53ef\u4ee5\u4f7f\u7528 -f \u9009\u9879\u3002<\/p>\n

\n
make -f my_makefile\r\n<\/pre>\n<\/div>\n

\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\uff0cmake \u547d\u4ee4\u4f1a\u9009\u62e9\u626b\u63cf my_makefile \u6765\u4ee3\u66ff Makefile\u3002<\/p>\n

\n

\u539f\u6587\u6765\u81ea\uff1ahttp:\/\/www.cnblogs.com\/hazir\/p\/linux_make_examples.html<\/a><\/p>\n

\u672c\u6587\u5730\u5740\uff1ahttps:\/\/lrxjmw.cn\/magic-make-work.html<\/a>\u7f16\u8f91\uff1a\u6731\u57f9\u68cb \uff0c\u5ba1\u6838\u5458\uff1a\u9004\u589e\u5b9d<\/span><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"

\u5bfc\u8bfb Linux \u4e0b\u00a0make\u00a0\u547d\u4ee4\u662f\u7cfb\u7edf\u7ba1\u7406\u5458\u548c\u7a0b\u5e8f\u5458\u7528\u7684\u6700\u9891\u7e41\u7684\u547d\u4ee4\u4e4b\u4e00\u3002\u7ba1\u7406\u5458\u7528\u5b83\u901a\u8fc7\u547d\u4ee4\u884c\u6765\u7f16\u8bd1\u548c\u5b89\u88c5 […]<\/p>\n","protected":false},"author":63,"featured_media":115420,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-115419","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/115419","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/63"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=115419"}],"version-history":[{"count":11,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/115419\/revisions"}],"predecessor-version":[{"id":117048,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/115419\/revisions\/117048"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/115420"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=115419"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=115419"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=115419"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}