{"id":490,"date":"2025-11-03T17:58:53","date_gmt":"2025-11-03T09:58:53","guid":{"rendered":"https:\/\/blog.keruone.cn\/?p=490"},"modified":"2025-11-10T13:46:40","modified_gmt":"2025-11-10T05:46:40","slug":"makefile_simple_study","status":"publish","type":"post","link":"https:\/\/blog.keruone.cn\/index.php\/2025\/11\/03\/makefile_simple_study\/","title":{"rendered":"makefile\u7b80\u5355\u521d\u63a2\u7d22"},"content":{"rendered":"<h1>makefile\u7b80\u5355\u521d\u63a2\u7d22<\/h1>\n<p>\u7cfb\u7edf ubuntu20.04<br \/>\n\u53c2\u8003<a class=\"wp-editor-md-post-content-link\" href=\"http:\/\/47.111.11.73\/docs\/boards\/arm-linux\/zdyz-i.mx6ull.html\">\u6b63\u70b9\u539f\u5b50<\/a><\/p>\n<p>\u53ea\u662f\u5b66\u4e60\u4e2d\u81ea\u5df1\u7684\u5c0f\u8bb0<\/p>\n<hr \/>\n<h2>0.\u5f15\u5b50<\/h2>\n<p>\u6211\u4eec\u5e38\u5e38\u4f1a\u9047\u5230\u6709c\u7f16\u8bd1\u7684\u9700\u6c42\uff0c\u5728linux\u73af\u5883\u4e0b\u4e00\u822c\u4f7f\u7528gcc\u7f16\u8bd1\uff0c\u5982<\/p>\n<pre><code class=\"language-bash line-numbers\">gcc main.c -o main\n<\/code><\/pre>\n<p>\u5982\u679c\u8fd8\u6709\u5f15\u7528\u522b\u7684\u6587\u4ef6\uff0c\u5219<\/p>\n<pre><code class=\"language-bash line-numbers\">gcc main.c file1.c file2.c -o main\n<\/code><\/pre>\n<p>\u8fd9\u5728\u4ec5\u6709\u4e00\u4e2a\u6587\u4ef6\u6216\u5c11\u91cf\u6587\u4ef6\u65f6\uff0c\u662f\u975e\u5e38\u65b9\u4fbf\u4f7f\u7528\u7684\u3002<\/p>\n<hr \/>\n<p>\u4f46\u662f\u8fd9\u91cc<strong>\u6709\u4e24\u4e2a\u95ee\u9898<\/strong>\uff1a<br \/>\n&#8211; \u6587\u4ef6\u4e0d\u53ef\u80fd\u4e00\u76f4\u8fd9\u4e48\u5c11\uff0c\u968f\u7740\u5de5\u7a0b\u7684\u589e\u5927\uff0c\u6587\u4ef6\u53ea\u4f1a\u8d8a\u6765\u8d8a\u591a<\/p>\n<blockquote><p>\n  \u8fd9\u4e2a\u65f6\u5019\u4f60\u603b\u4e0d\u80fd\u8fd8\u5728<code>gcc<\/code>\u540e\u9762\u4e00\u76f4\u52a0\u6587\u4ef6\u5427<br \/>\n  &#8211; \u5f53\u6587\u4ef6\u591a\u8d77\u6765\u540e\uff0c\u7f16\u8bd1\u4e5f\u5f88\u8d39\u65f6\uff0c\u603b\u4e0d\u80fd\u6bcf\u6b21\u90fd\u91cd\u65b0\u7f16\u8bd1\u5427<br \/>\n  \u8fd9\u4e2a\u65f6\u5019\u94fe\u63a5\u5c31\u597d\u7528\u591a\u4e86\n<\/p><\/blockquote>\n<p>\u8fd9\u53ef\u4e0d\u597d\uff01\u9700\u8981\u60f3\u529e\u6cd5\u89e3\u51b3\uff01<br \/>\n\u5bf9\u6b64\uff0c\u5c31\u8f6e\u5230 <strong>make<\/strong> \u5927\u5c55\u8eab\u624b\u5566\uff01<\/p>\n<hr \/>\n<h2>1. \u4ece\u5c0f\u6848\u4f8b\u5f00\u59cb<\/h2>\n<h3>1.1 \u51c6\u5907\u5de5\u4f5c<\/h3>\n<p>\u8fd9\u91cc\uff0c\u6211\u4eec\u5148\u5199\u4e2a\u5f88\u4e50\u8272\u7684\u4ee3\u7801<\/p>\n<pre><code class=\"language-c line-numbers\">\/* main.c *\/\n#include \"io.h\"\n#include \"calcu.h\"\n\nint main(void)\n{\n    int num1, num2;\n    int answer;\n    get_two_number(&amp;num1,&amp;num2);\n    answer = my_add(num1,num2);\n    display_number(&amp;answer);\n\n    return 0;\n}\n<\/code><\/pre>\n<details>\n<summary> calcu.c <\/summary>\n<pre><code class=\"language-C line-numbers\">\/* calcu.c *\/\n#include \"calcu.h\"\n\nint my_add(int num1, int num2)\n{\n    return num1 + num2;\n}\n\n<\/code><\/pre>\n<\/details>\n<details>\n<summary> io.c <\/summary>\n<pre><code class=\"language-C line-numbers\">\/* io.c *\/\n#include \"io.h\"\n\nvoid get_two_number(int* pnum1, int* pnum2)\n{\n    printf(\"input 2 number here:\\n\");\n    scanf(\"%d%d\", pnum1,pnum2);\n}\n\nvoid display_number(int* pnum)\n{\n    printf(\"%d\\n\",*pnum);\n}\n\n<\/code><\/pre>\n<\/details>\n<details>\n<summary> calcu.h <\/summary>\n<pre><code class=\"language-C line-numbers\">\/* calcu.h *\/\n#ifndef __CALCU_H__\n#define __CALCU_H__\n\nint my_add(int,int);\n\n#endif\n\n<\/code><\/pre>\n<\/details>\n<details>\n<summary> io.h <\/summary>\n<pre><code class=\"language-C line-numbers\">\/* io.h*\/\n#ifndef __MYIO_H__\n#define __MYIO_H__\n\n#include &lt;stdio.h&gt;\n\nvoid get_two_number(int*,int*);\nvoid display_number(int*);\n\n#endif\n\n<\/code><\/pre>\n<\/details>\n<h3>1.2 \u6700\u57fa\u7840\u7684makefile<\/h3>\n<p>\u9996\u5148\u6211\u4eec\u5148\u4ecb\u7ecdmakefile\u7684\u57fa\u7840\u89c4\u5219<br \/>\nmakefile\u53ef\u4ee5\u62c6\u6210\u5982\u4e0b\u7684\u57fa\u7840\u5757<\/p>\n<pre><code class=\"language-makefile line-numbers\">target: dependence\n    command1\n    command2\n    ...\n<\/code><\/pre>\n<p>\u5176\u4e2d\uff0ctarget\u662f\u4f60\u7684\u76ee\u6807\uff0cdependence\u662f\u4f60\u7684\u4f9d\u8d56\u9879\u3002<br \/>\n\u5f53\u4f60\u6267\u884c<code>make target(\u8fd9\u91cc\u66f4\u6362\u4e3a\u5b9e\u9645target)<\/code>\u65f6\uff0cmake\u5de5\u5177\u4f1a\u68c0\u67e5dependence\u662f\u5426\u5b58\u5728\u6216\u6ee1\u8db3\u3002\u5982\u679c\u6ee1\u8db3\uff0c\u5219\u4f1a\u6267\u884c\u547d\u4ee4command\u3002<\/p>\n<h4>version 0<\/h4>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u4e00\u6b65\u4e00\u6b65\u6539\u9020\u6211\u4eec\u7684makefile\uff0c\u8fd9\u91cc\u6709\u51e0\u70b9\u63d0\u9192\u8981\u6ce8\u610f\uff1a<br \/>\n&#8211; \u7f29\u8fdb\u8981\u662f<code>Tab<\/code>\uff0c\u4f46\u662f\u67d0\u4e9b\u5e73\u53f0\u4f1a\u5c06<code>Tab<\/code>\u8f6c\u6362\u4e3a4\u4e2a\u7a7a\u683c\uff0c\u8fd9\u5c06\u4f1a\u5bfc\u81f4makefile<strong>\u62a5\u9519<\/strong>\uff01<\/p>\n<h4>version 1<\/h4>\n<p>\u5bf9\u6b64\uff0c\u6211\u4eec\u4e0a\u8ff0\u7684\u4ee3\u7801\u7528make\u6765\u5b9e\u73b0\uff0c\u5c31\u5e94\u8be5\u662f<\/p>\n<pre><code class=\"language-makefile line-numbers\">main: main.c io.c calcu.c\n    gcc main.c io.c calcu.c -o main\n\nclean:\n    rm main\n    rm *.o\n<\/code><\/pre>\n<h4>version 2<\/h4>\n<p>\u4f46\u662f\u8fd9\u6837\u89e3\u51b3\u4e0d\u4e86\u95ee\u98982,\u8fd9\u5b9e\u9645\u4e0a\u8fd8\u662f\u5168\u7f16\u8bd1\u3002<br \/>\n\u5bf9\u6b64\uff0c\u6211\u4eec\u8981\u628a\u5b83\u62c6\u5f00<\/p>\n<pre><code class=\"language-makefile line-numbers\">main: main.o io.o calcu.o\n    gcc main.o io.o calcu.o -o main\n\nmain.o: main.c\n    gcc -c main.c\n\ncalcu.o: calcu.c\n    gcc -c calcu.c\n\nio.o: io.c\n    gcc -c io.c\n\nclean:\n    rm main\n    rm *.o\n<\/code><\/pre>\n<p>\u5f88\u597d\uff0c\u73b0\u5728\u7f16\u8bd1\u5c31\u4e0d\u662f\u5168\u7f16\u8bd1\u4e86\u3002<\/p>\n<h4>version 3<\/h4>\n<p>\u4e0d\u8fc7\u8fd9\u6837\u7684\u7f16\u5199\u592a\u5197\u957f\u4e86\uff0c\u5982<code>main.o io.o calcu.o<\/code>\u91cd\u590d\u5199\u5c31\u5f88\u70e6\u3002<br \/>\n\u8fd9\u4e2a\u65f6\u5019\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u4f7f\u7528\u53d8\u91cf<\/p>\n<pre><code class=\"language-makefile line-numbers\">objects = main.o io.o calcu.o\nmain: <span class=\"katex math inline\">(objects)\n    gcc<\/span>(objects) -o main\n\nmain.o: main.c\n    gcc -c main.c\n\ncalcu.o: calcu.c\n    gcc -c calcu.c\n\nio.o: io.c\n    gcc -c io.c\n\nclean:\n    rm main\n    rm *.o\n\n<\/code><\/pre>\n<h4>version 4<\/h4>\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u867d\u7136\u5df2\u7ecf\u8db3\u591f\u7b80\u6d01\u4e86\uff0c\u4f46\u662f\u4e00\u4e2a\u4e00\u4e2a\u7684\u5199 gcc \u7f16\u8bd1\u8fd8\u662f\u6709\u4e9b\u70e6\u3002<br \/>\n\u6b64\u65f6\uff0c\u901a\u914d\u7b26\u5c31\u51fa\u9a6c\u4e86\u3002<\/p>\n<pre><code class=\"language-makefile line-numbers\">objects = main.o io.o calcu.o\nmain: <span class=\"katex math inline\">(objects)\n    gcc<\/span>(objects) -o main\n\n%.o: %.c\n    gcc -c $&lt;\n\nclean:\n    rm main\n    rm *.o\n\n<\/code><\/pre>\n<p>\u5176\u4e2d<code>%<\/code>\u8868\u793a\u4efb\u610f\u6587\u5b57\uff0c<code>%.o: %.c<\/code> \u8868\u793a\uff1a\u4efb\u4f55 <code>.o<\/code> \u6587\u4ef6\u90fd\u53ef\u4ee5\u7531<strong>\u540c\u540d<\/strong>\u7684 <code>.c<\/code> \u6587\u4ef6\u751f\u6210\u3002\uff08\u5728dependence\u91cc\u7684<code>%<\/code>\u5177\u4f53\u662f\u4ec0\u4e48\u5185\u5bb9\uff0c\u53d6\u51b3\u4e8e\u6267\u884c\u6b64\u89c4\u5219\u65f6\u5019\u7684target\u4e2d\u7684<code>%<\/code>\u662f\u4ec0\u4e48\u5185\u5bb9\uff09<br \/>\n\u5728command\u4e2d\u7684<code>$&lt;<\/code>\uff0c\u8868\u793a\u7b2c\u4e00\u4e2a\u4f9d\u8d56\u9879\uff08\u5982\u679c\u6709\u591a\u4e2a\uff09\u3002<\/p>\n<h4>version 5<\/h4>\n<p>\u4e0a\u6587\u5df2\u7ecf\u8db3\u591f\u7b80\u5355\uff0c\u4f46\u662f\u5fd8\u8bb0\u8003\u8651\u4e86\u4e00\u4e2a\u4e1c\u897f\uff0c\u5373\u4f9d\u8d56\u9879\u6ca1\u6709\u6dfb\u52a0\u5934\u6587\u4ef6\uff0c\u56e0\u4e3a\u5b9e\u9645\u5de5\u7a0b\u4e2d\uff0c\u5934\u6587\u4ef6\u53c8\u4e0d\u662f\u4e0d\u4f1a\u4fee\u6539\u3002<br \/>\nmakefile\u4f1a\u9012\u5f52\u68c0\u67e5\u6240\u6709\u4f9d\u8d56\u7684\u65f6\u95f4\u6233\u3002<\/p>\n<pre><code class=\"language-makefile line-numbers\">objects = main.o io.o calcu.o\nmain: <span class=\"katex math inline\">(objects)\n    gcc<\/span>(objects) -o main\n\n%.o: %.c %.h\n    gcc -c $&lt;\n\nclean:\n    rm main\n    rm *.o\n\n<\/code><\/pre>\n<h4>version 6<\/h4>\n<p>\u5982\u679c\u4f60\u5c1d\u8bd5\u5728\u540c\u6587\u4ef6\u5939\u4e0b\u9762\u6dfb\u52a0\u4e00\u4e2aclean\u6587\u4ef6\uff0c\u4f60\u518d\u6b21\u5c1d\u8bd5\u6e05\u7406<code>make clean<\/code>\u65f6\u4f1a\u53d1\u73b0\u51fa\u73b0<\/p>\n<pre><code class=\"language-bash line-numbers\">make: 'clean' is up to date.\n<\/code><\/pre>\n<p>\u8fd9\u662f\u56e0\u4e3aclean\u6ca1\u6709\u4efb\u4f55\u4f9d\u8d56\uff0c\u800c\u4e14clean\u5df2\u7ecf\u5b58\u5728\uff0c\u6240\u4ee5\u5bfc\u81f4\u7684\u95ee\u9898\u3002<br \/>\n\u5bf9\u6b64\uff0c\u53ef\u4ee5\u4f7f\u7528\u4f2a\u76ee\u6807\u6765\u907f\u514d\u3002<\/p>\n<pre><code class=\"language-makefile line-numbers\">objects = main.o io.o calcu.o\nmain: <span class=\"katex math inline\">(objects)\n    gcc<\/span>(objects) -o main\n\n%.o: %.c %.h\n    gcc -c $&lt;\n\n.PHONY = clean\nclean:\n    rm main\n    rm *.o\n\n<\/code><\/pre>\n<p>\u5176\u4e2d<code>.PHONY : clean<\/code>\u5c31\u662f\u5728\u8bf4\u660etarget clean\u53ea\u662f\u4e00\u4e2a\u4f2a\u76ee\u6807\uff0c\u4e0d\u4f1a\u771f\u7684\u751f\u6210\u4e1c\u897f\u3002<\/p>\n<hr \/>\n<h2>2 \u5c0f\u7ed3<\/h2>\n<p>\u81f3\u6b64\uff0c\u6211\u4eec\u4e00\u4e2a\u5c0f\u578b\u7684makefile\u5df2\u7ecf\u5199\u5b8c\u4e86\u3002<\/p>\n<p>\u4f46\u662f\u5f88\u663e\u7136\uff0c\u5f88\u591amakefile\u7684\u77e5\u8bc6\u70b9\u90fd\u6ca1\u6709\u63d0\u53ca\u3002<br \/>\n\u5982<br \/>\n&#8211; \u66f4\u8be6\u7ec6\u7684<strong>\u901a\u914d\u7b26<\/strong>\u4ecb\u7ecd<br \/>\n&#8211; <strong>\u6761\u4ef6\u8bed\u53e5<\/strong>\u7684\u4ecb\u7ecd<br \/>\n&#8211; <strong>\u51fd\u6570<\/strong>\u7684\u4ecb\u7ecd<\/p>\n<p>\u5269\u4e0b\u7684\u8fd9\u4e9b\uff0c\u53ef\u4ee5\u5728\u5b9e\u9645\u5de5\u7a0b\u4e2d\u8fb9\u5b66\u4fbf\u6478\u7d22\u3002<\/p>\n<blockquote><p>\n  \u4e0d\u8981\u4e00\u5473\u8ffd\u6c42\u5b8c\u7f8e\uff0c\u5c24\u5176\u5728\u9762\u5bf9\u964c\u751f\u9886\u57df\u65f6\u3002<br \/>\n  \u5148\u52a8\u624b\uff0c\u518d\u4f18\u5316\u2014\u2014\u4f60\u8d8a\u65e9\u4f7f\u7528\u5de5\u5177\uff0c\u5c31\u8d8a\u5feb\u638c\u63e1\u5b83\uff1b<br \/>\n  \u8d8a\u5feb\u638c\u63e1\u5b83\uff0c\u5c31\u8d8a\u9ad8\u6548\u5730\u63a8\u8fdb\u9879\u76ee\uff0c\u751a\u81f3\u771f\u6b63\u8e0f\u5165\u8fd9\u7247\u672a\u77e5\u4e4b\u5730\u3002<br \/>\n  \u6240\u4ee5\uff0c<strong>\u5148\u52a8\u624b\u5427<\/strong>\u3002\n<\/p><\/blockquote>\n<h2>3. \u53c2\u8003\u8d44\u6599<\/h2>\n<p>[1] <a class=\"wp-editor-md-post-content-link\" href=\"http:\/\/47.111.11.73\/docs\/boards\/arm-linux\/zdyz-i.mx6ull.html\">\u6b63\u70b9\u539f\u5b50\u8d44\u6599<\/a><br \/>\n[2] <a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/github.com\/seisman\/how-to-write-makefile\">\u300a\u8ddf\u6211\u4e00\u8d77\u5199makefile\u300b<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>makefile\u7b80\u5355\u521d\u63a2\u7d22 \u7cfb\u7edf ubuntu20.04 \u53c2\u8003\u6b63\u70b9\u539f\u5b50 \u53ea\u662f\u5b66\u4e60\u4e2d\u81ea\u5df1\u7684\u5c0f\u8bb0 0.\u5f15\u5b50 \u6211\u4eec\u5e38 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[19],"tags":[],"class_list":["post-490","post","type-post","status-publish","format-standard","hentry","category-embedding_linux_study"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/blog.keruone.cn\/index.php\/wp-json\/wp\/v2\/posts\/490","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.keruone.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.keruone.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.keruone.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.keruone.cn\/index.php\/wp-json\/wp\/v2\/comments?post=490"}],"version-history":[{"count":6,"href":"https:\/\/blog.keruone.cn\/index.php\/wp-json\/wp\/v2\/posts\/490\/revisions"}],"predecessor-version":[{"id":499,"href":"https:\/\/blog.keruone.cn\/index.php\/wp-json\/wp\/v2\/posts\/490\/revisions\/499"}],"wp:attachment":[{"href":"https:\/\/blog.keruone.cn\/index.php\/wp-json\/wp\/v2\/media?parent=490"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.keruone.cn\/index.php\/wp-json\/wp\/v2\/categories?post=490"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.keruone.cn\/index.php\/wp-json\/wp\/v2\/tags?post=490"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}