





{"id":1703,"date":"2011-01-25T01:56:19","date_gmt":"2011-01-25T05:56:19","guid":{"rendered":"http:\/\/www.killtest.hk\/?p=1703"},"modified":"2011-01-25T01:56:19","modified_gmt":"2011-01-25T05:56:19","slug":"linux-p","status":"publish","type":"post","link":"https:\/\/www.killtest.hk\/index.php\/unix\/linux-p\/","title":{"rendered":"Linux\u4e2d\u5982\u4f55\u5275\u5efa\u975c\u614b\u5eab\u548c\u52d5\u614b\u5eab"},"content":{"rendered":"<p>\u975c\u614b\u5eab\u5728\u7a0b\u5e8f\u7de8\u8b6f\u6642\u6703\u88ab\u9023\u63a5\u5230\u76ee\u6a19\u4ee3\u78bc\u4e2d\uff0c\u7a0b\u5e8f\u904b\u884c\u6642\u5c07\u4e0d\u518d\u9700\u8981\u8a72\u975c\u614b\u5eab\u3002<\/p>\n<p>\u3000\u3000\u52d5\u614b\u5eab\u5728\u7a0b\u5e8f\u7de8\u8b6f\u6642\u4e26\u4e0d\u6703\u88ab\u9023\u63a5\u5230\u76ee\u6a19\u4ee3\u78bc\u4e2d\uff0c\u800c\u662f\u5728\u7a0b\u5e8f\u904b\u884c\u662f\u624d\u88ab\u8f09\u5165\uff0c\u56e0\u6b64\u5728\u7a0b\u5e8f\u904b\u884c\u6642\u9084\u9700\u8981\u52d5\u614b\u5eab\u5b58\u5728\u3002<\/p>\n<p>\u3000\u3000\u7a0b\u5e8f1: hello.h<\/p>\n<p>\u3000\u3000#ifndef HELLO_H<\/p>\n<p>\u3000\u3000#define HELLO_H<\/p>\n<p>\u3000\u3000void hello(const char *name);<\/p>\n<p>\u3000\u3000#endif \/\/HELLO_H<\/p>\n<p>\u3000\u3000\u7a0b\u5e8f2: hello.c<\/p>\n<p>\u3000\u3000#include &lt;stdio.h&gt;<\/p>\n<p>\u3000\u3000void hello(const char *name)<\/p>\n<p>\u3000\u3000{<\/p>\n<p>\u3000\u3000printf(&#8220;Hello %s!\\n&#8221;, name);<\/p>\n<p>\u3000\u3000}<\/p>\n<p>\u3000\u3000\u7a0b\u5e8f3: main.c<\/p>\n<p>\u3000\u3000#include &#8220;hello.h&#8221;<\/p>\n<p>\u3000\u3000int main()<\/p>\n<p>\u3000\u3000{<\/p>\n<p>\u3000\u3000hello(&#8220;everyone&#8221;);<\/p>\n<p>\u3000\u3000return 0;<\/p>\n<p>\u3000\u3000}<\/p>\n<p>\u3000\u3000\u7121\u8ad6\u52d5\u614b\u5eab\u9084\u662f\u975c\u614b\u5eab\u90fd\u9700\u8981\u7528\u5230.o\u6587\u4ef6\u4f86\u751f\u6210\uff0c\u5148\u7de8\u8b6f\u751f\u6210.o\u6587\u4ef6\u3002<\/p>\n<p>\u3000\u3000# gcc -c hello.c<\/p>\n<p>\u3000\u30001\uff1a\u5275\u5efa\u975c\u614b\u5eab<\/p>\n<p>\u3000\u3000\u975c\u614b\u5eab\u6587\u4ef6\u540d\u7684\u547d\u540d\u898f\u7bc4\u662f\u4ee5lib\u70ba\u524d\u7db4\uff0c\u7dca\u63a5\u8457\u8ddf\u975c\u614b\u5eab\u540d\uff0c\u64f4\u5c55\u540d\u70ba.a\u3002\u4f8b\u5982\uff1a\u6211\u5011\u5c07\u5275\u5efa\u7684\u975c\u614b\u5eab\u540d\u70bamyhello\uff0c\u5247\u975c\u614b\u5eab\u6587\u4ef6\u540d\u5c31\u662flibmyhello.a\u3002<\/p>\n<p>\u3000\u3000# ar cr libmyhello.a hello.o<\/p>\n<p>\u3000\u3000\u4f7f\u7528\u975c\u614b\u5eab\uff1a\u53ea\u9700\u8981\u5728\u59b3\u7684\u6e90\u7a0b\u5e8f\u4e2d\u52a0\u5165\u5305\u542b\u59b3\u6240\u9700\u8981\u4f7f\u7528\u5230\u7684\u51fd\u6578\u7684\u8072\u660e\uff08\u5373\u5305\u542b\u982d\u6587\u4ef6\uff09\uff0c\u7136\u5f8c\u5728gcc\u751f\u6210\u76ee\u6a19\u6587\u4ef6\u6642\u5019\u6307\u660e\u975c\u614b\u5eab\u5c31OK\u4e86\uff08\u9664\u975e\u59b3\u5305\u542b\u7684\u982d\u6587\u4ef6\u5728\/usr\/include\uff0c\u5eab\u6587\u4ef6\u5728\u6a19\u6e96\u5eab\/usr\/lib,\/lib\u4e0b\uff0c\u5426\u5247\u59b3\u5f97\u986f\u793a\u6307\u660e\u4ed6\u5011\u7684\u8def\u5f91\uff09<\/p>\n<p>\u3000\u3000# gcc -o hello main.c -L. -lmyhello<\/p>\n<p>\u3000\u3000# .\/hello<\/p>\n<p>\u3000\u3000Hello everyone!<\/p>\n<p>\u3000\u3000\u522a\u9664\u975c\u614b\u5eab\u6587\u4ef6\u904b\u884c.\/hello\uff0c\u7a0b\u5e8f\u6b63\u5e38\u904b\u884c\uff0c\u8aaa\u660e\u975c\u614b\u5eab\u516c\u7528\u51fd\u6578\u5df2\u7d93\u93c8\u63a5\u5230\u76ee\u6a19\u6587\u4ef6\u3002<\/p>\n<p>\u3000\u30002\uff1a \u5229\u7528.o\u6587\u4ef6\u5275\u5efa\u52d5\u614b\u5eab<\/p>\n<p>\u3000\u3000\u52d5\u614b\u5eab\u6587\u4ef6\u64f4\u5c55\u540d\u70ba.so\u3002<\/p>\n<p>\u3000\u3000# gcc -shared -fPCI -o libmyhello.so hello.o<\/p>\n<p>\u3000\u3000\u52d5\u614b\u5eab\u7684\u4f7f\u7528\u8207\u975c\u614b\u5eab\u4f7f\u7528\u65b9\u5f0f\u58f9\u6a23<\/p>\n<p>\u3000\u3000# gcc -o hello main.c -L. -lmyhello<\/p>\n<p>\u3000\u3000# .\/hello<\/p>\n<p>\u3000\u3000.\/hello: error while loading shared libraries: libmyhello.so: cannot open shared object file: No such file or directory<\/p>\n<p>\u3000\u3000\u54e6\uff01\u51fa\u932f\u4e86\u3002\u5feb\u770b\u770b\u932f\u8aa4\u63d0\u793a\uff0c\u539f\u4f86\u662f\u627e\u4e0d\u5230\u52d5\u614b\u5eab\u6587\u4ef6libmyhello.so\u3002\u7a0b\u5e8f\u5728\u904b\u884c\u6642\uff0c\u6703\u5728\/usr\/lib\u548c\/lib\u7b49\u76ee\u9304\u4e2d\u67e5\u627e\u9700\u8981\u7684\u52d5\u614b\u5eab\u6587\u4ef6\u3002\u82e5\u627e\u5230\uff0c\u5247\u8f09\u5165\u52d5\u614b\u5eab\uff0c\u5426\u5247\u5c07\u63d0\u793a\u985e\u4f3c\u4e0a\u8ff0\u932f\u8aa4\u800c\u7d42\u6b62\u7a0b\u5e8f\u904b\u884c\u3002<\/p>\n<p>\u3000\u3000\u5982\u4f55\u627e\u5230\u751f\u6210\u7684\u52d5\u614b\u5eab\u67093\u7a2e\u65b9\u5f0f\uff1a<\/p>\n<p>\u3000\u30001)\u628a\u5eab\u62f7\u8c9d\u5230\/usr\/lib\u548c\/lib\u76ee\u9304\u4e0b\u3002<\/p>\n<p>\u3000\u3000(2)\u5728LD_LIBRARY_PATH\u74b0\u5883\u8b8a\u91cf\u4e2d\u52a0\u4e0a\u5eab\u6240\u5728\u8def\u5f91\u3002<\/p>\n<p>\u3000\u3000\u4f8b\u5982\u52d5\u614b\u5eablibhello.so\u5728\/home\/example\/lib\u76ee\u9304\u4e0b\uff1a<\/p>\n<p>\u3000\u3000$export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:\/home\/example\/lib<\/p>\n<p>\u3000\u3000(3) \u4fee\u6539\/etc\/ld.so.conf\u6587\u4ef6\uff0c\u628a\u5eab\u6240\u5728\u7684\u8def\u5f91\u52a0\u5230\u6587\u4ef6\u672b\u5c3e\uff0c\u4e26\u57f7\u884cldconfig\u5237\u65b0\u3002\u9019\u6a23\uff0c\u52a0\u5165\u7684\u76ee\u9304\u4e0b\u7684\u6240\u6709\u5eab\u6587\u4ef6\u90fd\u53ef\u898b\u3002<\/p>\n<p>\u3000\u3000\u7576\u975c\u614b\u5eab\u548c\u52d5\u614b\u5eab\u540c\u540d\u6642\uff0c gcc\u547d\u4ee4\u5c07\u512a\u5148\u4f7f\u7528\u52d5\u614b\u5eab\u3002<\/p>\n<p>\u3000\u3000\u51fd\u6578\u5eab\u5206\u70ba\u975c\u614b\u5eab\u548c\u52d5\u614b\u5eab\u5169\u7a2e\u3002<\/p>\n<p>\u3000\u3000\u975c\u614b\u5eab\u5728\u7a0b\u5e8f\u7de8\u8b6f\u6642\u6703\u88ab\u9023\u63a5\u5230\u76ee\u6a19\u4ee3\u78bc\u4e2d\uff0c\u7a0b\u5e8f\u904b\u884c\u6642\u5c07\u4e0d\u518d\u9700\u8981\u8a72\u975c\u614b\u5eab\u3002<\/p>\n<p>\u3000\u3000\u52d5\u614b\u5eab\u5728\u7a0b\u5e8f\u7de8\u8b6f\u6642\u4e26\u4e0d\u6703\u88ab\u9023\u63a5\u5230\u76ee\u6a19\u4ee3\u78bc\u4e2d\uff0c\u800c\u662f\u5728\u7a0b\u5e8f\u904b\u884c\u662f\u624d\u88ab\u8f09\u5165\uff0c\u56e0\u6b64\u5728\u7a0b\u5e8f\u904b\u884c\u6642\u9084\u9700\u8981\u52d5\u614b\u5eab\u5b58\u5728\u3002<\/p>\n<p>\u3000\u3000\u7a0b\u5e8f1: hello.h<\/p>\n<p>\u3000\u3000#ifndef HELLO_H<\/p>\n<p>\u3000\u3000#define HELLO_H<\/p>\n<p>\u3000\u3000void hello(const char *name);<\/p>\n<p>\u3000\u3000#endif \/\/HELLO_H<\/p>\n<p>\u3000\u3000\u7a0b\u5e8f2: hello.c<\/p>\n<p>\u3000\u3000#include &lt;stdio.h&gt;<\/p>\n<p>\u3000\u3000void hello(const char *name)<\/p>\n<p>\u3000\u3000{<\/p>\n<p>\u3000\u3000printf(&#8220;Hello %s!\\n&#8221;, name);<\/p>\n<p>\u3000\u3000}<\/p>\n<p>\u3000\u3000\u7a0b\u5e8f3: main.c<\/p>\n<p>\u3000\u3000#include &#8220;hello.h&#8221;<\/p>\n<p>\u3000\u3000int main()<\/p>\n<p>\u3000\u3000{<\/p>\n<p>\u3000\u3000hello(&#8220;everyone&#8221;);<\/p>\n<p>\u3000\u3000return 0;<\/p>\n<p>\u3000\u3000}<\/p>\n<p>\u3000\u3000\u7121\u8ad6\u52d5\u614b\u5eab\u9084\u662f\u975c\u614b\u5eab\u90fd\u9700\u8981\u7528\u5230.o\u6587\u4ef6\u4f86\u751f\u6210\uff0c\u5148\u7de8\u8b6f\u751f\u6210.o\u6587\u4ef6\u3002<\/p>\n<p>\u3000\u3000# gcc -c hello.c<\/p>\n<p>\u3000\u30001\uff1a\u5275\u5efa\u975c\u614b\u5eab<\/p>\n<p>\u3000\u3000\u975c\u614b\u5eab\u6587\u4ef6\u540d\u7684\u547d\u540d\u898f\u7bc4\u662f\u4ee5lib\u70ba\u524d\u7db4\uff0c\u7dca\u63a5\u8457\u8ddf\u975c\u614b\u5eab\u540d\uff0c\u64f4\u5c55\u540d\u70ba.a\u3002\u4f8b\u5982\uff1a\u6211\u5011\u5c07\u5275\u5efa\u7684\u975c\u614b\u5eab\u540d\u70bamyhello\uff0c\u5247\u975c\u614b\u5eab\u6587\u4ef6\u540d\u5c31\u662flibmyhello.a\u3002<\/p>\n<p>\u3000\u3000# ar cr libmyhello.a hello.o<\/p>\n<p>\u3000\u3000\u4f7f\u7528\u975c\u614b\u5eab\uff1a\u53ea\u9700\u8981\u5728\u59b3\u7684\u6e90\u7a0b\u5e8f\u4e2d\u52a0\u5165\u5305\u542b\u59b3\u6240\u9700\u8981\u4f7f\u7528\u5230\u7684\u51fd\u6578\u7684\u8072\u660e\uff08\u5373\u5305\u542b\u982d\u6587\u4ef6\uff09\uff0c\u7136\u5f8c\u5728gcc\u751f\u6210\u76ee\u6a19\u6587\u4ef6\u6642\u5019\u6307\u660e\u975c\u614b\u5eab\u5c31OK\u4e86\uff08\u9664\u975e\u59b3\u5305\u542b\u7684\u982d\u6587\u4ef6\u5728\/usr\/include\uff0c\u5eab\u6587\u4ef6\u5728\u6a19\u6e96\u5eab\/usr\/lib,\/lib\u4e0b\uff0c\u5426\u5247\u59b3\u5f97\u986f\u793a\u6307\u660e\u4ed6\u5011\u7684\u8def\u5f91\uff09<\/p>\n<p>\u3000\u3000# gcc -o hello main.c -L. -lmyhello<\/p>\n<p>\u3000\u3000# .\/hello<\/p>\n<p>\u3000\u3000Hello everyone!<\/p>\n<p>\u3000\u3000\u522a\u9664\u975c\u614b\u5eab\u6587\u4ef6\u904b\u884c.\/hello\uff0c\u7a0b\u5e8f\u6b63\u5e38\u904b\u884c\uff0c\u8aaa\u660e\u975c\u614b\u5eab\u516c\u7528\u51fd\u6578\u5df2\u7d93\u93c8\u63a5\u5230\u76ee\u6a19\u6587\u4ef6\u3002<\/p>\n<p>\u3000\u30002\uff1a \u5229\u7528.o\u6587\u4ef6\u5275\u5efa\u52d5\u614b\u5eab<\/p>\n<p>\u3000\u3000\u52d5\u614b\u5eab\u6587\u4ef6\u64f4\u5c55\u540d\u70ba.so\u3002<\/p>\n<p>\u3000\u3000# gcc -shared -fPCI -o libmyhello.so hello.o<\/p>\n<p>\u3000\u3000\u52d5\u614b\u5eab\u7684\u4f7f\u7528\u8207\u975c\u614b\u5eab\u4f7f\u7528\u65b9\u5f0f\u58f9\u6a23<\/p>\n<p>\u3000\u3000# gcc -o hello main.c -L. -lmyhello<\/p>\n<p>\u3000\u3000# .\/hello<\/p>\n<p>\u3000\u3000.\/hello: error while loading shared libraries: libmyhello.so: cannot open shared object file: No such file or directory<\/p>\n<p>\u3000\u3000\u54e6\uff01\u51fa\u932f\u4e86\u3002\u5feb\u770b\u770b\u932f\u8aa4\u63d0\u793a\uff0c\u539f\u4f86\u662f\u627e\u4e0d\u5230\u52d5\u614b\u5eab\u6587\u4ef6libmyhello.so\u3002\u7a0b\u5e8f\u5728\u904b\u884c\u6642\uff0c\u6703\u5728\/usr\/lib\u548c\/lib\u7b49\u76ee\u9304\u4e2d\u67e5\u627e\u9700\u8981\u7684\u52d5\u614b\u5eab\u6587\u4ef6\u3002\u82e5\u627e\u5230\uff0c\u5247\u8f09\u5165\u52d5\u614b\u5eab\uff0c\u5426\u5247\u5c07\u63d0\u793a\u985e\u4f3c\u4e0a\u8ff0\u932f\u8aa4\u800c\u7d42\u6b62\u7a0b\u5e8f\u904b\u884c\u3002<\/p>\n<p>\u3000\u3000\u5982\u4f55\u627e\u5230\u751f\u6210\u7684\u52d5\u614b\u5eab\u67093\u7a2e\u65b9\u5f0f\uff1a<\/p>\n<p>\u3000\u30001)\u628a\u5eab\u62f7\u8c9d\u5230\/usr\/lib\u548c\/lib\u76ee\u9304\u4e0b\u3002<\/p>\n<p>\u3000\u3000(2)\u5728LD_LIBRARY_PATH\u74b0\u5883\u8b8a\u91cf\u4e2d\u52a0\u4e0a\u5eab\u6240\u5728\u8def\u5f91\u3002<\/p>\n<p>\u3000\u3000\u4f8b\u5982\u52d5\u614b\u5eablibhello.so\u5728\/home\/example\/lib\u76ee\u9304\u4e0b\uff1a<\/p>\n<p>\u3000\u3000$export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:\/home\/example\/lib<\/p>\n<p>\u3000\u3000(3) \u4fee\u6539\/etc\/ld.so.conf\u6587\u4ef6\uff0c\u628a\u5eab\u6240\u5728\u7684\u8def\u5f91\u52a0\u5230\u6587\u4ef6\u672b\u5c3e\uff0c\u4e26\u57f7\u884cldconfig\u5237\u65b0\u3002\u9019\u6a23\uff0c\u52a0\u5165\u7684\u76ee\u9304\u4e0b\u7684\u6240\u6709\u5eab\u6587\u4ef6\u90fd\u53ef\u898b\u3002<\/p>\n<p>\u3000\u3000\u7576\u975c\u614b\u5eab\u548c\u52d5\u614b\u5eab\u540c\u540d\u6642\uff0c gcc\u547d\u4ee4\u5c07\u512a\u5148\u4f7f\u7528\u52d5\u614b\u5eab\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u975c\u614b\u5eab\u5728\u7a0b\u5e8f\u7de8\u8b6f\u6642\u6703\u88ab\u9023\u63a5\u5230\u76ee\u6a19\u4ee3\u78bc\u4e2d\uff0c\u7a0b\u5e8f\u904b\u884c\u6642\u5c07\u4e0d\u518d\u9700\u8981\u8a72\u975c\u614b\u5eab\u3002 \u3000\u3000\u52d5\u614b\u5eab\u5728\u7a0b\u5e8f\u7de8\u8b6f\u6642\u4e26\u4e0d\u6703\u88ab\u9023\u63a5\u5230\u76ee [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[531],"tags":[295,704,703],"class_list":["post-1703","post","type-post","status-publish","format-standard","hentry","category-unix","tag-linux","tag-704","tag-703"],"_links":{"self":[{"href":"https:\/\/www.killtest.hk\/index.php\/wp-json\/wp\/v2\/posts\/1703","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.killtest.hk\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.killtest.hk\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.killtest.hk\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.killtest.hk\/index.php\/wp-json\/wp\/v2\/comments?post=1703"}],"version-history":[{"count":0,"href":"https:\/\/www.killtest.hk\/index.php\/wp-json\/wp\/v2\/posts\/1703\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.killtest.hk\/index.php\/wp-json\/wp\/v2\/media?parent=1703"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.killtest.hk\/index.php\/wp-json\/wp\/v2\/categories?post=1703"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.killtest.hk\/index.php\/wp-json\/wp\/v2\/tags?post=1703"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}