{"id":4059,"date":"2012-06-10T14:10:36","date_gmt":"2012-06-10T06:10:36","guid":{"rendered":"http:\/\/edsionte.com\/techblog\/?p=4059"},"modified":"2012-06-10T14:11:14","modified_gmt":"2012-06-10T06:11:14","slug":"%e9%80%9a%e8%bf%87make%e7%bc%96%e8%af%91%e5%a4%9a%e6%96%87%e4%bb%b6%e7%9a%84%e5%86%85%e6%a0%b8%e6%a8%a1%e5%9d%97","status":"publish","type":"post","link":"http:\/\/edsionte.com\/techblog\/archives\/4059","title":{"rendered":"\u901a\u8fc7make\u7f16\u8bd1\u591a\u6587\u4ef6\u7684\u5185\u6838\u6a21\u5757"},"content":{"rendered":"<p>\u4e4b\u524d\u5bf9make\u7684\u4f7f\u7528\u90fd\u4ec5\u5c40\u9650\u4e8e\u5355\u4e2a\u6587\u4ef6\uff0c\u6bd4\u5982\u7528make\u7f16\u8bd1\u5355\u4e2a\u6587\u4ef6\u7684\u5185\u6838\u6a21\u5757\u3002\u6211\u8bd5\u7740\u5c06\u4e00\u4e2a\u7a0b\u5e8f\u5206\u89e3\u6210\u591a\u4e2a\u6587\u4ef6\uff0c\u7136\u540e\u518d\u901a\u8fc7make\u8fdb\u884c\u591a\u6587\u4ef6\u7684\u7f16\u8bd1\uff0c\u9014\u4e2d\u8fd8\u662f\u9047\u5230\u4e00\u4e9b\u5217\u95ee\u9898\uff0c\u56e0\u6b64\u603b\u7ed3\u6210\u4e0b\u6587\u3002<\/p>\n<h3>1.\u6587\u4ef6\u6e05\u5355<\/h3>\n<p>\u4e0b\u9762\u7684\u6587\u4ef6\u6e05\u5355\u7c7b\u51fa\u7684\u662f\u4e00\u4e2a\u5185\u6838\u6a21\u5757\u7a0b\u5e8f\uff0c\u6211\u4eec\u8fd9\u91cc\u4e0d\u53bb\u6df1\u7a76\u8fd9\u4e2a\u6a21\u5757\u7684\u529f\u80fd\u3002procfs_mod.c\u4e3b\u8981\u5305\u542b\u5185\u6838\u6a21\u5757\u7a0b\u5e8f\u7684\u4e3b\u8981\u6846\u67b6\uff0c\u6bd4\u5982\u52a0\u8f7d\u51fd\u6570\u548c\u5378\u8f7d\u51fd\u6570\u7b49\uff1bprocfs.c\u5305\u542b\u4e3b\u8981\u7684\u51fd\u6570\u7684\u5b9e\u73b0\uff1b\u800cprocfs.h\u662f\u6574\u4e2a\u7a0b\u5e8f\u6240\u7528\u5230\u7684\u5934\u6587\u4ef6\uff0c\u5305\u542b\u51fd\u6570\u4ee5\u53ca\u53d8\u91cf\u7684\u58f0\u660e\u7b49\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nedsionte@edsionte-desktop:~\/proc_vfs$ ls\r\nMakefile  procfs.c  procfs.h  procfs_mod.c\r\n<\/pre>\n<h3>\n2.Makefile\u7684\u7ec4\u6210<\/h3>\n<p>\u6211\u4eec\u53ef\u4ee5\u5c06Makefile\u770b\u4f5c\u662f\u7f16\u8bd1\u89c4\u5219\u96c6\u5408\uff0c\u5b83\u544a\u8bc9make\u5e94\u8be5\u5982\u4f55\u8fdb\u884c\u7f16\u8bd1\uff0c\u6700\u7ec8\u751f\u6210\u76ee\u6807\u6587\u4ef6\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nedsionte@edsionte-desktop:~\/proc_vfs$ cat Makefile\r\nobj-m += myprocfs.o\r\nmyprocfs-objs := procfs.o procfs_mod.o\r\n\r\nall:\r\n\tmake -C \/lib\/modules\/$(shell uname -r)\/build M=$(shell pwd) modules\r\nclean:\r\n\tmake -C \/lib\/modules\/$(shell uname -r)\/build M=$(shell pwd) clean\r\n<\/pre>\n<p>\u5728\u8fd9\u4e2aMakefile\u4e2d\uff0cobj-m\u8bf4\u660e\u6700\u7ec8\u751f\u6210\u7684\u5185\u6838\u6a21\u5757\u76ee\u6807\u6587\u4ef6\u662fmyprofs.ko\uff0c\u5e76\u4e14\u5b83\u5fc5\u987b\u4f9d\u8d56\u4e24\u4e2a\u6587\u4ef6procfs.c\u548cprocfs_mod.c\u3002\u56e0\u6b64\uff0c\u901a\u8fc7\u6a21\u5757\u540d\u52a0-objs\u7684\u5f62\u5f0f\u53ef\u4ee5\u5b9a\u4e49\u6574\u4e2a\u6a21\u5757\u6240\u5305\u542b\u7684\u6587\u4ef6\u3002\u6ce8\u610f\uff0c\u8fd9\u91cc\u5b9a\u4e49\u7684\u90fd\u662f\u4e0e\u539f\u6587\u4ef6\u540c\u540d\u7684\u76ee\u6807\u6587\u4ef6\u3002<\/p>\n<h3>3.\u5934\u6587\u4ef6\u7684\u5f15\u7528<\/h3>\n<p>\u5728\u672c\u6587\u6240\u4e3e\u4f8b\u7684\u6e90\u7a0b\u5e8f\u4e2d\uff0cprocfs.h\u5305\u542b\u4e86\u6574\u4e2a\u7a0b\u5e8f\u6240\u9700\u8981\u7684\u51fd\u6570\u4ee5\u53ca\u53d8\u91cf\uff0c\u4e5f\u5373\u662f\u8bf4\u6240\u6709.c\u7684\u6e90\u7a0b\u5e8f\u90fd\u4f1a\u5f15\u7528\u8be5\u5934\u6587\u4ef6\u3002\u4e3a\u4e86\u9632\u6b62\u5934\u6587\u4ef6\u88ab\u591a\u6b21\u91cd\u590d\u5f15\u7528\uff0c\u6211\u4eec\u5e94\u8be5\u5728\u5934\u6587\u4ef6\u4e2d\u4f7f\u7528\u6761\u4ef6\u7f16\u8bd1\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nedsionte@edsionte-desktop:~\/proc_vfs$ cat procfs.h\r\n#ifndef _MY_PROC_FS_H\r\n#define _MY_PROC_FS_H\r\n\r\n#include \r\n\u2026\u2026\u2026\u2026\r\n#endif\r\n<\/pre>\n<p>\u4e5f\u5c31\u662f\u5c06\u6574\u4e2a\u5934\u6587\u4ef6\u7684\u5185\u5bb9\u7528\u5934\u6587\u4ef6\u5305\u56f4\u8d77\u6765\u3002\u4e0a\u9762\u7684\u6761\u4ef6\u7f16\u8bd1\u8bf4\u660e\uff1a\u5982\u679c\u6ca1\u6709\u5b9a\u4e49\u7b26\u53f7\u5e38\u91cf_MY_PROC_FS_H\uff0c\u5c31\u5b9a\u4e49\u8be5\u53d8\u91cf\u5e76\u4e14\u5305\u542b\u8be5\u5934\u6587\u4ef6\u3002<\/p>\n<p>\u4f46\u662f\uff0c\u5373\u4fbf\u6211\u5728\u5934\u6587\u4ef6\u4e2d\u4f7f\u7528\u4e86\u6761\u4ef6\u7f16\u8bd1\uff0c\u5728\u7f16\u8bd1\u65f6\u8fd8\u662f\u51fa\u73b0\u4e86\u4e0b\u9762\u7684\u9519\u8bef\uff1a<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nedsionte@edsionte-desktop:~\/proc_vfs$ make\r\nmake -C \/lib\/modules\/2.6.32-24-generic\/build M=\/home\/edsionte\/proc_vfs modules\r\nmake&#x5B;1]: \u6b63\u5728\u8fdb\u5165\u76ee\u5f55 `\/usr\/src\/linux-headers-2.6.32-24-generic'\r\n  CC &#x5B;M]  \/home\/edsionte\/proc_vfs\/procfs.o\r\n  CC &#x5B;M]  \/home\/edsionte\/proc_vfs\/procfs_mod.o\r\n  LD &#x5B;M]  \/home\/edsionte\/proc_vfs\/myprocfs.o\r\n\/home\/edsionte\/proc_vfs\/procfs_mod.o:(.bss+0x820): multiple definition of `my_proc_file'\r\n\/home\/edsionte\/proc_vfs\/procfs.o:(.bss+0x820): first defined here\r\n\/home\/edsionte\/proc_vfs\/procfs_mod.o:(.data+0x80): multiple definition of `my_inode_ops'\r\n\/home\/edsionte\/proc_vfs\/procfs.o:(.data+0x80): first defined here\r\nmake&#x5B;2]: *** &#x5B;\/home\/edsionte\/proc_vfs\/myprocfs.o] \u9519\u8bef 1\r\nmake&#x5B;1]: *** &#x5B;_module_\/home\/edsionte\/proc_vfs] \u9519\u8bef 2\r\nmake&#x5B;1]:\u6b63\u5728\u79bb\u5f00\u76ee\u5f55 `\/usr\/src\/linux-headers-2.6.32-24-generic'\r\nmake: *** &#x5B;all] \u9519\u8bef 2\r\n<\/pre>\n<p>\u4ea7\u751f\u8fd9\u79cd\u9519\u8bef\u7684\u539f\u56e0\u662f\u6211\u4e4b\u524d\u4e00\u76f4\u5728\u5934\u6587\u4ef6\u5bf9\u4e00\u4e9b\u5168\u5c40\u53d8\u91cf\u8fdb\u884c\u5b9a\u4e49\uff0c\u8fd9\u6837\u5bfc\u81f4\u8fdb\u884c\u4e86\u591a\u6b21\u5b9a\u4e49\u3002\u6b63\u786e\u7684\u505a\u6cd5\u662f\u5728\u67d0\u4e00\u4e2a.c\u4e2d\u8fdb\u884c\u5168\u5c40\u53d8\u91cf\u7684\u5b9a\u4e49\uff0c\u800c\u5728.h\u6587\u4ef6\u4e2d\u4f7f\u7528extern\u8fdb\u884c\u5916\u90e8\u5373\u53ef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e4b\u524d\u5bf9make\u7684\u4f7f\u7528\u90fd\u4ec5\u5c40\u9650\u4e8e\u5355\u4e2a\u6587\u4ef6\uff0c\u6bd4\u5982\u7528make\u7f16\u8bd1\u5355\u4e2a\u6587\u4ef6\u7684\u5185\u6838\u6a21\u5757\u3002\u6211\u8bd5\u7740\u5c06\u4e00\u4e2a\u7a0b\u5e8f\u5206\u89e3\u6210\u591a\u4e2a\u6587\u4ef6\uff0c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[205],"tags":[294,343,344],"class_list":["post-4059","post","type-post","status-publish","format-standard","hentry","category-205","tag-make","tag-343","tag-344"],"views":17001,"_links":{"self":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/4059","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/comments?post=4059"}],"version-history":[{"count":0,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/4059\/revisions"}],"wp:attachment":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/media?parent=4059"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/categories?post=4059"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/tags?post=4059"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}