{"id":1350,"date":"2010-09-15T15:03:57","date_gmt":"2010-09-15T07:03:57","guid":{"rendered":"http:\/\/edsionte.com\/techblog\/?p=1350"},"modified":"2010-09-24T13:52:23","modified_gmt":"2010-09-24T05:52:23","slug":"printf%e5%88%b0printk%e7%9a%84%e8%bd%ac%e5%8f%98","status":"publish","type":"post","link":"http:\/\/edsionte.com\/techblog\/archives\/1350","title":{"rendered":"printf\u5230printk\u7684\u8f6c\u53d8"},"content":{"rendered":"<p>\u6628\u5929\u4e0b\u5348stepbystep\u7684\u4e3a\u5176\u4ed6\u540c\u5b66\u6f14\u793a\u4e86\u5185\u6838\u6a21\u5757\u7f16\u7a0bhello,kernel\uff01\u5728\u9648\u8001\u5e08\u7684\u6307\u5bfc\u4e0b\uff0c\u5148\u4e3a\u5927\u5bb6\u6f14\u793a\u4e86\u6700\u57fa\u672c\u7684C\u7a0b\u5e8fhello,world\u3002\u7136\u540e\u53c8\u4e00\u6b65\u6b65\u7684\u8f6c\u6362\u6210\u5185\u6838\u6a21\u5757\u7a0b\u5e8f\u3002\u5728\u8fd9\u4e00\u6b65\u6b65\u7684\u8f6c\u53d8\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4e5f\u53d1\u73b0\u4e86\u81ea\u5df1\u5728\u5b66\u4e60\u5185\u6838\u6a21\u5757\u4e2d\u7684\u4e0d\u8db3\uff0c\u4e0b\u9762\u5c06\u4e0b\u5348\u9047\u5230\u7684\u4e00\u4e9b\u95ee\u9898\u603b\u7ed3\u5982\u4e0b\u3002<\/p>\n<p>\u4e00\u4e2a\u7b80\u5355\u7684hello\uff0cworld\u7a0b\u5e8f\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush:c\">#include &lt;\\stdio.h\\&gt;\r\nint main()\r\n{\r\n\tprintf(\"hello,world\\n\");\r\n\treturn 0;\r\n}<\/pre>\n<p>\u5bf9\u4e8e\u4e0a\u9762\u7684\u7a0b\u5e8f\uff0c\u6211\u4eec\u901a\u5e38\u4e00\u6b65\u5230\u4f4d\u8fdb\u884c\u7f16\u8bd1\uff1a<\/p>\n<pre class=\"brush:c\">gcc hello.c -o hello<\/pre>\n<p>\u8fd9\u6837\u7684\u547d\u4ee4\u7b80\u5355\u65b9\u4fbf\uff0c\u4e0d\u8fc7\u901a\u5e38\u4f1a\u8ba9\u6211\u4eec\u5ffd\u7565\u4ece.c\u6587\u4ef6\u5230\u53ef\u6267\u884c\u6587\u4ef6\u7684\u6574\u4e2a\u7f16\u8bd1\u8fc7\u7a0b\u3002\u901a\u5e38\u5728\u4f7f\u7528gcc\u7f16\u8bd1\u7a0b\u5e8f\u65f6\uff0c\u7f16\u8bd1\u8fc7\u7a0b\u901a\u5e38\u4f1a\u5206\u4e3a4\u4e2a\u9636\u6bb5\uff0c\u5373\u9884\u5904\u7406\uff08pre-processing\uff09\uff0c\u7f16\u8bd1\uff08compiling\uff09\uff0c\u6c47\u7f16\uff08assembling\uff09\uff0c\u94fe\u63a5\uff08linking\uff09\u3002<\/p>\n<p>\u5728\u9884\u5904\u7406\u9636\u6bb5\uff0c\u4e00\u822c\u8f93\u5165\u7684\u662f.c\u6587\u4ef6\uff0c\u800c\u8f93\u51fa\u7684\u662f.i\u6587\u4ef6\u3002\u5728\u6b64\u9636\u6bb5\u4e2d\u901a\u5e38\u4f1a\u5904\u7406\u6e90\u6587\u4ef6\u4e2d\u7684\u9884\u5904\u7406\u547d\u4ee4\uff0c\u6bd4\u5982#define\uff0c#include\uff0c#ifdef\u7b49\u547d\u4ee4\u3002\u5982\u679c\u60f3\u8981\u751f\u6210\u8fd9\u79cd.i\u4e2d\u95f4\u6587\u4ef6\uff0c\u90a3\u4e48\u53ef\u4ee5\u4f7f\u7528\u4e0b\u9762\u547d\u4ee4\uff1a<\/p>\n<pre class=\"brush:c\">gcc -E hello.c -o hello.i<\/pre>\n<p>\u5728\u7f16\u8bd1\u9636\u6bb5\uff0c\u8f93\u5165\u7684\u662f.i\u4e2d\u95f4\u6587\u4ef6\uff0c\u8f93\u51fa\u7684\u662f.s\u6c47\u7f16\u8bed\u8a00\u6587\u4ef6\u3002\u53ef\u4ee5\u4f7f\u7528\u4e0b\u9762\u7684\u547d\u4ee4\uff1a<\/p>\n<pre class=\"brush:c\">gcc -S hello.i -o test.s<\/pre>\n<p>\u5728\u6c47\u7f16\u9636\u6bb5\uff0c\u8f93\u5165\u7684\u662f\u4e0a\u4e00\u6b65\u4ea7\u751f\u7684.s\u6587\u4ef6\uff0c\u4ea7\u751f\u7684\u662f\u4e8c\u8fdb\u5236\u673a\u5668\u4ee3\u7801.o\u6587\u4ef6\u3002\u6b64\u9636\u6bb5\u5bf9\u5e94\u7684\u547d\u4ee4\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush:c\">gcc -c test.s -o test.o<\/pre>\n<p>\u5728\u94fe\u63a5\u9636\u6bb5\uff0c\u8f93\u5165\u7684\u662f.o\u4e8c\u8fdb\u5236\u673a\u5668\u4ee3\u7801\u6587\u4ef6\uff0c\u8fde\u540c\u5176\u4ed6\u7684\uff08\u5982\u679c\u6709\u7684\u8bdd\uff09\u673a\u5668\u4ee3\u7801\u6587\u4ef6\u548c\u5e93\u6587\u4ef6\u4e00\u8d77\u6c47\u96c6\u6210\u4e00\u4e2a\u53ef\u6267\u884c\u7684\u4e8c\u8fdb\u5236\u4ee3\u7801\u6587\u4ef6\u3002<\/p>\n<pre class=\"brush:c\">gcc test.o -o test<\/pre>\n<p>\u4ee5\u4e0a\u662f\u4e00\u4e2a\u7a0b\u5e8f\u7f16\u8bd1\u5b8c\u6574\u7684\u56db\u4e2a\u9636\u6bb5\u3002\u4e00\u822c\u6765\u8bf4\u6211\u4eec\u4f1a\u5c06\u524d\u4e09\u4e2a\u9636\u6bb5\u4e00\u6b65\u641e\u5b9a\uff0c\u90a3\u4e48\u6574\u4e2a\u7f16\u8bd1\u8fc7\u7a0b\u53ef\u4ee5\u7528\u4e0b\u9762\u4e24\u6761\u547d\u4ee4\u5c31\u5b8c\u6210\uff1a<\/p>\n<pre class=\"brush:c\">gcc -c test.c -o test.o\r\ngcc test.o -o test<\/pre>\n<p>\u4e5f\u5c31\u662f\u5148\u751f\u6210\u76ee\u6807\u6587\u4ef6\uff0c\u518d\u5c06\u76ee\u6807\u6587\u4ef6\u8fde\u63a5\u6210\u53ef\u6267\u884c\u6587\u4ef6\u3002\u5f53\u4f60\u719f\u6089\u4e86\u6574\u4e2a\u7f16\u8bd1\u8fc7\u7a0b\u540e\uff0c\u53ef\u4ee5\u7528\u4e00\u5f00\u59cb\u6211\u4eec\u8bf4\u7684\u4e00\u6761\u547d\u4ee4\u6765\u5b8c\u6210\u3002<\/p>\n<p>\u4e86\u89e3\u666e\u901a\u6587\u4ef6\u7684\u7f16\u8bd1\u8fc7\u7a0b\uff0c\u6211\u4eec\u73b0\u5728\u5c31\u5c06hello.c\u4e2d\u7684\u4ee3\u7801\u8f6c\u6362\u6210\u6a21\u5757\u7f16\u7a0b\u4e2d\u7684.c\u4ee3\u7801\u3002\u9996\u5148\u6211\u4eec\u8981\u66f4\u6539\u5934\u6587\u4ef6\uff1a<\/p>\n<pre class=\"brush:c\">#include &lt;\\linux\/kernel.h\\&gt;\r\n#include &lt;\\linux\/module.h\\&gt;<\/pre>\n<p>\u8fd9\u4e0e\u6211\u4eec\u4e00\u822c\u7684\u5934\u6587\u4ef6\u4e0d\u540c\u3002\u4e00\u822c\u6211\u4eec\u5728\u7528\u6237\u6001\u4e0b\u7f16\u5199C\u7a0b\u5e8f\uff0c\u5934\u6587\u4ef6\u4f1a\u653e\u5728\uff1a\/usr\/include\/\u4e0b\uff0c\u800c\u6211\u4eec\u6a21\u5757\u7f16\u7a0b\u65f6\uff0c\u5b83\u4f7f\u7528\u7684\u662f\u5185\u6838\u4e2d\u7684\u5934\u6587\u4ef6\uff0c\u4e00\u822c\u5728\uff1acd \/usr\/src\/linux-headers-2.6.32-21\/include\/\u3002\u7279\u522b\u7684\u6211\u4eec\u8fd9\u91cc\u4f7f\u7528\u7684\u662finclude\/\u76ee\u5f55\u4e0blinux\/\u8fd9\u4e2a\u5b50\u76ee\u5f55\u4e2d\u7684\u5934\u6587\u4ef6\uff0c\u56e0\u6b64\u6a21\u5757\u7f16\u8bd1\u7684\u65f6\u5019\u4f1a\u81ea\u52a8\u5728\u5185\u6838\u4e2d\u7684include\/\u76ee\u5f55\u4e0b\u627elinux\/kernel.h\u8fd9\u6837\u7684\u5934\u6587\u4ef6\u3002<\/p>\n<p>\u5176\u6b21\uff0cprintf\u5230printk\u662f\u4e00\u4e2a\u5178\u578b\u7684\u7528\u6237\u6001\u4e0b\u7f16\u7a0b\u4e0e\u5185\u6838\u6a21\u5757\u7f16\u7a0b\u7684\u4e0d\u540c\u3002\u53ef\u80fd\u6211\u4eec\u4e00\u5f00\u59cb\u4f1a\u6bd4\u8f83\u5947\u602a\uff0c\u4e3a\u4ec0\u4e48\u6211make\u6210\u529f\uff0c\u52a0\u8f7d\u4e5f\u6210\u529f\uff0c\u4f46\u662f\u5c31\u662f\u4e0d\u80fd\u663e\u793aprintk\u91cc\u9762\u7684\u8bed\u53e5\u5462\uff1f\u6211\u4eec\u53ef\u4ee5\u8fd9\u4e48\u60f3printk\u5c31\u662f\u4e13\u95e8\u4e3a\u5185\u6838\u201c\u670d\u52a1\u201d\u3002\u5b83\u4e00\u822c\u8f93\u51fa\u7684\u8bed\u53e5\u90fd\u5728\u5185\u6838\u7684\u65e5\u5fd7\u6587\u4ef6\u5f53\u4e2d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6628\u5929\u4e0b\u5348stepbystep\u7684\u4e3a\u5176\u4ed6\u540c\u5b66\u6f14\u793a\u4e86\u5185\u6838\u6a21\u5757\u7f16\u7a0bhello,kernel\uff01\u5728\u9648\u8001\u5e08\u7684\u6307\u5bfc\u4e0b\uff0c\u5148\u4e3a\u5927\u5bb6 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[110],"tags":[134,147],"class_list":["post-1350","post","type-post","status-publish","format-standard","hentry","category-linux","tag-kernel","tag-147"],"views":36319,"_links":{"self":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/1350","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=1350"}],"version-history":[{"count":0,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/1350\/revisions"}],"wp:attachment":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/media?parent=1350"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/categories?post=1350"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/tags?post=1350"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}