{"id":526,"date":"2019-09-23T11:40:37","date_gmt":"2019-09-23T03:40:37","guid":{"rendered":"http:\/\/iqotom.com\/?p=526"},"modified":"2019-10-17T14:24:18","modified_gmt":"2019-10-17T06:24:18","slug":"linux%e9%a9%b1%e5%8a%a8-%e7%bc%96%e8%af%91%e4%b8%80%e4%b8%aahello%e6%a8%a1%e5%9d%97","status":"publish","type":"post","link":"http:\/\/iqotom.com\/?p=526","title":{"rendered":"Linux\u9a71\u52a8 \u7f16\u8bd1\u4e00\u4e2ahello\u6a21\u5757"},"content":{"rendered":"\n<p><strong>\u8bb0\u5f55\u4e00\u70b9linux\u9a71\u52a8\u5b66\u4e60\u7b14\u8bb0\uff0c\u65b9\u4fbf\u4ee5\u540e\u67e5\u9605<\/strong><\/p>\n\n\n\n<p>\u300a<a href=\"http:\/\/iqotom.com\/?p=526\">Linux\u9a71\u52a8 \u7f16\u8bd1\u4e00\u4e2ahello\u6a21\u5757<\/a>\u300b<br>\u300a<a href=\"http:\/\/iqotom.com\/?p=539\">Linux\u9a71\u52a8 \u5b9e\u73b0\u4e00\u4e2a\u5b57\u7b26\u8bbe\u5907\u9a71\u52a8<\/a>\u300b<br>\u300a<a href=\"http:\/\/iqotom.com\/?p=552\">Liunx\u9a71\u52a8 \u5b9e\u73b0\u4e00\u4e2amisc\u8bbe\u5907\u9a71\u52a8<\/a>\u300b<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u7f16\u5199hello world\u6a21\u5757\u4ee3\u7801<\/h3>\n\n\n\n<p> \u5728\u5185\u6838\u6e90\u7801driver\u76ee\u5f55\u521b\u4e00\u4e2a<code>hello_driver<\/code>\u7684\u76ee\u5f55\uff0c\u5e76\u5206\u522b\u521b\u5efa<em>hello_kernel.c\u3001Kconfig\u3001Makefile<\/em>\u4e09\u4e2a\u6587\u4ef6 <\/p>\n\n\n\n<p><em>hello_kernel.c<\/em>\u4ee3\u7801\u5982\u4e0b\uff1a <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include&lt;linux\/kernel.h>\n#include&lt;linux\/module.h>\n\nstatic int __init hello_init(void)\n{\n    printk(\"Hello, kernel instaled!\\n\");\n    return 0;\n}\n\nstatic void __exit hello_cleanup(void)\n{\n    printk(\"Good bye, removed!\\n\");\n}\n\nmodule_init(hello_init);\nmodule_exit(hello_cleanup);\nMODULE_LICENSE(\"GPL\");<\/code><\/pre>\n\n\n\n<p> \u8fd9\u91cc\u4f7f\u7528\u5185\u6838\u6839\u76ee\u5f55\u4e0b\u7684Makefile\u8fdb\u884c\u7f16\u8bd1\u3002<br> <em>Makefile<\/em>\u4ee3\u7801\u5982\u4e0b\uff1a <br><code>obj-$(CONFIG_HELLO_KERNEL) += hello_kernel.o<\/code><\/p>\n\n\n\n<p><em>Kconfig<\/em>\u4ee3\u7801\u5982\u4e0b\uff1a <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>menu \"hello_driver\"\n    config HELLO_KERNEL\n        tristate \"hello_kernel\"\n        default n\n    help\n        if you select, you can use it\n\nendmenu<\/code><\/pre>\n\n\n\n<ul><li> menu\uff1a\u914d\u7f6e\u9009\u9879\u7684\u83dc\u5355 <\/li><li> config\uff1a\u8981\u914d\u7f6e\u7684\u53c2\u6570 <\/li><li> tristate\uff1a\u8868\u793a\u6709\u4e09\u79cd\u72b6\u6001\u53ef\u914d\u7f6e\uff0cM\u4ee5\u6a21\u5757\u7f16\u8bd1\uff0c\u00d7\u7f16\u8bd1\u6210.o <\/li><li> default\uff1a\u9ed8\u8ba4\u914d\u7f6e\uff0c\u6709y\uff0cn\uff0c\u8fd8\u53ef\u4ee5\u5199\u6a21\u5757\uff08y if xxmod\uff09 <\/li><li> help\uff1a\u63d0\u793a\u4fe1\u606f\uff0c\u53ef\u4ee5\u81ea\u7531\u6dfb\u52a0 <\/li><li> Kconfig\u914d\u7f6e\u6709\u5f88\u591a\uff0c\u53ef\u4ee5copy\u5176\u4ed6\u914d\u7f6e\u591a\u5c1d\u8bd5\uff0c\u7136\u540emake menuconfig\u770b\u6548\u679c <\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u4fee\u6539\u5185\u6838\u914d\u7f6e\u6587\u4ef6<\/h3>\n\n\n\n<p> 1.\u6253\u5f00\u5185\u6838\u6e90\u7801drivers\u76ee\u5f55\u7684Kconfig\uff0c\u5728endmenu\u4e0a\u9762\u6dfb\u52a0<code>source \"drivers\/hello_driver\/Kconfig\"<\/code> <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"652\" height=\"232\" src=\"http:\/\/iqotom.com\/wp-content\/uploads\/2019\/09\/hello-02.png\" alt=\"\" class=\"wp-image-527\" srcset=\"http:\/\/iqotom.com\/wp-content\/uploads\/2019\/09\/hello-02.png 652w, http:\/\/iqotom.com\/wp-content\/uploads\/2019\/09\/hello-02-300x107.png 300w\" sizes=\"(max-width: 652px) 100vw, 652px\" \/><\/figure><\/div>\n\n\n\n<p>2. \u5728\u5185\u6838\u6e90\u7801\u6839\u76ee\u5f55\u6267\u884c<code>make menuconfig<\/code>\u8fdb\u5165<strong>Device Drivers<\/strong>\uff0c\u79fb\u52a8\u5230\u6700\u4e0b\u9762\u53ef\u4ee5\u770b\u5230\u6dfb\u52a0\u7684\u9009\u9879 <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"909\" height=\"228\" src=\"http:\/\/iqotom.com\/wp-content\/uploads\/2019\/09\/hello-01.png\" alt=\"\" class=\"wp-image-534\" srcset=\"http:\/\/iqotom.com\/wp-content\/uploads\/2019\/09\/hello-01.png 909w, http:\/\/iqotom.com\/wp-content\/uploads\/2019\/09\/hello-01-300x75.png 300w, http:\/\/iqotom.com\/wp-content\/uploads\/2019\/09\/hello-01-768x193.png 768w\" sizes=\"(max-width: 909px) 100vw, 909px\" \/><\/figure><\/div>\n\n\n\n<p>3. \u6309\u56de\u8f66\u8fdb\u5165\uff0c\u7a7a\u683c\u952e\u8fdb\u884c\u914d\u7f6e <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"905\" height=\"440\" src=\"http:\/\/iqotom.com\/wp-content\/uploads\/2019\/09\/hello-04.png\" alt=\"\" class=\"wp-image-529\" srcset=\"http:\/\/iqotom.com\/wp-content\/uploads\/2019\/09\/hello-04.png 905w, http:\/\/iqotom.com\/wp-content\/uploads\/2019\/09\/hello-04-300x146.png 300w, http:\/\/iqotom.com\/wp-content\/uploads\/2019\/09\/hello-04-768x373.png 768w\" sizes=\"(max-width: 905px) 100vw, 905px\" \/><\/figure><\/div>\n\n\n\n<p>4. \u7f16\u8bd1\u524d\u8fd8\u9700\u8981\u4fee\u6539\u5185\u6838\u9a71\u52a8\u76ee\u5f55Makefile\u6587\u4ef6 <br> \u56e0\u4e3a\u662f\u4f7f\u7528\u5185\u6838\u6e90\u7801\u8fdb\u884c\u7f16\u8bd1\uff0c\u6240\u4ee5\u9700\u8981\u5c06\u65b0\u589e\u7684<code>hello_driver<\/code>\u76ee\u5f55\u6dfb\u52a0\u5230\u5185\u6838\u6e90\u7801drivers\u7684Makefile\u91cc <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"814\" height=\"172\" src=\"http:\/\/iqotom.com\/wp-content\/uploads\/2019\/09\/hello-05.png\" alt=\"\" class=\"wp-image-530\" srcset=\"http:\/\/iqotom.com\/wp-content\/uploads\/2019\/09\/hello-05.png 814w, http:\/\/iqotom.com\/wp-content\/uploads\/2019\/09\/hello-05-300x63.png 300w, http:\/\/iqotom.com\/wp-content\/uploads\/2019\/09\/hello-05-768x162.png 768w\" sizes=\"(max-width: 814px) 100vw, 814px\" \/><\/figure><\/div>\n\n\n\n<p>5. \u7f16\u8bd1\u5185\u6838\u4ee3\u7801 <br> \u5185\u6838\u8ddf\u76ee\u5f55\u4e0b\u6267\u884cmake -j4\uff0c\u751f\u6210\u9a71\u52a8\u6a21\u5757 <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"621\" height=\"214\" src=\"http:\/\/iqotom.com\/wp-content\/uploads\/2019\/09\/hello-06.png\" alt=\"\" class=\"wp-image-531\" srcset=\"http:\/\/iqotom.com\/wp-content\/uploads\/2019\/09\/hello-06.png 621w, http:\/\/iqotom.com\/wp-content\/uploads\/2019\/09\/hello-06-300x103.png 300w\" sizes=\"(max-width: 621px) 100vw, 621px\" \/><\/figure><\/div>\n\n\n\n<pre class=\"wp-block-code\"><code>root@linux:\/zdisk\/linux-5.0.6\/drivers\/hello_driver# ls\nhello_kernel.c   hello_kernel.mod.c  hello_kernel.o  Makefile\nhello_kernel.ko  hello_kernel.mod.o  Kconfig         modules.order<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u9a8c\u8bc1\u7ed3\u679c<\/h3>\n\n\n\n<p> \u4e3a\u4e86\u65b9\u4fbf\uff0c\u8fd9\u91cc\u624b\u52a8\u52a0\u8f7d\/\u5378\u8f7d\u9a71\u52a8\u6587\u4ef6\uff0c<code>insmod hello_kernel.ko\/rmmod hello_kernel.ko<\/code><br> \u8f93\u5165dmesg\uff0c\u67e5\u770b\u65e5\u5fd7\uff0c\u6709\u6253\u5370hello_driver.c\u4e2d\u7684\u5b57\u7b26\u4e32<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"685\" height=\"129\" src=\"http:\/\/iqotom.com\/wp-content\/uploads\/2019\/09\/hello-07.png\" alt=\"\" class=\"wp-image-532\" srcset=\"http:\/\/iqotom.com\/wp-content\/uploads\/2019\/09\/hello-07.png 685w, http:\/\/iqotom.com\/wp-content\/uploads\/2019\/09\/hello-07-300x56.png 300w\" sizes=\"(max-width: 685px) 100vw, 685px\" \/><\/figure><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u8bb0\u5f55\u4e00\u70b9linux\u9a71\u52a8\u5b66\u4e60\u7b14\u8bb0\uff0c\u65b9\u4fbf\u4ee5\u540e\u67e5\u9605 \u300aLinux\u9a71\u52a8 \u7f16\u8bd1\u4e00\u4e2ahello\u6a21\u5757\u300b\u300aLinux\u9a71\u52a8 \u5b9e\u73b0\u2026 <span class=\"read-more\"><a href=\"http:\/\/iqotom.com\/?p=526\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":528,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48,5,49],"tags":[7,51,50],"_links":{"self":[{"href":"http:\/\/iqotom.com\/index.php?rest_route=\/wp\/v2\/posts\/526"}],"collection":[{"href":"http:\/\/iqotom.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/iqotom.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/iqotom.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/iqotom.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=526"}],"version-history":[{"count":3,"href":"http:\/\/iqotom.com\/index.php?rest_route=\/wp\/v2\/posts\/526\/revisions"}],"predecessor-version":[{"id":901,"href":"http:\/\/iqotom.com\/index.php?rest_route=\/wp\/v2\/posts\/526\/revisions\/901"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/iqotom.com\/index.php?rest_route=\/wp\/v2\/media\/528"}],"wp:attachment":[{"href":"http:\/\/iqotom.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=526"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/iqotom.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=526"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/iqotom.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=526"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}