{"id":885,"date":"2024-02-17T22:12:39","date_gmt":"2024-02-17T14:12:39","guid":{"rendered":"http:\/\/blog.chxzking.cn\/?p=885"},"modified":"2024-02-24T20:56:09","modified_gmt":"2024-02-24T12:56:09","slug":"linux%e4%b8%ad%e7%9a%84make%e5%b7%a5%e5%85%b7","status":"publish","type":"post","link":"http:\/\/blog.chxzking.cn\/index.php\/2024\/02\/17\/linux%e4%b8%ad%e7%9a%84make%e5%b7%a5%e5%85%b7\/","title":{"rendered":"Linux\u4e2d\u7684Make\u5de5\u5177\uff08\u5165\u95e8\uff09"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">\u547d\u4ee4\u89c4\u5219<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">makefile\u7684\u57fa\u672c\u683c\u5f0f\uff1a<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><code>targets : prerequisties<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>[tab\u952e]command<\/code><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>target<\/code> : \u76ee\u6807\u6587\u4ef6\uff0c\u53ef\u4ee5\u662fObjectFile\uff0c\u4e5f\u53ef\u4ee5\u662f\u6267\u884c\u6587\u4ef6\uff0c\u8fd8\u53ef\u4ee5\u662f\u6807\u7b7eLabel\u3002<\/li>\n\n\n\n<li><code>prerequisite<\/code> : \u8981\u751f\u6210\u7684\u54ea\u4e2atarget\u6240\u9700\u8981\u7684\u6587\u4ef6\u6216\u662f\u76ee\u6807<\/li>\n\n\n\n<li><code>command<\/code>\uff1a\u662fmake\u9700\u8981\u6267\u884c\u7684\u547d\u4ee4<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f2a\u76ee\u6807<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728makefile\u4e2d\uff0c\u4f2a\u76ee\u6807\u662f\u4e00\u79cd\u7279\u6b8a\u7c7b\u578b\u7684\u76ee\u6807\uff0c\u5b83\u5e76\u4e0d\u8868\u793a\u751f\u6210\u4efb\u4f55\u6587\u4ef6\uff0c\u800c\u662f\u8868\u793a\u4e00\u4e2a\u52a8\u4f5c\u6216\u8005\u64cd\u4f5c\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u4f2a\u76ee\u6807\u7684\u610f\u4e49<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f2a\u76ee\u6807\u6709\u4ee5\u4e0b\u51e0\u4e2a\u4e3b\u8981\u4f5c\u7528\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>\u547d\u540d\u7ea6\u5b9a<\/code>\uff1a\u901a\u8fc7\u5b9a\u4e49\u4f2a\u76ee\u6807\uff0c\u53ef\u4ee5\u5bf9\u5e38\u7528\u7684\u64cd\u4f5c\u6216\u4efb\u52a1\u8fdb\u884c\u547d\u540d\u7ea6\u5b9a\uff0c\u4f7f\u5f97\u5176\u4ed6\u5f00\u53d1\u8005\u6216\u4f7f\u7528\u8005\u80fd\u591f\u6e05\u6670\u5730\u7406\u89e3\u8be5\u76ee\u6807\u7684\u7528\u9014\u548c\u4f5c\u7528<\/li>\n\n\n\n<li><code>\u907f\u514d\u4e0e\u540c\u540d\u6587\u4ef6\u51b2\u7a81<\/code>\uff1a\u6709\u65f6\u5019\uff0c\u53ef\u80fd\u4f1a\u6709\u4e0e\u76ee\u6807\u540c\u540d\u7684\u6587\u4ef6\u5b58\u5728\u3002\u901a\u8fc7\u5b9a\u4e49\u4f2a\u76ee\u6807\uff0c\u53ef\u4ee5\u907f\u514d\u4e0e\u540c\u540d\u6587\u4ef6\u4ea7\u751f\u51b2\u7a81<\/li>\n\n\n\n<li><code>\u7ec4\u7ec7\u548c\u7ba1\u7406\u6784\u5efa\u4efb\u52a1<\/code>\uff1a\u4f2a\u76ee\u6807\u53ef\u4ee5\u7528\u6765\u7ec4\u7ec7\u548c\u7ba1\u7406\u6784\u5efa\u6d41\u7a0b\u4e2d\u7684\u5404\u4e2a\u4efb\u52a1\u6216\u64cd\u4f5c\u3002\u901a\u8fc7\u5b9a\u4e49\u4f2a\u76ee\u6807\uff0c\u53ef\u4ee5\u5c06\u76f8\u5173\u7684\u4efb\u52a1\u7ec4\u7ec7\u5728\u4e00\u8d77\uff0c\u5e76\u901a\u8fc7\u4f9d\u8d56\u5173\u7cfb\u548c\u547d\u4ee4\u5b9a\u4e49\u6765\u63a7\u5236\u5b83\u4eec\u7684\u6267\u884c\u987a\u5e8f<\/li>\n\n\n\n<li><code>\u63d0\u9ad8\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027<\/code>\uff1a\u901a\u8fc7\u4f7f\u7528\u4f2a\u76ee\u6807\uff0c\u53ef\u4ee5\u4f7fMakefile\u66f4\u5177\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u901a\u8fc7\u7ed9\u4efb\u52a1\u547d\u540d\uff0c\u5e76\u5c06\u4efb\u52a1\u7684\u903b\u8f91\u548c\u547d\u4ee4\u5b9a\u4e49\u5728\u4e00\u5904\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u6e05\u6670\u5ea6\u548c\u53ef\u7ef4\u62a4\u6027<\/li>\n\n\n\n<li><code>\u6267\u884c\u5e38\u7528\u64cd\u4f5c<\/code>\uff1a\u4f2a\u76ee\u6807\u901a\u5e38\u7528\u4e8e\u6267\u884c\u4e00\u4e9b\u5e38\u7528\u64cd\u4f5c\uff0c\u4f8b\u5982\u6e05\u7406\u4e34\u65f6\u6587\u4ef6\u3001\u6d4b\u8bd5\u9879\u76ee\u3001\u751f\u6210\u6587\u6863\u3001\u6253\u5305\u53d1\u5e03\u7b49\u3002\u901a\u8fc7\u5b9a\u4e49\u4f2a\u76ee\u6807\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u6267\u884c\u8fd9\u4e9b\u64cd\u4f5c\uff0c\u800c\u65e0\u9700\u624b\u52a8\u8f93\u5165\u590d\u6742\u7684\u547d\u4ee4<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f2a\u76ee\u6807\u5e76\u4e0d\u5bf9\u5e94\u5b9e\u9645\u7684\u6587\u4ef6\uff0c\u800c\u662f\u4ee3\u8868\u4e00\u79cd\u64cd\u4f5c\u6216\u4efb\u52a1\u3002\u4f8b\u5982\uff0c<code>clean<\/code>\u8fd9\u4e2a\u4f2a\u76ee\u6807\u5c31\u662f\u7528\u6765\u6e05\u9664\u751f\u6210\u7684\u76ee\u6807\u6587\u4ef6\u7684\u3002\u5f53\u4f60\u6267\u884c<code>make clean<\/code>\u65f6\uff0cMake\u5c31\u4f1a\u6267\u884c<code>clean<\/code>\u4f2a\u76ee\u6807\u4e0b\u7684\u547d\u4ee4\uff0c\u6e05\u9664\u6240\u6709\u7684\u76ee\u6807\u6587\u4ef6\u3002\u8fd9\u6837\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49\u4f2a\u76ee\u6807\u6765\u7ec4\u7ec7\u548c\u7ba1\u7406\u6784\u5efa\u8fc7\u7a0b\u4e2d\u7684\u5404\u79cd\u4efb\u52a1\u548c\u64cd\u4f5c\u3002\u8fd9\u4e5f\u662fMakefile\u7684\u4e00\u4e2a\u91cd\u8981\u7279\u6027\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u7075\u6d3b\u7684\u65b9\u5f0f\u6765\u63a7\u5236\u548c\u7ba1\u7406\u6784\u5efa\u8fc7\u7a0b\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f2a\u76ee\u6807\u662f\u81ea\u5df1\u5b9a\u4e49\u7684\uff0c\u4f8b\u5982\u53ef\u4ee5\u5b9a\u4e49<code>my_clean<\/code> ,\u751a\u81f3\u662f<code>aaa<\/code>\uff0c\u8981\u6ce8\u610f\u70b9\u4e00\u70b9\u662f\uff0cmake\u5de5\u5177\u5e76\u4e0d\u63d0\u4f9b\u5185\u7f6e\u4f2a\u76ee\u6807\uff0c\u6240\u6709\u7684\u4f2a\u76ee\u6807\u90fd\u662f\u81ea\u5b9a\u4e49\u7684\uff0c\u5305\u62ec<code>debug<\/code>\u3001<code>clean<\/code>\u7b49\u5e38\u7528\u7684\u4f2a\u76ee\u6807\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">.PHONY<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728makefile\u4e2d\uff0c<code>.PHONY<\/code>\u662f\u4e00\u4e2a\u7279\u6b8a\u7684\u76ee\u6807\uff0c\u7528\u4e8e\u58f0\u660e\u67d0\u4e9b\u76ee\u6807\u4e3a\u4f2a\u76ee\u6807\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>.PHONY<\/code>\u4e3b\u8981\u6709\u4e24\u79cd\u4f5c\u7528\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u907f\u514d\u540c\u540d\u6587\u4ef6\u51b2\u7a81<\/mark>\uff1a\u5982\u679cMakefile\u7684\u540c\u7ea7\u76ee\u5f55\u4e0b\u5b58\u5728\u4e00\u4e2a\u4e0e\u4f2a\u76ee\u6807\u540c\u540d\u7684\u6587\u4ef6\uff0c\u90a3\u4e48Make\u4f1a\u4f18\u5148\u8003\u8651\u8fd9\u4e2a\u6587\u4ef6\uff0c\u800c\u4e0d\u662f\u4f2a\u76ee\u6807\u3002\u4f46\u662f\uff0c\u5982\u679c\u6211\u4eec\u5728Makefile\u4e2d\u4f7f\u7528\u4e86.PHONY\uff0c\u90a3\u4e48Make\u5c31\u4f1a\u5ffd\u7565\u8fd9\u4e2a\u540c\u540d\u6587\u4ef6\uff0c\u800c\u76f4\u63a5\u6267\u884c\u4f2a\u76ee\u6807\u7684\u547d\u4ee4\u3002<\/li>\n\n\n\n<li><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u786e\u4fdd\u4f2a\u76ee\u6807\u603b\u662f\u88ab\u6267\u884c<\/mark>\uff1a\u5728Make\u7684\u903b\u8f91\u4e2d\uff0c\u5982\u679c\u76ee\u6807\u6587\u4ef6\uff08\u6216\u8005\u8bf4\u662f\u8f93\u51fa\u6587\u4ef6\uff09\u5df2\u7ecf\u5b58\u5728\uff0c\u5e76\u4e14\u5176\u4f9d\u8d56\u6587\u4ef6\u6ca1\u6709\u53d1\u751f\u53d8\u5316\uff0c\u90a3\u4e48Make\u5c31\u4e0d\u4f1a\u518d\u6267\u884c\u8be5\u76ee\u6807\u7684\u547d\u4ee4\u3002\u4f46\u662f\u5bf9\u4e8e\u4f2a\u76ee\u6807\u6765\u8bf4\uff0c\u6211\u4eec\u901a\u5e38\u5e0c\u671b\u65e0\u8bba\u4f55\u65f6\uff0c\u53ea\u8981\u6211\u4eec\u6267\u884c\u4e86make &lt;\u4f2a\u76ee\u6807>\uff0c\u5c31\u5e94\u8be5\u6267\u884c\u4f2a\u76ee\u6807\u7684\u547d\u4ee4\u3002.PHONY\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e3e\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.PHONY: clean\n\nclean:\n\trm *.o<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2a\u4ee3\u7801\u7684\u4f5c\u7528\u662f\u6267\u884c\u4f2a\u76ee\u6807clean\uff0c\u5c06\u6e05\u9664\u6240\u6709.o\u7ed3\u5c3e\u7684\u6587\u4ef6\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8f93\u51fa\u7ed3\u679c\u4e2d\uff0c\u80fd\u770b\u5230\u547d\u4ee4<code>rm *.o <\/code>\uff0c\u5982\u679c\u4e0d\u5e0c\u671b\u663e\u793a\u51fa\u6765\uff0c\u53ef\u4ee5\u5728\u547d\u4ee4\u524d\u9762\u52a0\u4e0a<code>@<\/code>\u7b26\u53f7\uff0c\u5373<code>@rm *.o<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Make\u53d8\u91cf<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u57fa\u672c\u7684\u8d4b\u503c\u7b26\u53f7\u548c\u7eed\u884c\u7b26<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">\u8d4b\u503c\u7b26<\/h5>\n\n\n\n<p class=\"wp-block-paragraph\"><code>=<\/code> \u6700\u57fa\u672c\u7684\u8d4b\u503c\u7b26\u53f7\uff0c\u5176\u4f5c\u7528\u662f\u7cfb\u7edf\u81ea\u52a8\u63a8\u5bfc\u5c06<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">\u6700\u7ec8<\/mark>\u8d4b\u503c\u4f5c\u4e3a\u53d8\u91cf\u7684\u503c\u3002\u4e5f\u53eb\u505a<mark style=\"background-color:#8ed1fc\" class=\"has-inline-color\">\u5ef6\u8fdf\u8d4b\u503c<\/mark> \uff0c\u5373\u53ea\u6709\u5728\u7b49\u53f7\u8c03\u7528\u7684\u65f6\u5019\u624d\u5f00\u59cb\u8fd0\u7b97\uff0c\u6709\u70b9\u7c7b\u4f3cc\u8bed\u8a00\u4e2d\u5f62\u53c2\u7684\u6982\u5ff5\uff0c\u53ea\u6709\u5f53\u8c03\u7528\u51fd\u6570\u7684\u65f6\u5019\u624d\u5206\u914d\u7a7a\u95f4\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u4e3e\u4f8b\uff1a\nB = $(A)b #$(A) \u4ee3\u8868\u53d6A\u4e2d\u7684\u503c\nA = c\n\ndebug : \n\techo $(B)#\u8c03\u7528B<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u8c03\u7528<code>B<\/code>\uff0c\u90a3\u4e48<code>=<\/code>\u624d\u5f00\u59cb\u8fd0\u7b97\uff0c\u5c06<code>$(A)<\/code>\u6362\u6210<code>c <\/code>\u6700\u540e\u8f93\u51fa<code>cb<\/code> \uff0c\u6b63\u662f\u56e0\u4e3a\u5728\u8c03\u7528\u53d8\u91cf\u65f6\u624d\u5f00\u59cb\u8fd0\u7b97\uff0c\u6240\u4ee5\u624d\u4e0d\u4f1a\u5728<code>B = $(A)b<\/code>\u8fd9\u6bb5\u4ee3\u7801\u4e2d\u51fa\u73b0\u672a\u5b9a\u4e49\u9519\u8bef\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>:= <\/code>\u662f<mark style=\"background-color:#8ed1fc\" class=\"has-inline-color\">\u8986\u76d6\u5f0f\u8d4b\u503c<\/mark> \u4e5f\u53ef\u4ee5\u53eb\u505a<mark style=\"background-color:#8ed1fc\" class=\"has-inline-color\">\u7acb\u5373\u8d4b\u503c<\/mark>\uff0c\u5982\u679c\u4e00\u4e2a\u53d8\u91cf\u5728\u4e4b\u524d\u5df2\u7ecf\u8d4b\u8fc7\u503c\u4e86\uff0c\u90a3\u4e48\u8fd9\u6b21\u7684\u8d4b\u503c\u5c06\u4f1a\u8986\u76d6\u6389\u65e7\u503c\uff0c\u4f46\u662f\u8fd9\u4e2a\u7b26\u53f7\u53ea\u80fd\u63a8\u5bfc<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">\u8be5\u7b26\u53f7\u4e4b\u524d<\/mark>\u7684\u503c\u3002\u4f7f\u7528\u8fd9\u79cd\u7b26\u53f7\uff0c\u4f1a\u8ba9\u7b49\u53f7\u53f3\u8fb9\u7684\u503c\u7acb\u5373\u8d4b\u503c\u7ed9\u5de6\u8fb9\u7684\u53d8\u91cf\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>A := a\nB := $(A)b #$(A) \u4ee3\u8868\u53d6A\u4e2d\u7684\u503c\nA := c\n\ndebug : # \u8c03\u7528B\n\techo $(B) \n\t<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>echo ab  \nab<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u4eec\u53ef\u4ee5\u89c2\u5bdf\u5230\u6b64\u65f6\u5df2\u7ecf\u5c06a\u8d4b\u503c\u7ed9\u4e86A\uff0c\u4f46\u662f\u5982\u679c\u662f\u5ef6\u8fdf\u8d4b\u503c\u7684\u7ed3\u679c\u5219\u6070\u6070\u76f8\u53cd\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e3e\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>A = a\nB = $(A)b #$(A) \u4ee3\u8868\u53d6A\u4e2d\u7684\u503c\nA = c\n\ndebug : # \u8c03\u7528B\n\techo $(B) \n\t<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>echo cb  \ncb<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u4eec\u53ef\u4ee5\u89c2\u5bdf\u5230\u8fd9\u4e2a\u65f6\u5019\u7684A\u662f\u53d6\u7684\u6700\u7ec8\u503c\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>+=<\/code> <mark style=\"background-color:#8ed1fc\" class=\"has-inline-color\">\u8ffd\u52a0\u8d4b\u503c<\/mark>\u7b26\u53f7\uff0c\u4f1a\u5c06\u65b0\u503c\u76f4\u63a5\u7c98\u8d34\u5728\u65e7\u503c\u7684\u672b\u5c3e\uff0c\u5728\u53d8\u91cf\u9996\u6b21\u8d4b\u503c\u7684\u65f6\u5019\uff0c+=\u4f1a\u88ab\u770b\u4f5c=\u7b26\u53f7\uff0c\u800c\u4e14\u8ffd\u52a0\u7684\u65f6\u5019\uff0c\u4f1a\u5728\u65e7\u503c\u548c\u65b0\u503c\u4e4b\u95f4\u589e\u52a0\u4e00\u4e2a\u7a7a\u683c\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e3e\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>A += a #\u5f53\u53d8\u91cf\u9996\u6b21\u8d4b\u503c\u7684\u65f6\u5019\uff0c+=\u76f8\u5f53\u4e8e=\nA += c \n\ndebug : # \u8c03\u7528A\n\techo $(A) <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>echo a  c  \na c<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>?=<\/code> <mark style=\"background-color:#8ed1fc\" class=\"has-inline-color\">\u6761\u4ef6\u8d4b\u503c<\/mark>\uff0c\u8fd9\u4e2a\u7b26\u53f7\u4f1a\u9996\u5148\u786e\u5b9a\u8fd9\u4e2a\u53d8\u91cf\u662f\u5426\u88ab\u8d4b\u503c\u8fc7\uff0c\u5982\u679c\u672a\u8d4b\u503c\uff0c\u5219\u5c06\u53f3\u503c\u8d4b\u503c\uff0c\u5982\u679c\u5df2\u7ecf\u8d4b\u503c\u4e86\uff0c\u5219\u4fdd\u6301\u65e7\u503c<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e3e\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>A ?= a\nA ?= c \n\ndebug : # \u8c03\u7528A\n\techo $(A) <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>echo a \na<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ece\u7ed3\u679c\u53ef\u4ee5\u770b\u5230\uff0cA\u53ea\u4f1a\u5728\u9996\u6b21\u88ab\u8d4b\u503c\uff0c\u7b2c\u4e8c\u6b21A\u7684\u503c\u5df2\u7ecf\u4e3aa\uff0c\u4e8e\u662fc\u6ca1\u6709\u8d4b\u503c\u6210\u529f\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">\u7eed\u884c\u7b26<\/h5>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u5f00\u53d1\u4e2d\uff0c\u6709\u53ef\u80fd\u4f1a\u51fa\u73b0\u53c2\u6570\u592a\u957f\uff0c\u4e3a\u4e86\u8ba9\u677f\u4e66\u66f4\u52a0\u7f8e\u89c2\u548c\u4fbf\u4e8e\u7406\u89e3\uff0c\u4e8e\u662f\u53ef\u4ee5\u4f7f\u7528\u7eed\u884c\u7b26\u53f7\u8fdb\u884c\u6362\u884c\uff0c\u4e0d\u8fc7\u4f1a\u5728\u6362\u884c\u5904\u81ea\u52a8\u6dfb\u52a0\u4e00\u4e2a\u7a7a\u683c\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e3e\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>A := aaaaaaaaaaaaaaaaaa\\\n     bbbbbbbbbbbbbbbbbb\\\n\t cccccccccccccccccc\\\n\ndebug :\n\t@echo $(A)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb cccccccccccccccccc<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u53d8\u91cf\u7684\u8c03\u7528<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5bf9\u4e8e\u4e00\u4e2a\u53d8\u91cf\uff0c\u5982\u679c\u60f3\u8c03\u7528\u5176\u4ee3\u8868\u7684\u503c\uff0c\u9700\u8981\u4f7f\u7528<code>$()<\/code>\u6216\u8005<code>${}<\/code>\uff0c\u8fd9\u4e24\u79cd\u8868\u8fbe\u65b9\u5f0f\u662f\u7b49\u4ef7\u7684\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0d\u8fc7makefile\u652f\u6301\u4e00\u4e9b\u7279\u6b8a\u7684\u81ea\u52a8\u5316\u53d8\u91cf\uff0c\u6216\u8005\u8bf4\u662f\u9884\u5b9a\u4e49\u53d8\u91cf\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#9b51e0\" class=\"has-inline-color\">\u5e38\u7528\u7684\u81ea\u52a8\u5316\u53d8\u91cf\uff1a<\/mark><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>$@\uff1a\u8868\u793a\u89c4\u5219\u7684\u76ee\u6807\u6587\u4ef6\u540d\u3002\u5728\u591a\u76ee\u6807\u6a21\u5f0f\u89c4\u5219\u4e2d\uff0c\u5b83\u4ee3\u8868\u7684\u662f\u89e6\u53d1\u89c4\u5219\u88ab\u6267\u884c\u7684\u6587\u4ef6\u540d\u3002<\/li>\n\n\n\n<li>$%\uff1a\u5f53\u76ee\u6807\u6587\u4ef6\u662f\u4e00\u4e2a\u9759\u6001\u5e93\u6587\u4ef6\u65f6\uff0c\u4ee3\u8868\u9759\u6001\u5e93\u7684\u4e00\u4e2a\u6210\u5458\u540d\u3002<\/li>\n\n\n\n<li>$&lt;\uff1a\u89c4\u5219\u7684\u7b2c\u4e00\u4e2a\u4f9d\u8d56\u7684\u6587\u4ef6\u540d\u3002\u5982\u679c\u662f\u4e00\u4e2a\u76ee\u6807\u6587\u4ef6\u4f7f\u7528\u9690\u542b\u7684\u89c4\u5219\u6765\u91cd\u5efa\uff0c\u5219\u5b83\u4ee3\u8868\u7531\u9690\u542b\u89c4\u5219\u52a0\u5165\u7684\u7b2c\u4e00\u4e2a\u4f9d\u8d56\u6587\u4ef6\u3002<\/li>\n\n\n\n<li>$?\uff1a\u6240\u6709\u6bd4\u76ee\u6807\u6587\u4ef6\u66f4\u65b0\u7684\u4f9d\u8d56\u6587\u4ef6\u5217\u8868\uff0c\u7a7a\u683c\u5206\u9694\u3002\u5982\u679c\u76ee\u6807\u6587\u4ef6\u65f6\u9759\u6001\u5e93\u6587\u4ef6\uff0c\u4ee3\u8868\u7684\u662f\u5e93\u6587\u4ef6\uff08.o \u6587\u4ef6\uff09\u3002<\/li>\n\n\n\n<li>$^\uff1a\u4ee3\u8868\u7684\u662f\u6240\u6709\u4f9d\u8d56\u6587\u4ef6\u5217\u8868\uff0c\u4f7f\u7528\u7a7a\u683c\u5206\u9694\u3002\u5982\u679c\u76ee\u6807\u662f\u9759\u6001\u5e93\u6587\u4ef6\uff0c\u5b83\u6240\u4ee3\u8868\u7684\u53ea\u80fd\u662f\u6240\u6709\u7684\u5e93\u6210\u5458\uff08.o \u6587\u4ef6\uff09\u540d\u3002\u4e00\u4e2a\u6587\u4ef6\u53ef\u91cd\u590d\u7684\u51fa\u73b0\u5728\u76ee\u6807\u7684\u4f9d\u8d56\u4e2d\uff0c\u53d8\u91cf\u201c$^\u201d\u53ea\u8bb0\u5f55\u5b83\u7684\u7b2c\u4e00\u6b21\u5f15\u7528\u7684\u60c5\u51b5\u3002<\/li>\n\n\n\n<li>$+\uff1a\u7c7b\u4f3c\u201c$^\u201d\uff0c\u4f46\u662f\u5b83\u4fdd\u7559\u4e86\u4f9d\u8d56\u6587\u4ef6\u4e2d\u91cd\u590d\u51fa\u73b0\u7684\u6587\u4ef6\u3002\u4e3b\u8981\u7528\u5728\u7a0b\u5e8f\u94fe\u63a5\u65f6\u5e93\u7684\u4ea4\u53c9\u5f15\u7528\u573a\u5408\u3002<\/li>\n\n\n\n<li>$*\uff1a\u5728\u6a21\u5f0f\u89c4\u5219\u548c\u9759\u6001\u6a21\u5f0f\u89c4\u5219\u4e2d\uff0c\u4ee3\u8868\u201c\u830e\u201d\u3002 \u201c\u830e\u201d\u662f\u76ee\u6807\u6a21\u5f0f\u4e2d\u201c%\u201d\u6240\u4ee3\u8868\u7684\u90e8\u5206\uff08\u5f53\u6587\u4ef6\u540d\u4e2d\u5b58\u5728\u76ee\u5f55\u65f6\uff0c \u201c\u830e\u201d\u4e5f\u5305\u542b\u76ee\u5f55\u90e8\u5206\uff09<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Make\u51fd\u6570<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u51fd\u6570\u7684\u4f7f\u7528\u683c\u5f0f<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u51fd\u6570\u7684\u683c\u5f0f\u6709\u4e24\u79cd\u2014\u2014<code>$(function arguments)<\/code>\u6216\u8005<code>${function arguments}<\/code>\uff0c\u5f53\u5b58\u5728\u591a\u4e2a\u53c2\u6570\u7684\u65f6\u5019\uff0c\u53c2\u6570\u4e4b\u95f4\u7528<code> , <\/code>\u53f7\u8fde\u63a5\uff0c\u4f46\u662f\u8981\u6ce8\u610f\uff0c\u51fd\u6570\u540d\u548c\u53c2\u6570\u4e4b\u95f4\u4f7f\u7528<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">\u7a7a\u683c<\/mark>\u9694\u5f00<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">shell\u51fd\u6570<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">shell\u51fd\u6570\u7684\u4e3b\u8981\u4f5c\u7528\u662f\u53ef\u4ee5<mark style=\"background-color:#8ed1fc\" class=\"has-inline-color\">\u8c03\u7528shell\u547d\u4ee4<\/mark>\uff0c\u53c2\u6570\u5c31\u662f\u547d\u4ee4\u7c7b\u578b\uff0c\u5176\u8fd4\u56de\u503c\u662f\u547d\u4ee4\u7684\u7ed3\u679c\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6807\u51c6\u683c\u5f0f\uff1a<code>$(shell &lt;command&gt; &lt;arguments&gt;) <\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u793a\u4f8b1\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>A := $(shell ls)#\u6253\u5370\u5f53\u524d\u76ee\u5f55\u4e2d\u6240\u6709\u7684\u6587\u4ef6\n\ndebug :\n\t@echo $(A)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>2.c 2.h 3.c 3.h 4.c 4.h main.c Makefile<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u793a\u4f8b2\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>A := $(shell ls | grep \"\\.c\\$$\")#\u6253\u5370\u5f53\u524d\u76ee\u5f55\u4e0b\u4ee5.c\u7ed3\u5c3e\u7684\u6587\u4ef6\n\ndebug :\n\t@echo $(A)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>2.c 3.c 4.c main.c<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7\u8fd9\u4e24\u4e2a\u4f8b\u5b50\uff0c\u53ef\u4ee5\u6e05\u6670\u7684\u770b\u5230\uff0c\u8fd9\u4e2a\u51fd\u6570\u4e2d\u662f\u53ef\u4ee5\u4f7f\u7528\u7ba1\u9053\u7b26\u53f7<code> | <\/code>\u8fde\u63a5\u7136\u540e\u6267\u884c\u7ec4\u5408\u547d\u4ee4\u7684<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">subst\u51fd\u6570<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u6807\u51c6\u683c\u5f0f\u2014\u2014<code>$(subst FROM,TO,TEXT)<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>subst\u51fd\u6570<\/code>\u662f\u4e00\u4e2a<mark style=\"background-color:#8ed1fc\" class=\"has-inline-color\">\u6587\u672c\u66ff\u6362\u51fd\u6570<\/mark>\uff0c\u5176\u4e3b\u8981\u4f5c\u7528\u662f\u5c06\u51fd\u6570\u4e2d\u7684 <code>FROM<\/code> \u66ff\u6362\u4e3a <code>TO<\/code> \u7684\u503c\uff0c\u4e0d\u8fc7\u8fd9\u4e2a\u66ff\u6362\u7684\u6587\u672c\u662f <code>TEXT<\/code> \uff0c\u5c31\u7c7b\u4f3c\u4e8e<code> vscode <\/code>\u4e2d\u7684 <code>ctrl+F <\/code>\u7684\u66ff\u6362\u529f\u80fd\uff0c\u4f1a\u5c06\u6587\u672c\u4e2d\u6240\u6709\u7684 <code>FROM<\/code> \u66ff\u6362\u4e3a <code>TO<\/code> \u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>result := $(shell ls | grep \"\\.c\\$$\") #\u5c06\u5f53\u524d\u76ee\u5f55\u4e2d\u7684\u6240\u6709.c\u6587\u4ef6\u5b58\u653e\u5230result\u4e2d\n\nresult := $(subst .c,.cpp,$(result)) #\u5c06result\u4e2d\u7684.c\u66ff\u6362\u4e3a.cpp\n\ndebug : #\u6253\u5370\n\t@echo $(result)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>2.cpp 3.cpp 4.cpp main.cpp<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7\u8fd0\u884c\u7ed3\u679c\u53ef\u4ee5\u770b\u5230\uff0c\u6240\u6709\u7684\u8f93\u51fa\u7ed3\u679c\u90fd\u662f.cpp\u540e\u7f00<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">patsubst\u51fd\u6570<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u6807\u51c6\u683c\u5f0f \u2014\u2014 <code>$(patsubst &lt;pattern&gt;,&lt;replacement&gt;,&lt;text&gt;)<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2a\u51fd\u6570\u7684\u4f5c\u7528\u540c\u6837\u662f\u66ff\u6362\uff0c\u4e0d\u8fc7\u548csubst\u6709\u6240\u4e0d\u540c\uff0cpatsubst\u662f\u4ee5\u5355\u8bcd\u4e3a\u5355\u4f4d\u8fdb\u884c\u7684<mark style=\"background-color:#8ed1fc\" class=\"has-inline-color has-black-color\">\u5355\u8bcd\u66ff\u6362<\/mark>\uff0c\u81f3\u4e8e\u8fd9\u4e2a\u5355\u8bcd\u7684\u5b9a\u4e49\uff0c\u7cfb\u7edf\u4f1a\u5c06\u7a7a\u683c\u3001\u56de\u8f66\u3001Tab\u7b49\u4f5c\u4e3a\u5355\u8bcd\u7684\u8fb9\u754c\u3002\u540c\u6837\u662f\u5728text\u6587\u672c\u4e2d\uff0c\u5c06\u5355\u8bcd<code>pattern<\/code> \u66ff\u6362\u4e3a <code>replacement<\/code> \u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u793a\u4f8b1\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>result := $(shell ls | grep \"\\.c\\$$\") #\u5c06\u5f53\u524d\u76ee\u5f55\u4e2d\u7684\u6240\u6709.c\u6587\u4ef6\u5b58\u653e\u5230result\u4e2d\n\nresult := $(patsubst .c,.cpp,$(result)) #\u5c06result\u4e2d\u7684.c\u66ff\u6362\u4e3a.cpp\n\ndebug : #\u6253\u5370\n\t@echo $(result)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>2.c 3.c 4.c main.c<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u91cc\u53ef\u4ee5\u5f88\u6e05\u6670\u5730\u770b\u5230\u5355\u8bcd\u6ca1\u6709\u88ab\u66ff\u6362\uff0c\u56e0\u4e3a\u8fd9\u56db\u4e2a\u6587\u4ef6\u540d\u6ca1\u6709\u53eb\u505a<code>.c<\/code>\u7684\u6587\u4ef6\uff0c\u56e0\u4e3a<code>.c<\/code>\u53ea\u662f\u8fd9\u4e2a\u540d\u5b57\u7684\u4e00\u90e8\u5206\uff0c\u5c31\u6bd4\u5982\u4f60\u662f\u5546\u5bb6\uff0c\u4e00\u4ef6\u5546\u54c1\u535699\uff0c\u987e\u5ba2\u53ea\u613f\u610f\u51fa\u5176\u4e2d\u7684\u4e00\u4e2a9\uff0c\u800c\u4e14\u662f\u4e2a\u4f4d\uff0c\u4f60\u662f\u4e0d\u53ef\u80fd\u7b54\u5e94\u4ed6\u65e0\u7406\u7684\u8981\u6c42\u7684\u3002\u90a3\u4e48\u95ee\u9898\u6765\u4e86\uff0c\u8be5\u5982\u4f55\u8fdb\u884c\u5339\u914d\u4fee\u6539\u5462\uff1f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u793a\u4f8b2\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>result := $(shell ls | grep \"\\.c\\$$\") #\u5c06\u5f53\u524d\u76ee\u5f55\u4e2d\u7684\u6240\u6709.c\u6587\u4ef6\u5b58\u653e\u5230result\u4e2d\n#\u53ef\u4ee5\u4f7f\u7528\u901a\u914d\u7b26%\u6765\u8fdb\u884c\u5339\u914d\nresult := $(patsubst %.c,%.cpp,$(result)) #\u5c06result\u4e2d\u7684.c\u66ff\u6362\u4e3a.cpp\n\ndebug : #\u6253\u5370\n\t@echo $(result)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>2.cpp 3.cpp 4.cpp main.cpp<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u7ed3\u679c\u663e\u800c\u6613\u89c1\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">foreach\u51fd\u6570<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u6807\u51c6\u683c\u5f0f\u2014\u2014<code>$(foreach var,list,text)<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2a\u51fd\u6570\u662f\u4e00\u4e2a<mark style=\"background-color:#8ed1fc\" class=\"has-inline-color\">\u5faa\u73af\u51fd\u6570<\/mark> \uff0c\u5b83\u7684\u6267\u884c\u8fc7\u7a0b\u662f\u5c06list\u4e2d\u9010\u4e00\u63d0\u53d6\u51fa\u5355\u8bcd\uff0c\u7136\u540e\u5b58\u653e\u5230var\u4e2d\uff0c\u63a5\u7740\u8ba9var\u7684\u503c\u5728text\u4e2d\u5b9a\u4e49\u7684\u8868\u8fbe\u5f0f\u8fdb\u884c\u8ba1\u7b97\uff0c\u7136\u540e\u5c06text\u8ba1\u7b97\u7ed3\u679c\u8fd4\u56de\uff0c\u968f\u540e\u5f00\u59cb\u4e0b\u4e00\u8f6e\u5faa\u73af\uff0c\u8ba9\u4e0b\u4e00\u8f6e\u5faa\u73af\u7684\u7ed3\u679c\u548c\u4e4b\u524d\u7684\u7ed3\u679c\u62fc\u63a5\uff08\u5355\u8bcd\u4e0e\u5355\u8bcd\u65f6\u95f4\u4f1a\u6dfb\u52a0\u7a7a\u683c\u9694\u5f00\uff09\uff0c\u5f53\u5faa\u73af\u7ed3\u675f\u540e\u5c06\u7ed3\u679c\u8fd4\u56de\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u6709\u70b9\u7c7b\u4f3cpython\u4e2d\u7684for x in\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#\u6211\u5728\u5f53\u524d\u76ee\u5f55\u4e2d\u5b9a\u4e49\u4e86a b c d e5\u4e2a\u6587\u4ef6\nlist := $(shell ls | grep \"^&#91;a-z]\\$$\") #\u5c06\u5f53\u524d\u76ee\u5f55\u4e2d\u7684\u8fd9\u4e94\u4e2a\u6587\u4ef6\u5b58\u653e\u5230list\u4e2d\n\nresult := $(foreach var,$(list),$(var).c) #\u5c06list\u4e2d\u7684\u6570\u636e\u9010\u4e2a\u62ff\u51fa\u6765\uff0c\u7ed9\u6bcf\u4e2a\u6587\u4ef6\u6dfb\u52a0\u4e00\u4e2a.c\u540e\u7f00\n\ndebug : #\u6253\u5370\n\t@echo $(result)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a.c b.c c.c d.c e.c<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u7531\u793a\u4f8b\u4ee3\u7801\u53ef\u4ee5\u770b\u5230\uff0c\u6700\u7ec8\u7684\u8f93\u51fa\u662f\u6bcf\u4e2a\u7ed3\u679c\u90fd\u52a0\u4e0a\u4e86\u540e\u7f00\uff0c\u4e5f\u5c31\u662f\u5b8c\u6210\u4e86\u4e00\u8f6e\u5faa\u73af<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">dir\u51fd\u6570<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u6807\u51c6\u683c\u5f0f\u2014\u2014<code>$(dir &lt;names&gt; )<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>names<\/code>\u662f\u4e00\u4e2a\u5305\u542b\u4e86\u8def\u5f84\u7684\u6587\u4ef6\u540d\uff0c<code>dir<\/code>\u7684\u529f\u80fd\u5c31\u662f<mark style=\"background-color:#8ed1fc\" class=\"has-inline-color\">\u5206\u79bb\u51fa\u6587\u4ef6\u7684\u8def\u5f84<\/mark> \uff0c\u5982\u679c\u6587\u4ef6\u6ca1\u6709\u5199\u8def\u5f84\uff0c\u4ec5\u4ec5\u53ea\u6709\u4e00\u4e2a\u540d\u5b57\uff0c\u90a3\u4e48\u51fd\u6570\u4f1a\u9ed8\u8ba4\u8865\u5145\u4e00\u4e2a<code>.\/<\/code>\uff0cnames\u53ef\u4ee5\u4e3a\u591a\u4e2a\u6587\u4ef6\uff0c\u4f46\u662f\u53c2\u6570\u4e0e\u53c2\u6570\u4e4b\u95f4\u4e0d\u9700\u8981 <code>,<\/code> \u7b26\u53f7<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u793a\u4f8b1\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#dir \u53c2\u6570\u4e3a1\u65f6\u7684\u60c5\u51b5\nsrc_t := $(shell find .\/ -name 2.c)\n\nresult := $(dir $(src_t))\n\ndebug :\n\t@echo $(result)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.\/<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u793a\u4f8b2\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#dir \u53c2\u6570\u5927\u4e8e\u4e00\u4e2a\u7684\u60c5\u51b5\nsrc := $(shell pwd)\n\nsrc_t := $(shell find .\/ -name 2.c)\n\nresult := $(dir $(src) $(src_t))\n\ndebug :\n\t@echo $(result)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/home\/chxzking\/MR.XU\/service\/ .\/<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">notdir\u51fd\u6570<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u57fa\u672c\u683c\u5f0f\u2014\u2014<code>$(notdir &lt;names&gt;)<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2a\u51fd\u6570\u548cdir\u6070\u597d\u76f8\u53cd\uff0c\u5b83\u7684\u4f5c\u7528\u662f\u63d0<mark style=\"background-color:#8ed1fc\" class=\"has-inline-color\">\u53d6\u8def\u5f84\u4e2d\u975e\u76ee\u5f55\u7684\u90e8\u5206<\/mark>\uff0c\u4e5f\u5c31\u662f\u63d0\u53d6\u6587\u4ef6\u540d \uff0c\u5176\u7528\u6cd5\u548cdir\u5b8c\u5168\u4e00\u6837\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u793a\u4f8b1\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#notdir \u53c2\u6570\u4e3a1\u65f6\u7684\u60c5\u51b5\nsrc_t := $(shell find .\/ -name 2.c)\n \nresult := $(notdir $(src_t))\n \ndebug :\n\t@echo $(result)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>2.c<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u793a\u4f8b2\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#notdir \u53c2\u6570\u5927\u4e8e\u4e00\u4e2a\u7684\u60c5\u51b5\nsrc := $(shell pwd)\n \nsrc_t := $(shell find .\/ -name 2.c)\n \nresult := $(notdir $(src) $(src_t))\n \ndebug :\n\t@echo $(result)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Core 2.c<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">filter\u51fd\u6570<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u6807\u51c6\u683c\u5f0f\u2014\u2014<code>$(filter PATTERN\u2026,TEXT)<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">TEXT\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\u5e8f\u5217\uff0cPATTERN\u4ee3\u8868\u4e86\u4e0d\u9650\u6570\u91cf\u7684\u6a21\u5f0f\uff0cfilter\u51fd\u6570\u80fd\u4eceTEXT\u4e2d<mark style=\"background-color:#8ed1fc\" class=\"has-inline-color\">\u8fc7\u6ee4\u51fa\u6765\u7b26\u5408\u8981\u6c42\u7684\u5b57\u7b26\u4e32<\/mark>\uff0c\u4e0d\u7b26\u5408PATTERN\u7684\u5b57\u7b26\u4e32\u5c06\u4ece\u4e2d\u5220\u9664\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u793a\u4f8b1\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#\u5355\u4e2a\u6a21\u5f0f\nstr := a.c 2.c b.cpp b.h d.s p.so a.a\n\nresult := $(filter %.c,$(str))\n\ndebug :\n\t@echo $(result)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a.c 2.c<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u793a\u4f8b2\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#\u591a\u4e2a\u6a21\u5f0f\nstr := a.c 2.c b.cpp b.h d.s p.so a.a\n\nresult := $(filter %.c %.h,$(str)) #\u6ce8\u610f\uff1a\u6a21\u5f0f\u4e0e\u6a21\u5f0f\u4e4b\u95f4\u4e0d\u9700\u8981\uff0c\u53f7\n\ndebug :\n\t@echo $(result)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a.c 2.c b.h<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">basename\u51fd\u6570<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u6807\u51c6\u683c\u5f0f\u2014\u2014<code>$(basename &lt;names&gt;)<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2a\u51fd\u6570\u7684\u4f5c\u7528\u662f\u4ece\u4e00\u4e2a\u6587\u4ef6\u4e2d<mark style=\"background-color:#8ed1fc\" class=\"has-inline-color\">\u63d0\u53d6\u57fa\u672c\u540d\u90e8\u5206<\/mark>\uff0c\u57fa\u672c\u540d\u5c31\u662f\u9664\u53bb<code>.<\/code>\u53ca\u540e\u7f00\u7684\u90e8\u5206\uff0c\u4f8b\u5982<code>1.c<\/code>\u7684\u57fa\u672c\u540d\u662f<code>1<\/code>\uff0c<code>\/service\/libfun.so<\/code>\u7684\u57fa\u672c\u540d\u662f<code>\/service\/libfun<\/code>\uff0c\u8fd9\u4e2a\u51fd\u6570\u7684\u53c2\u6570\u4e5f\u53ef\u4ee5\u662f\u591a\u4e2a\uff0c\u4e14\u4e2d\u95f4\u4f7f\u7528\u7a7a\u683c\u4f5c\u4e3a\u5206\u9694\u7b26<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u793a\u4f8b1\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#\u5355\u4e2a\u60c5\u51b5\nstr := .\/Core\/moudle\/1.c \n\nresult := $(basename $(str))\n\ndebug :\n\t@echo $(result)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.\/Core\/moudle\/1<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u793a\u4f8b2\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">#\u591a\u4e2a\u60c5\u51b5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>str := .\/Core\/moudle\/1.c 2.c\n\nresult := $(basename $(str))\n\ndebug :\n\t@echo $(result)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.\/Core\/moudle\/1 2<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u7f16\u8bd1<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u7f16\u8bd1\u6d41\u7a0b<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5c06\u4e00\u4e2a<code>.c<\/code>\u6587\u4ef6\u7f16\u8bd1\u6210\u53ef\u6267\u884c\u6587\u4ef6\u7684\u8fc7\u7a0b\u901a\u5e38\u5305\u62ec\u56db\u4e2a\u6b65\u9aa4\uff1a\u9884\u5904\u7406\u3001\u7f16\u8bd1\u3001\u6c47\u7f16\u548c\u94fe\u63a5\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u9884\u5904\u7406\uff08Preprocessing\uff09<\/strong>\uff1a\u9884\u5904\u7406\u5668\uff08\u7f29\u5199\uff1acpp\uff09\u63a5\u53d7\u6e90\u4ee3\u7801<code>.c<\/code>\u6587\u4ef6\uff0c\u5904\u7406\u5176\u4e2d\u7684\u9884\u5904\u7406\u6307\u4ee4\u3002\u9884\u5904\u7406\u6307\u4ee4\u662f\u4ee5<code>#<\/code>\u5f00\u5934\u7684\uff0c\u5982<code>#include<\/code>\u3001<code>#define<\/code>\u7b49\u3002\u9884\u5904\u7406\u5668\u4f1a\u5904\u7406\u8fd9\u4e9b\u6307\u4ee4\uff0c\u6bd4\u5982\u5305\u542b\u5934\u6587\u4ef6\u3001\u5b8f\u66ff\u6362\u7b49\uff0c\u5e76\u751f\u6210\u4e00\u4e2a\u9884\u5904\u7406\u540e\u7684<code>.i<\/code>\u6587\u4ef6\u3002<\/li>\n\n\n\n<li><strong>\u7f16\u8bd1\uff08Compilation\uff09<\/strong>\uff1a\u7f16\u8bd1\u5668\uff08\u7f29\u5199\uff1agcc\uff09\u63a5\u53d7\u9884\u5904\u7406\u540e\u7684<code>.i<\/code>\u6587\u4ef6\uff0c\u8fdb\u884c\u8bcd\u6cd5\u5206\u6790\u3001\u8bed\u6cd5\u5206\u6790\u3001\u8bed\u4e49\u5206\u6790\u548c\u4f18\u5316\u7b49\u6b65\u9aa4\uff0c\u751f\u6210\u6c47\u7f16\u4ee3\u7801<code>.s<\/code>\u6587\u4ef6\u3002<\/li>\n\n\n\n<li><strong>\u6c47\u7f16\uff08Assembly\uff09<\/strong>\uff1a\u6c47\u7f16\u5668\uff08\u7f29\u5199\uff1aas\uff09\u63a5\u53d7\u6c47\u7f16\u4ee3\u7801<code>.s<\/code>\u6587\u4ef6\uff0c\u5c06\u6c47\u7f16\u4ee3\u7801\u8f6c\u6362\u4e3a\u673a\u5668\u8bed\u8a00\u4ee3\u7801\uff0c\u751f\u6210\u76ee\u6807\u6587\u4ef6<code>.o<\/code>\u3002<\/li>\n\n\n\n<li><strong>\u94fe\u63a5\uff08Linking\uff09<\/strong>\uff1a\u94fe\u63a5\u5668\uff08\u7f29\u5199\uff1ald\uff09\u63a5\u53d7\u4e00\u4e2a\u6216\u591a\u4e2a\u76ee\u6807\u6587\u4ef6<code>.o<\/code>\uff0c\u5c06\u5b83\u4eec\u4e0e\u6240\u9700\u7684\u5e93\u4e00\u8d77\u94fe\u63a5\uff0c\u751f\u6210\u53ef\u6267\u884c\u6587\u4ef6\u3002<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u5206\u522b\u5bf9\u5e94\u7684\u547d\u4ee4\u662f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>gcc -E main.c -o main.i  # \u9884\u5904\u7406\ngcc -S main.i -o main.s  # \u7f16\u8bd1\ngcc -c main.s -o main.o  # \u6c47\u7f16\ngcc main.o -o main.out   # \u94fe\u63a5<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0d\u8fc7\u4e5f\u53ef\u4ee5\u4f7f\u7528gcc main.o -o main.out\u4e00\u6761\u547d\u4ee4\u5b8c\u6210\u6574\u4e2a\u8fc7\u7a0b\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7efc\u5408\u4e4b\u524d\u7684\u77e5\u8bc6\uff0c\u53ef\u4ee5\u5199\u51fa\u4e00\u4e2a\u5b8c\u6574\u7684\u591a\u6587\u4ef6\u7f16\u8bd1\u7684makefile\u6587\u4ef6\uff0c\u5047\u8bbe\u5728makefile\u540c\u76ee\u5f55\u6709\u4e09\u4e2a\u6587\u4ef6\uff1a1.h 1.c main.c<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/1.h\u6587\u4ef6\n#ifndef _1_H\n#define _1_H\n#include&lt;stdio.h&gt;\nvoid func2();\n#endif \/\/_1_H<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/1.c\u6587\u4ef6\n#include \"1.h\"\nvoid func2(){\n    printf(\"this is 1.c\\n\");\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/main.c\n#include&lt;stdio.h&gt;\n#include \"1.h\"\nint main(){\n    printf(\"hello world\\n\");\n    func2();\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">makefile\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>c_file := $(shell ls | grep \"\\.c\\$$\") #\u83b7\u53d6\u5f53\u524d\u6587\u4ef6\u5939\u4e2d\u6240\u6709\u7684.c\u6587\u4ef6\u540d\nc_o_temp := $(patsubst %.c,temp_o\/%.o,$(c_file)) #\u5c06.c\u6587\u4ef6\u5b57\u7b26\u4e32\u66ff\u6362\u4e3a\u540c\u540d\u7684temp_o\/\u6587\u4ef6\u540d.o\u6587\u4ef6\n\n.PHONY : deubug clean #\u58f0\u660e\u4f2a\u76ee\u6807\n\ntemp_o\/%.o: %.c #\u5c06\u6bcf\u4e2a.c\u6587\u4ef6\u7f16\u8bd1\u6210\u76ee\u6807\u6587\u4ef6\uff0c\u5e76\u5c06\u6587\u4ef6\u653e\u5165temp_o\u76ee\u5f55\u4e2d\n\t@mkdir -p $(dir $@) \n\t@gcc $^ -o $@ -c\n\nworkspace\/main:$(c_o_temp) #\u5c06\u76ee\u6807\u6587\u4ef6\u7f16\u8bd1\u6210\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u5e76\u653e\u5165worksapce\u76ee\u5f55\u4e2d\n\t@mkdir -p $(dir $@)\n\t@gcc $^ -o $@ \t\n\ndebug: workspace\/main #\u4f2a\u76ee\u6807debug\uff1a\u81ea\u5b9a\u4e49\u7f16\u8bd1\u547d\u4ee4\uff0c\u5e76\u6267\u884c\u6700\u7ec8\u7f16\u8bd1\u7ed3\u679c\n\t@.\/$^\n\nclean: #\u4f2a\u76ee\u6807clean\uff0c\u6e05\u9664\u6240\u6709\u7684.o\u6587\u4ef6\uff0c\u5220\u9664workspace\u3001temp_o\u76ee\u5f55\n\t@rm -rf *.o workspace temp_o<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff08make debug\uff09\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>hello world\nthis is 1.c<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e3a\u4e86\u5e94\u5bf9\u5404\u79cd\u60c5\u51b5\uff0c\u4ee5\u4e0b\u8fd8\u6709\u5e38\u7528\u7684gcc\u7f16\u8bd1\u9009\u9879<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">gcc\u7f16\u8bd1\u7684\u9009\u9879<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>-m64<\/code>\uff1a\u751f\u621064\u4f4d\u4ee3\u7801\u3002<\/li>\n\n\n\n<li><code>-std=<\/code>\uff1a\u6307\u5b9a\u7f16\u8bd1\u5668\u5e94\u9075\u5faa\u7684C++\u6807\u51c6\uff0c\u4f8b\u5982<code>-std=c++11<\/code>\u8868\u793a\u4f7f\u7528C++11\u6807\u51c6\u3002<\/li>\n\n\n\n<li><code>-g<\/code>\uff1a\u751f\u6210\u8c03\u8bd5\u4fe1\u606f\uff0c\u4ee5\u4fbf\u4f7f\u7528gdb\u7b49\u8c03\u8bd5\u5668\u8fdb\u884c\u8c03\u8bd5\u3002<\/li>\n\n\n\n<li><code>-w<\/code>\uff1a\u6291\u5236\u6240\u6709\u8b66\u544a\u4fe1\u606f\u7684\u8f93\u51fa\u3002<\/li>\n\n\n\n<li><code>-O<\/code>\uff1a-O\u9009\u9879\u7528\u4e8e\u63a7\u5236\u4f18\u5316\u7ea7\u522b\u3002-O1\uff1a\u542f\u7528\u4e00\u4e9b\u4f18\u5316\uff0c\u540c\u65f6\u4e0d\u4f1a\u663e\u8457\u589e\u52a0\u7f16\u8bd1\u65f6\u95f4\u3002-O2\uff1a\u542f\u7528\u8fdb\u4e00\u6b65\u7684\u4f18\u5316\uff0c\u5305\u62ec\u6240\u6709-O1\u7ea7\u522b\u7684\u4f18\u5316\u548c\u5176\u4ed6\u5927\u591a\u6570\u4f18\u5316\u3002-O3\uff1a\u542f\u7528\u6240\u6709\u4f18\u5316\uff0c\u5305\u62ec\u6240\u6709-O2\u7ea7\u522b\u7684\u4f18\u5316\u548c\u5176\u4ed6\u4e00\u4e9b\u989d\u5916\u7684\u4f18\u5316\u3002<\/li>\n\n\n\n<li><code>-I<\/code>\uff1a\u6dfb\u52a0\u5305\u542b\u6587\u4ef6\u7684\u641c\u7d22\u8def\u5f84\uff0c\u4f8b\u5982<code>-I\/home\/user\/include<\/code>\u4f1a\u5728<code>\/home\/user\/include<\/code>\u76ee\u5f55\u4e0b\u641c\u7d22\u5934\u6587\u4ef6\u3002<\/li>\n\n\n\n<li><code>-fPIC<\/code>\uff1a\u751f\u6210\u4f4d\u7f6e\u65e0\u5173\u4ee3\u7801(Position Independent Code)\uff0c\u901a\u5e38\u7528\u4e8e\u52a8\u6001\u94fe\u63a5\u5e93\u7684\u7f16\u8bd1<\/li>\n\n\n\n<li><code>-l<\/code>\uff1a\u94fe\u63a5\u5e93\uff0c\u540e\u9762\u901a\u5e38\u8ddf\u5e93\u540d\uff0c\u4f8b\u5982<code>-lm<\/code>\u8868\u793a\u94fe\u63a5\u6570\u5b66\u5e93\uff08libm\uff09\u3002<\/li>\n\n\n\n<li><code>-L<\/code>\uff1a\u6dfb\u52a0\u5e93\u6587\u4ef6\u7684\u641c\u7d22\u8def\u5f84\uff0c\u4f8b\u5982<code>-L\/home\/user\/lib<\/code>\u4f1a\u5728<code>\/home\/user\/lib<\/code>\u76ee\u5f55\u4e0b\u641c\u7d22\u5e93\u6587\u4ef6\u3002<\/li>\n\n\n\n<li><code>-Wl,&lt;\u9009\u9879&gt;<\/code>\uff1a\u5c06<code>&lt;\u9009\u9879&gt;<\/code>\u4f20\u9012\u7ed9\u94fe\u63a5\u5668\u3002\u4f8b\u5982\uff0c<code>-Wl,-rpath,\/home\/user\/lib<\/code>\u4f1a\u5c06<code>\/home\/user\/lib<\/code>\u6dfb\u52a0\u5230\u8fd0\u884c\u65f6\u5e93\u641c\u7d22\u8def\u5f84\u3002<\/li>\n\n\n\n<li><code>-rpath=<\/code>\uff1a\u8bbe\u7f6e\u8fd0\u884c\u65f6\u5e93\u641c\u7d22\u8def\u5f84\uff0c\u8fd9\u662f\u94fe\u63a5\u5668\u9009\u9879\uff0c\u901a\u5e38\u5f62\u5f0f\u4e3a<code>-Wl,-rpath,\u8def\u5f84<\/code>\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee5<code>-I<\/code>\u4e3e\u4f8b<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f9d\u65e7\u4ee51.h 1.c main.c\u4e09\u4e2a\u6587\u4ef6\u4e3e\u4f8b\uff0c\u4f46\u662f\u5047\u8bbe\u8fd9\u4e09\u4e2a\u6587\u4ef6\u5728\u4e0d\u540c\u7684\u76ee\u5f55\u4e2d\u3002\u90a3\u4e48makefile\u4ee3\u7801\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>c_file := $(shell find .\/ -name \"*.c\") #\u83b7\u53d6make\u540c\u76ee\u5f55\u53ca\u5b50\u76ee\u5f55\u4e2d\u6240\u6709\u7684.c\u6587\u4ef6\u540d\nc_o_temp := $(patsubst .\/%.c,temp_o\/%.o,$(c_file)) #\u5c06.c\u6587\u4ef6\u5b57\u7b26\u4e32\u66ff\u6362\u4e3a\u540c\u540d\u7684temp_o\/\u6587\u4ef6\u540d.o\u6587\u4ef6\n\nc_h :=$(shell find .\/ -name \"*.h\") #\u83b7\u53d6make\u540c\u76ee\u5f55\u53ca\u5b50\u76ee\u5f55\u4e2d\u6240\u6709\u7684.h\u6587\u4ef6\u540d\nc_h_path := $(dir $(patsubst %,-I%,$(c_h))) #\u6574\u5408-I\u547d\u4ee4\n\n.PHONY : debug clean #\u58f0\u660e\u4f2a\u76ee\u6807\n\ntemp_o\/%.o: %.c #\u5c06\u6bcf\u4e2a.c\u6587\u4ef6\u7f16\u8bd1\u6210\u76ee\u6807\u6587\u4ef6\uff0c\u5e76\u5c06\u6587\u4ef6\u653e\u5165temp_o\u76ee\u5f55\u4e2d\n\t@gcc $&lt; -o $@ -c $(c_h_path)\n\nworkspace\/main:$(c_o_temp) #\u5c06\u76ee\u6807\u6587\u4ef6\u7f16\u8bd1\u6210\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u5e76\u653e\u5165worksapce\u76ee\u5f55\u4e2d\n\t@mkdir -p $(dir $@)\n\t@gcc $^ -o $@ \t\n\ndebug: workspace\/main #\u4f2a\u76ee\u6807debug\uff1a\u81ea\u5b9a\u4e49\u7f16\u8bd1\u547d\u4ee4\uff0c\u5e76\u6267\u884c\u6700\u7ec8\u7f16\u8bd1\u7ed3\u679c\n\t@.\/$^\n\nclean: #\u4f2a\u76ee\u6807clean\uff0c\u6e05\u9664\u6240\u6709\u7684.o\u6587\u4ef6\uff0c\u5220\u9664workspace\u3001temp_o\u76ee\u5f55\n\t@rm -rf *.o workspace temp_o<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff08make debug\uff09\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>hello world\nthis is 1.c<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u9759\u6001\u5e93<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">\u9759\u6001\u5e93\u7f16\u8bd1<\/h5>\n\n\n\n<p class=\"wp-block-paragraph\">\u52a8\u6001\u5e93\u7f16\u8bd1\u4e3b\u8981\u5206\u4e3a\u6e90\u6587\u4ef6\u7f16\u8bd1\u548c\u52a8\u6001\u5e93\u521b\u5efa\u4e24\u4e2a\u8fc7\u7a0b<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u6e90\u6587\u4ef6\u7f16\u8bd1\uff1a<\/mark>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5c06\u6bcf\u4e2a\u6e90\u6587\u4ef6\uff08<code>.c<\/code>\u6587\u4ef6\uff09\u7f16\u8bd1\u6210\u76ee\u6807\u6587\u4ef6\uff08<code>.o<\/code>\u6587\u4ef6\uff09\u3002\u8fd9\u53ef\u4ee5\u4f7f\u7528<code>gcc<\/code>\u7684<code>-c<\/code>\u9009\u9879\u6765\u5b8c\u6210\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5047\u8bbe\u6709\u4e00\u4e2atemp.c\u6587\u4ef6<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>gcc temp.c -o temp.o -c<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u521b\u5efa\u9759\u6001\u5e93\uff1a<\/mark>\u4f60\u53ef\u4ee5\u4f7f\u7528<code>gcc<\/code>\u7684<code>-shared<\/code>\u9009\u9879\u6765\u5c06\u4e00\u4e2a\u6216\u591a\u4e2a\u76ee\u6807\u6587\u4ef6\u6253\u5305\u6210\u4e00\u4e2a\u9759\u6001\u5e93\uff08<code>.<\/code>a\u6587\u4ef6\uff09\u3002\u901a\u5e38\u9759\u6001\u5e93\u7684\u547d\u540d\u662f<code>lib<\/code>\u5f00\u5934<code>.<\/code>a\u7ed3\u5c3e<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ar rcs libtemp.a temp.o<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u9664\u6b64\u4e4b\u5916\uff0c\u9759\u6001\u5e93\u662f\u53ef\u4ee5\u5b9e\u73b0\u4e00\u6b21\u6027\u7f16\u8bd1\u7684\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>gcc temp.c -o temp.o -c &amp;&amp; ar rcs libtemp.a temp.o<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">\u9759\u6001\u5e93\u7684\u94fe\u63a5<\/h5>\n\n\n\n<p class=\"wp-block-paragraph\">\u5047\u8bbe\u6211\u5728\u5f53\u524d\u76ee\u5f55\u6709\u4e00\u4e2a\u9759\u6001\u5e93libtemp.a\uff0c\u8fd9\u4e2a\u547d\u4ee4\u7684<code>-L<\/code>\u9700\u8981\u6307\u5b9a\u9759\u6001\u5e93\u7684\u4f4d\u7f6e\uff0c<code>-l<\/code>\u662f\u9759\u6001\u5e93\u7684\u540d\u5b57\uff0c\u4f46\u662f\u4e0d\u9700\u8981\u52a0\u4e0a\u5f00\u5934\u7684<code>lib<\/code>\u548c\u7ed3\u5c3e\u7684<code>.a<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>gcc main.c -o main -L.\/ -ltemp <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u793a\u4f8b\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5047\u8bbe\u5f53\u524d\u76ee\u5f55\u5b58\u57281.c 1.h 2.c 2.h main.c \u6587\u4ef6<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>c_file := $(shell ls | grep \"&#91;0-9]\\.c\\$$\") #\u5bfb\u627e.c\u6587\u4ef6\nc_o := $(patsubst %.c,temp_o\/%.o,$(c_file))\n\ntemp_o\/%.o:%.c #\u6c47\u7f16\u6210\u76ee\u6807\u6587\u4ef6\n\t@mkdir -p $(dir $@)\n\t@gcc $&lt; -o $@ -c\n\nworkspace\/libtemp.a:$(c_o) #\u751f\u6210\u9759\u6001\u5e93\n\t@mkdir -p $(dir $@)\n\t@ar rcs $@ $^\n\ndebug : workspace\/libtemp.a #\u94fe\u63a5\u9759\u6001\u5e93\n\t@gcc main.c -o main -L.\/workspace -ltemp  \n\t@.\/main\n\nclean :\n\t@rm -rf *.o main temp_o workspace\n\n.PHONY : debug clean <\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u52a8\u6001\u5e93<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">\u52a8\u6001\u5e93\u7f16\u8bd1<\/h5>\n\n\n\n<p class=\"wp-block-paragraph\">\u52a8\u6001\u5e93\u7f16\u8bd1\u4e3b\u8981\u5206\u4e3a\u6e90\u6587\u4ef6\u7f16\u8bd1\u548c\u52a8\u6001\u5e93\u521b\u5efa\u4e24\u4e2a\u8fc7\u7a0b<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u6e90\u6587\u4ef6\u7f16\u8bd1\uff1a<\/mark>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5c06\u6bcf\u4e2a\u6e90\u6587\u4ef6\uff08<code>.c<\/code>\u6587\u4ef6\uff09\u7f16\u8bd1\u6210\u76ee\u6807\u6587\u4ef6\uff08<code>.o<\/code>\u6587\u4ef6\uff09\u3002\u8fd9\u53ef\u4ee5\u4f7f\u7528<code>gcc<\/code>\u7684<code>-c<\/code>\u9009\u9879\u6765\u5b8c\u6210\u3002\u4e0d\u8fc7\uff0c\u7531\u4e8e\u6211\u4eec\u8981\u521b\u5efa\u7684\u662f\u52a8\u6001\u5e93\uff0c\u6240\u4ee5\u8fd8\u9700\u8981\u6dfb\u52a0<code>-fPIC<\/code>\u9009\u9879\u6765\u751f\u6210\u4f4d\u7f6e\u65e0\u5173\u4ee3\u7801\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5047\u8bbe\u6709\u4e00\u4e2atemp.c\u6587\u4ef6<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>gcc temp.c -o temp.o -c -fPIC<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u521b\u5efa\u52a8\u6001\u5e93\uff1a<\/mark>\u4f60\u53ef\u4ee5\u4f7f\u7528<code>gcc<\/code>\u7684<code>-shared<\/code>\u9009\u9879\u6765\u5c06\u4e00\u4e2a\u6216\u591a\u4e2a\u76ee\u6807\u6587\u4ef6\u6253\u5305\u6210\u4e00\u4e2a\u52a8\u6001\u5e93\uff08<code>.so<\/code>\u6587\u4ef6\uff09\u3002\u901a\u5e38\u52a8\u6001\u5e93\u7684\u547d\u540d\u662f<code>lib<\/code>\u5f00\u5934<code>.so<\/code>\u7ed3\u5c3e<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>gcc temp.o -o libtemp.so -shared<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6b64\u5916\uff0c\u5b83\u4e5f\u5b58\u5728\u4e00\u6b21\u7f16\u8bd1\u7684\u4ee3\u7801\u7684\u65b9\u5f0f<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>gcc temp.c -o libtemp.so -fPIC -shared<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">\u52a8\u6001\u5e93\u7684\u94fe\u63a5<\/h5>\n\n\n\n<p class=\"wp-block-paragraph\">\u5047\u8bbe\u6211\u5728\u5f53\u524d\u76ee\u5f55\u6709\u4e00\u4e2a\u52a8\u6001\u5e93libtemp.so\uff0c\u8fd9\u4e2a\u547d\u4ee4\u7684<code>-L<\/code>\u9700\u8981\u6307\u5b9a\u52a8\u6001\u5e93\u7684\u4f4d\u7f6e\uff0c<code>-l<\/code>\u662f\u52a8\u6001\u5e93\u7684\u540d\u5b57\uff0c\u4f46\u662f\u4e0d\u9700\u8981\u52a0\u4e0a\u5f00\u5934\u7684<code>lib<\/code>\u548c\u7ed3\u5c3e\u7684<code>.so<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>gcc main.c -o main -L.\/ -ltemp <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u9664\u6b64\u4e4b\u5916\u8fd8\u9700\u8981\u5176\u4ed6\u64cd\u4f5c\u4ee5\u4fbf\u4e8e\u64cd\u4f5c\u7cfb\u7edf\u8c03\u7528\u5e93\uff0c\u8be6\u60c5\u8bf7\u89c1<a href=\"http:\/\/blog.chxzking.cn\/index.php\/2024\/02\/23\/linux%e4%b8%8b%e7%9a%84c%e5%8a%a8%e6%80%81%e5%ba%93\/\" data-type=\"link\" data-id=\"http:\/\/blog.chxzking.cn\/index.php\/2024\/02\/23\/linux%e4%b8%8b%e7%9a%84c%e5%8a%a8%e6%80%81%e5%ba%93\/\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">Linux\u4e0b\u7684\u52a8\u6001\u5e93\u4e0e\u9759\u6001\u5e93<\/mark><\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u793a\u4f8b\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5047\u8bbe\u5f53\u524d\u76ee\u5f55\u5b58\u57281.c 1.h 2.c 2.h main.c \u6587\u4ef6<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>c_file := $(shell ls | grep \"&#91;0-9]\\.c\\$$\") #\u5bfb\u627e.c\u6587\u4ef6\nc_o := $(patsubst %.c,temp_o\/%.o,$(c_file))\n\ntemp_o\/%.o:%.c #\u6c47\u7f16\u6210\u76ee\u6807\u6587\u4ef6\n\t@mkdir -p $(dir $@)\n\t@gcc $&lt; -o $@ -c -fPIC\n\nworkspace\/libtemp.so:$(c_o) #\u751f\u6210\u52a8\u6001\u5e93\n\t@mkdir -p $(dir $@)\n\t@gcc $^ -o $@ -shared\n\ndebug : workspace\/libtemp.so #\u94fe\u63a5\u52a8\u6001\u5e93\n\t@gcc main.c -o main -L.\/workspace -ltemp -Wl,-rpath,.\/workspace \n\t@.\/main\n\nclean :\n\t@rm -rf *.o main temp_o workspace\n\n.PHONY : debug clean <\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u6761\u4ef6\u5224\u65ad<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Makefile \u8bed\u6cd5\u4e2d\u5b58\u5728\u6761\u4ef6\u5224\u65ad\u3002\u8fd9\u4e9b\u6761\u4ef6\u5224\u65ad\u4e3b\u8981\u901a\u8fc7 ifeq\u3001ifneq\u3001ifdef \u548c ifndef \u7b49\u5173\u952e\u5b57\u6765\u5b9e\u73b0\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>ifeq<\/code>\u00a0\u548c\u00a0<code>ifneq<\/code>\u00a0\u7528\u4e8e\u6bd4\u8f83\u53d8\u91cf\u7684\u503c\u662f\u5426\u76f8\u7b49\u6216\u4e0d\u76f8\u7b49\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ifeq ($(first), $(second))\n    echo \"first == second\"\nelse\n    echo \"first != second\"\nendif<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5982\u679c\u53d8\u91cf first \u548c second \u7684\u503c\u76f8\u7b49\uff0c\u90a3\u4e48\u5c06\u6267\u884c echo &#8220;first == second&#8221;\uff0c\u5426\u5219\u5c06\u6267\u884c echo &#8220;first != second&#8221;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u793a\u4f8b2\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>ifdef<\/code>\u00a0\u548c\u00a0<code>ifndef<\/code>\u00a0\u7528\u4e8e\u68c0\u67e5\u53d8\u91cf\u662f\u5426\u5df2\u5b9a\u4e49\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ifdef VAR\n    echo \"VAR is defined\"\nelse\n    echo \"VAR is not defined\"\nendif<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5982\u679c\u53d8\u91cf VAR \u5df2\u5b9a\u4e49\uff0c\u90a3\u4e48\u5c06\u6267\u884c echo &#8220;VAR is defined&#8221;\uff0c\u5426\u5219\u5c06\u6267\u884c echo &#8220;VAR is not defined&#8221;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u6a21\u5f0f\u89c4\u5219<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 Makefile \u4e2d\uff0c\u6a21\u5f0f\u89c4\u5219\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u89c4\u5219\uff0c\u5b83\u5141\u8bb8\u6211\u4eec\u6839\u636e\u6587\u4ef6\u540d\u7684\u6a21\u5f0f\u6765\u5b9a\u4e49\u89c4\u5219\u3002\u6a21\u5f0f\u89c4\u5219\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u76ee\u6807\u6a21\u5f0f: \u4f9d\u8d56\u6a21\u5f0f\n    \u547d\u4ee4<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u6a21\u5f0f\u89c4\u5219\u4e2d\uff0c\u76ee\u6807\u540d\u548c\u4f9d\u8d56\u540d\u4e2d\u9700\u8981\u5305\u542b\u6709\u6a21\u5f0f\u5b57\u7b26 %\u3002% \u53ef\u4ee5\u5339\u914d\u4efb\u4f55\u975e\u7a7a\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u6a21\u5f0f\u89c4\u5219 %.o : %.c\uff0c\u5b83\u8868\u793a\u7684\u542b\u4e49\u662f\uff1a\u6240\u6709\u7684 .o \u6587\u4ef6\u4f9d\u8d56\u4e8e\u5bf9\u5e94\u7684 .c \u6587\u4ef6\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u6267\u884c\u6a21\u5f0f\u89c4\u5219\u65f6\uff0c\u547d\u4ee4\u884c\u4e2d\u7684\u81ea\u52a8\u5316\u53d8\u91cf\u5c06\u6839\u636e\u5b9e\u9645\u7684\u76ee\u6807\u548c\u4f9d\u8d56\u6587\u4ef6\u53d6\u5bf9\u5e94\u503c\uff0c\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>%.o : %.c\n    $(CC) -c $(CFLAGS) $&lt; -o $@\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u547d\u4ee4\u89c4\u5219 makefile\u7684\u57fa\u672c\u683c\u5f0f\uff1a targets : prerequisties [tab\u952e]comma [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,1],"tags":[17,18,33,37,36],"class_list":["post-885","post","type-post","status-publish","format-standard","hentry","category-c-c","category-learning","tag-c-2","tag-c","tag-linux","tag-makefile","tag-make"],"_links":{"self":[{"href":"http:\/\/blog.chxzking.cn\/index.php\/wp-json\/wp\/v2\/posts\/885","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.chxzking.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.chxzking.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.chxzking.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.chxzking.cn\/index.php\/wp-json\/wp\/v2\/comments?post=885"}],"version-history":[{"count":27,"href":"http:\/\/blog.chxzking.cn\/index.php\/wp-json\/wp\/v2\/posts\/885\/revisions"}],"predecessor-version":[{"id":967,"href":"http:\/\/blog.chxzking.cn\/index.php\/wp-json\/wp\/v2\/posts\/885\/revisions\/967"}],"wp:attachment":[{"href":"http:\/\/blog.chxzking.cn\/index.php\/wp-json\/wp\/v2\/media?parent=885"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.chxzking.cn\/index.php\/wp-json\/wp\/v2\/categories?post=885"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.chxzking.cn\/index.php\/wp-json\/wp\/v2\/tags?post=885"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}