{"id":18,"date":"2006-01-01T21:24:00","date_gmt":"2006-01-01T21:24:00","guid":{"rendered":"http:\/\/marvelliu.wordpress.com\/2006\/01\/01\/compile-linux-module"},"modified":"2006-01-01T21:24:00","modified_gmt":"2006-01-01T21:24:00","slug":"compile-linux-module","status":"publish","type":"post","link":"https:\/\/marvelliu.space\/?p=18","title":{"rendered":"Compile Linux Module"},"content":{"rendered":"<p>\u5728\u5b66\u4e60\u5185\u6838\u7684\u65f6\u5019\u603b\u662f\u6709\u4e00\u79cd\u5728\u5b66\u4e60\u7406\u8bba\u7684\u611f\u89c9\u3002\u5e94\u8be5\u5f88\u591a\u4e1c\u897f\u90fd\u5b9e\u9a8c\u4e0d\u4e86\u3002\u6bd4\u5982\u8bf4\u5185\u6838\u7684\u5bc4\u5b58\u5668\u3001\u5168\u5c40\u53d8\u91cf\u7b49\u7b49\u3002\u50cf\u5185\u6838\u7a7a\u95f4\u7684\u5730\u5740\u5728\u6b63\u5e38\u7684\u7528\u6237\u7a7a\u95f4\u5c31\u6839\u672c\u63a5\u89e6\u4e0d\u5230\u3002<\/p>\n<p>\u4f46\u662f\u5f53\u6211\u770b\u5230\u4e86Linux\u7684\u6a21\u5757\u7684\u65f6\u5019\uff0c\u6211\u53d1\u73b0\u8fd9\u4e2a\u95ee\u9898\u89e3\u51b3\u4e86\uff01\u6211\u4eec\u5b8c\u5168\u53ef\u4ee5\u901a\u8fc7\u63d2\u5165\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u6a21\u5757\u6765\u4e86\u89e3\u5185\u6838\u6001\u7684\u60c5\u51b5\u3002<\/p>\n<p>\u4e8e\u662f\uff0c\u4eca\u5929\u4f9d\u6837\u753b\u846b\u82a6\u5199\u4e86\u4e00\u4e2ahelloworld\u7684\u6a21\u5757\u3002\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<p>hello.c<\/p>\n<p>[coolcode lang=&#8221;cpp&#8221;]<br \/>#include &#8220;linux\/init.h&#8221;<br \/>#include  &#8220;linux\/module.h&#8221;<br \/>MODULE_LICENSE(&#8220;GPL&#8221;);<br \/>static int hello_init(void)<br \/>{<br \/>printk(&#8220;Hello,world!n&#8221;);<br \/>return 0;<br \/>}<br \/>static void hello_exit(void)<br \/>{<br \/>printk(&#8220;Good Bye!Exit!n&#8221;);<br \/>}<br \/>module_init(hello_init);<br \/>module_exit(hello_exit);<br \/>[\/coolcode]<\/p>\n<p>Makefile:<br \/>[coolcode]<br \/>obj-m += hello.o<br \/>default:<br \/>make -C \/usr\/src\/linux SUBDIRS=$(PWD) modules<br \/>clean:<br \/>rm -f .*.cmd *.mod.c *.o *.ko -r .tmp*<br \/>[\/coolcode]<br \/>\u5f88\u5947\u602a\u7684Makefile\uff0c\u4e0d\u662f\u5417\uff1f\u5f53\u7136\uff0c\u8fd9\u4e2a\u662f2.6\u5185\u6838\u4e13\u7528\u7684\u3002<br \/>\u66f4\u5947\u602a\u7684\u662f\uff0c\u6211make\u7684\u65f6\u5019\u51fa\u4e86\u597d\u591a\u9519\uff1a<\/p>\n<p>make -C \/lib\/modules\/2.6.8-2-686-smp\/build\/ SUBDIRS=\/home\/marvel\/programming\/module modules<br \/>make[1]: Entering directory `\/usr\/src\/kernel-source-2.6.8&#8242;<br \/>CC [M]  \/home\/marvel\/programming\/module\/hello.o<br \/>In file included from include\/linux\/init.h:4,<br \/>from \/home\/marvel\/programming\/module\/hello.c:1:<br \/>include\/linux\/config.h:4:28: error: linux\/autoconf.h: \u6ca1\u6709\u90a3\u4e2a\u6587\u4ef6\u6216\u76ee\u5f55<br \/>In file included from include\/linux\/module.h:10,<br \/>from \/home\/marvel\/programming\/module\/hello.c:2:<\/p>\n<p>\u6ca1\u6709\u627e\u5230autoconf.h\uff0c\u8fd9\u4e2a\u53ef\u662f\u6e90\u7801\u5305\uff0c\u600e\u4e48\u4f1a\u5c11\u6587\u4ef6\u5462\uff1f\u90c1\u95f7\u2026\u2026<\/p>\n<p>\u5c1d\u8bd5\u76f4\u63a5gcc -c hello.c\uff0c\u9519\u8bef\u66f4\u591a\uff1a<\/p>\n<p>In file included from \/usr\/include\/linux\/sched.h:12,<br \/>from \/usr\/include\/linux\/module.h:10,<br \/>from hello.c:2:<br \/>\/usr\/include\/linux\/jiffies.h:84: error: syntax error before &#8216;jiffies_64&#8217;<br \/>\/usr\/include\/linux\/jiffies.h:88: error: syntax error before &#8216;get_jiffies_64&#8217;<br \/>\/usr\/include\/linux\/jiffies.h: In function &#8216;timespec_to_jiffies&#8217;:<br \/>\/usr\/include\/linux\/jiffies.h:320: error: called object &#8216;u64&#8217; is not a function<br \/>\/usr\/include\/linux\/jiffies.h:320: error: called object &#8216;u64&#8217; is not a function<br \/>\/usr\/include\/linux\/jiffies.h:320: error: &#8216;NSEC_PER_SEC&#8217; undeclared (first use in this function)<br \/>\/usr\/include\/linux\/jiffies.h:320: error: (Each undeclared identifier is reported only once<br \/>\/usr\/include\/linux\/jiffies.h:320: error: for each function it appears in.)<br \/>\/usr\/include\/linux\/jiffies.h:321: error: called object &#8216;u64&#8217; is not a function<br \/>\/usr\/include\/linux\/jiffies.h:321: error: called object &#8216;u64&#8217; is not a function<\/p>\n<p>Linuxsir\u4e0a\u5bfb\u627e\u5e2e\u52a9\u65e0\u679c\uff0c\u53ea\u80fd\u81ea\u5df1\u52a8\u624b\u3002\u5148\u4ece\/usr\/include\/linux\/\u4e0b\u590d\u5236\u4e00\u4e2aautoconf.h\u6587\u4ef6\u8fc7\u6765\uff0c\u7136\u540e\u5c06asm-i386\u5efa\u7acb\u4e00\u4e2aasm\u7684\u94fe\u63a5\uff08\u5176\u5b9e\u90a3\u4e00\u5806\u9519\u8bef\u8fd8\u6709\u6ca1\u6709\u627e\u5230asm\/\u4e0b\u7684\u6587\u4ef6\uff09\u3002\u8fd9\u6837\u4f3c\u4e4e\u5927\u4f53\u53ef\u4ee5\u4e86\uff0c\u4f46\u662f\u6700\u540e\u8bf4\u4ec0\u4e48\u7f3a\u5c11script\/basic\/\u4e0b\u7684\u4e00\u4e2a\u53ef\u6267\u884c\u6587\u4ef6\u3002\u5f88\u660e\u663e\u4e86\uff0c\u8fd9\u4e2a\u6587\u4ef6\u9700\u8981\u5728\u7f16\u8bd1\u5185\u6838\u7684\u65f6\u5019\u751f\u6210\u3002<\/p>\n<p>\u4e0d\u7ba1\u600e\u4e48\u6837\uff0c\u5148\u91cd\u65b0\u7f16\u8bd1\u4e00\u4e2a\u5185\u6838\u8bd5\u4e00\u8bd5\u3002\u6ca1\u60f3\u5230\u7f16\u8bd1\u5230\u4e00\u534a\uff0cmake\u5c45\u7136\u6210\u529f\u4e86\u3002\u3000:)<\/p>\n<p>\u540e\u6765\u5206\u6790\u4e86\u4e00\u4e0b \uff0c\u6211\u8ba4\u4e3a\u539f\u56e0\u662f\u8fd9\u6837\u7684\u3002#include\u7684init.h\u6587\u4ef6\u5c31\u5728\/usr\/src\/linux\/include\/linux\u76ee\u5f55\u4e0b\uff0c\u4f46\u662f\u8fd9\u4e2a\u76ee\u5f55\u5f00\u59cb\u7684\u65f6\u5019\u6ca1\u6709autoconf.h\u8fd9\u4e2a\u6587\u4ef6\uff0c\u5b83\u5e94\u8be5\u662f\u5728\u7f16\u8bd1\u4e4b\u524d\u81ea\u52a8\u751f\u6210\u7684\uff0c\u800casm\u4e5f\u662f\u81ea\u52a8\u5efa\u7acb\u7684\u3002\u81f3\u4e8e\u90a3\u4e2a\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u5f53\u7136\u662f\u7f16\u8bd1\u4e2d\u95f4\u751f\u6210\u7684\u5566\u3002<\/p>\n<p>\u4e8b\u5b9e\u4e0a\uff0c\u8fd9\u4e9b\u6587\u4ef6\u662f\u5728make\u00a0 [menu|x]config \u7684\u65f6\u5019\u751f\u6210\u7684\u3002\u7406\u8bba\u4e0a\u5185\u68382.6\u6709\u7ed3\u6784\u6811\uff0c\u6240\u4ee5\u4e0d\u9700\u8981\u7f16\u8bd1\u6574\u4e2a\u5185\u6838\uff0c\u5f53\u7136\u4f60\u53ef\u4ee5\u8bd5\u4e00\u8bd5\u7f16\u8bd1\u5185\u6838\uff0c\u4e4b\u540emake-kpkg clean\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u5b66\u4e60\u5185\u6838\u7684\u65f6\u5019\u603b\u662f\u6709\u4e00\u79cd\u5728\u5b66\u4e60\u7406\u8bba\u7684\u611f\u89c9\u3002\u5e94\u8be5\u5f88\u591a\u4e1c\u897f\u90fd\u5b9e\u9a8c\u4e0d\u4e86\u3002\u6bd4\u5982\u8bf4\u5185\u6838\u7684\u5bc4\u5b58\u5668\u3001\u5168\u5c40\u53d8\u91cf\u7b49\u7b49\u3002\u50cf\u5185\u6838\u7a7a\u95f4\u7684\u5730\u5740\u5728\u6b63\u5e38\u7684\u7528\u6237\u7a7a\u95f4\u5c31\u6839\u672c\u63a5\u89e6\u4e0d\u5230\u3002 \u4f46\u662f\u5f53\u6211\u770b\u5230\u4e86Linux\u7684\u6a21\u5757\u7684\u65f6\u5019\uff0c\u6211\u53d1\u73b0\u8fd9\u4e2a\u95ee\u9898\u89e3\u51b3\u4e86\uff01\u6211\u4eec\u5b8c\u5168\u53ef\u4ee5\u901a\u8fc7\u63d2\u5165\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u6a21\u5757\u6765\u4e86\u89e3\u5185\u6838\u6001\u7684\u60c5\u51b5\u3002 \u4e8e\u662f\uff0c\u4eca\u5929\u4f9d\u6837\u753b\u846b\u82a6\u5199\u4e86\u4e00\u4e2ahelloworld\u7684\u6a21\u5757\u3002\u4ee3\u7801\u5982\u4e0b\uff1a hello.c [coolcode lang=&#8221;cpp&#8221;]#include &#8220;linux\/init.h&#8221;#include &#8220;linux\/module.h&#8221;MODULE_LICENSE(&#8220;GPL&#8221;);static int hello_init(void){printk(&#8220;Hello,world!n&#8221;);return 0;}static void hello_exit(void){printk(&#8220;Good Bye!Exit!n&#8221;);}module_init(hello_init);module_exit(hello_exit);[\/coolcode] Makefile:[coolcode]obj-m += hello.odefault:make -C \/usr\/src\/linux SUBDIRS=$(PWD) modulesclean:rm -f .*.cmd *.mod.c *.o *.ko -r .tmp*[\/coolcode]\u5f88\u5947\u602a\u7684Makefile\uff0c\u4e0d\u662f\u5417\uff1f\u5f53\u7136\uff0c\u8fd9\u4e2a\u662f2.6\u5185\u6838\u4e13\u7528\u7684\u3002\u66f4\u5947\u602a\u7684\u662f\uff0c\u6211make\u7684\u65f6\u5019\u51fa\u4e86\u597d\u591a\u9519\uff1a make -C \/lib\/modules\/2.6.8-2-686-smp\/build\/ SUBDIRS=\/home\/marvel\/programming\/module modulesmake[1]: Entering directory `\/usr\/src\/kernel-source-2.6.8&#8242;CC [M] \/home\/marvel\/programming\/module\/hello.oIn file included from include\/linux\/init.h:4,from \/home\/marvel\/programming\/module\/hello.c:1:include\/linux\/config.h:4:28: error: linux\/autoconf.h: \u6ca1\u6709\u90a3\u4e2a\u6587\u4ef6\u6216\u76ee\u5f55In file included from include\/linux\/module.h:10,from \/home\/marvel\/programming\/module\/hello.c:2: \u6ca1\u6709\u627e\u5230autoconf.h\uff0c\u8fd9\u4e2a\u53ef\u662f\u6e90\u7801\u5305\uff0c\u600e\u4e48\u4f1a\u5c11\u6587\u4ef6\u5462\uff1f\u90c1\u95f7\u2026\u2026 \u5c1d\u8bd5\u76f4\u63a5gcc -c hello.c\uff0c\u9519\u8bef\u66f4\u591a\uff1a &hellip; <a href=\"https:\/\/marvelliu.space\/?p=18\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Compile Linux Module&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21],"tags":[],"class_list":["post-18","post","type-post","status-publish","format-standard","hentry","category-tech"],"_links":{"self":[{"href":"https:\/\/marvelliu.space\/index.php?rest_route=\/wp\/v2\/posts\/18","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/marvelliu.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/marvelliu.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/marvelliu.space\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/marvelliu.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=18"}],"version-history":[{"count":0,"href":"https:\/\/marvelliu.space\/index.php?rest_route=\/wp\/v2\/posts\/18\/revisions"}],"wp:attachment":[{"href":"https:\/\/marvelliu.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=18"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/marvelliu.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=18"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/marvelliu.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=18"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}