{"id":881,"date":"2014-09-20T14:40:31","date_gmt":"2014-09-20T06:40:31","guid":{"rendered":"http:\/\/www.menglanglang.cn\/?p=881"},"modified":"2016-02-28T15:27:19","modified_gmt":"2016-02-28T07:27:19","slug":"%e5%9c%a8linux%e4%b8%ad%e5%88%9b%e5%bb%ba%e9%9d%99%e6%80%81%e5%ba%93%e5%92%8c%e5%8a%a8%e6%80%81%e5%ba%93","status":"publish","type":"post","link":"http:\/\/www.menglanglang.cn\/?p=881","title":{"rendered":"\u5728Linux\u4e2d\u521b\u5efa\u9759\u6001\u5e93\u548c\u52a8\u6001\u5e93"},"content":{"rendered":"<p>\u6211\u4eec\u901a\u5e38\u628a\u4e00\u4e9b\u516c\u7528\u51fd\u6570\u5236\u4f5c\u6210\u51fd\u6570\u5e93\uff0c\u4f9b\u5176\u5b83\u7a0b\u5e8f\u4f7f\u7528\u3002\u51fd\u6570\u5e93\u5206\u4e3a\u9759\u6001\u5e93\u548c\u52a8\u6001\u5e93\u4e24\u79cd\u3002\u9759\u6001\u5e93\u5728\u7a0b\u5e8f\u7f16\u8bd1\u65f6\u4f1a\u88ab\u8fde\u63a5\u5230\u76ee\u6807\u4ee3\u7801\u4e2d\uff0c\u7a0b\u5e8f\u8fd0\u884c\u65f6\u5c06\u4e0d\u518d\u9700\u8981\u8be5\u9759\u6001\u5e93\u3002\u52a8\u6001\u5e93\u5728\u7a0b\u5e8f\u7f16\u8bd1\u65f6\u5e76\u4e0d\u4f1a\u88ab\u8fde\u63a5\u5230\u76ee\u6807\u4ee3\u7801\u4e2d\uff0c\u800c\u662f\u5728\u7a0b\u5e8f\u8fd0\u884c\u662f\u624d\u88ab\u8f7d\u5165\uff0c\u56e0\u6b64\u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\u8fd8\u9700\u8981\u52a8\u6001\u5e93\u5b58\u5728\u3002\u672c\u6587\u4e3b\u8981\u901a\u8fc7\u4e3e\u4f8b\u6765\u8bf4\u660e\u5728 Linux \u4e2d\u5982\u4f55\u521b\u5efa\u9759\u6001\u5e93\u548c\u52a8\u6001\u5e93\uff0c\u4ee5\u53ca\u4f7f\u7528\u5b83\u4eec\u3002<\/p>\n<p>\u5728\u521b\u5efa\u51fd\u6570\u5e93\u524d\uff0c\u6211\u4eec\u5148\u6765\u51c6\u5907\u4e3e\u4f8b\u7528\u7684\u6e90\u7a0b\u5e8f\uff0c\u5e76\u5c06\u51fd\u6570\u5e93\u7684\u6e90\u7a0b\u5e8f\u7f16\u8bd1\u6210.o\u6587\u4ef6\u3002<\/p>\n<p><strong>\u7b2c1\u6b65\uff1a\u7f16\u8f91\u5f97\u5230\u4e3e\u4f8b\u7684\u7a0b\u5e8f hello.h\u3001hello.c \u548c main.c\uff1b<\/strong><\/p>\n<p>hello.c(\u89c1\u7a0b\u5e8f2)\u662f\u51fd\u6570\u5e93\u7684\u6e90\u7a0b\u5e8f\uff0c\u5176\u4e2d\u5305\u542b\u516c\u7528\u51fd\u6570 hello\uff0c\u8be5\u51fd\u6570\u5c06\u5728\u5c4f\u5e55\u4e0a\u8f93\u51fa\"Hello XXX!\"\u3002hello.h(\u89c1\u7a0b\u5e8f1)\u4e3a\u8be5\u51fd\u6570\u5e93\u7684\u5934\u6587\u4ef6\u3002main.c(\u89c1\u7a0b\u5e8f3)\u4e3a\u6d4b\u8bd5\u5e93\u6587\u4ef6\u7684\u4e3b\u7a0b\u5e8f\uff0c\u5728\u4e3b\u7a0b\u5e8f\u4e2d\u8c03\u7528\u4e86\u516c\u7528\u51fd\u6570 hello\u3002<\/p>\n<pre><wbr><\/wbr>#ifndef HELLO_H\r\n#define HELLO_H\r\nvoid hello(const char *name);\r\n#endif \/\/HELLO_H<\/pre>\n<p><wbr><\/wbr>\u7a0b\u5e8f1: hello.h<\/p>\n<pre><wbr><\/wbr>#include &lt;stdio.h&gt;\r\nvoid hello(const char *name)\r\n{\r\n\u00a0printf(\"Hello %s!\/n\", name);\r\n}<\/pre>\n<p><wbr><\/wbr>\u7a0b\u5e8f2: hello.c<\/p>\n<pre><wbr><\/wbr>#include \"hello.h\"\r\nint main()\r\n{\r\n\u00a0hello(\"everyone\");\r\n\u00a0return 0;\r\n}<\/pre>\n<p><wbr><\/wbr>\u7a0b\u5e8f3: main.c<\/p>\n<p><strong>\u7b2c2\u6b65\uff1a\u5c06 hello.c \u7f16\u8bd1\u6210 .o \u6587\u4ef6\uff1b<\/strong><\/p>\n<p>\u65e0\u8bba\u9759\u6001\u5e93\uff0c\u8fd8\u662f\u52a8\u6001\u5e93\uff0c\u90fd\u662f\u7531.o\u6587\u4ef6\u521b\u5efa\u7684\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u5fc5\u987b\u5c06\u6e90\u7a0b\u5e8fhello.c\u901a\u8fc7gcc\u5148\u7f16\u8bd1\u6210.o\u6587\u4ef6\u3002<\/p>\n<p>\u5728\u7cfb\u7edf\u63d0\u793a\u7b26\u4e0b\u952e\u5165\u4ee5\u4e0b\u547d\u4ee4\u5f97\u5230hello.o\u6587\u4ef6\u3002<\/p>\n<pre># gcc -c hello.c<\/pre>\n<p>(\u6ce81\uff1a\u672c\u6587\u4e0d\u4ecb\u7ecd\u5404\u547d\u4ee4\u4f7f\u7528\u548c\u5176\u53c2\u6570\u529f\u80fd\uff0c\u82e5\u5e0c\u671b\u8be6\u7ec6\u4e86\u89e3\u5b83\u4eec\uff0c\u8bf7\u53c2\u8003\u5176\u4ed6\u6587\u6863\u3002)<\/p>\n<p>(\u6ce82\uff1a\u9996\u5b57\u7b26\"#\"\u662f\u7cfb\u7edf\u63d0\u793a\u7b26\uff0c\u4e0d\u9700\u8981\u952e\u5165\uff0c\u4e0b\u6587\u76f8\u540c\u3002)<\/p>\n<p>\u6211\u4eec\u8fd0\u884c ls \u547d\u4ee4\u770b\u770b\u662f\u5426\u751f\u5b58\u4e86 hello.o \u6587\u4ef6\u3002<\/p>\n<pre># ls\r\nhello.c hello.h hello.o main.c<\/pre>\n<p>(\u6ce83\uff1a\u9996\u5b57\u7b26\u4e0d\u662f\"#\"\u4e3a\u7cfb\u7edf\u8fd0\u884c\u7ed3\u679c\uff0c\u4e0b\u6587\u76f8\u540c\u3002)<\/p>\n<p>\u5728 ls \u547d\u4ee4\u7ed3\u679c\u4e2d\uff0c\u6211\u4eec\u770b\u5230\u4e86 hello.o \u6587\u4ef6\uff0c\u672c\u6b65\u64cd\u4f5c\u5b8c\u6210\u3002<\/p>\n<p>\u4e0b\u9762\u6211\u4eec\u5148\u6765\u770b\u770b\u5982\u4f55\u521b\u5efa\u9759\u6001\u5e93\uff0c\u4ee5\u53ca\u4f7f\u7528\u5b83\u3002<\/p>\n<p><strong>\u7b2c3\u6b65\uff1a\u7531 .o \u6587\u4ef6\u521b\u5efa\u9759\u6001\u5e93\uff1b<\/strong><\/p>\n<p>\u9759\u6001\u5e93\u6587\u4ef6\u540d\u7684\u547d\u540d\u89c4\u8303\u662f\u4ee5 lib \u4e3a\u524d\u7f00\uff0c\u7d27\u63a5\u7740\u8ddf\u9759\u6001\u5e93\u540d\uff0c\u6269\u5c55\u540d\u4e3a .a\u3002\u4f8b\u5982\uff1a\u6211\u4eec\u5c06\u521b\u5efa\u7684\u9759\u6001\u5e93\u540d\u4e3a myhello\uff0c\u5219\u9759\u6001\u5e93\u6587\u4ef6\u540d\u5c31\u662f libmyhello.a\u3002\u5728\u521b\u5efa\u548c\u4f7f\u7528\u9759\u6001\u5e93\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u8fd9\u70b9\u3002\u521b\u5efa\u9759\u6001\u5e93\u7528 ar \u547d\u4ee4\u3002<\/p>\n<p>\u5728\u7cfb\u7edf\u63d0\u793a\u7b26\u4e0b\u952e\u5165\u4ee5\u4e0b\u547d\u4ee4\u5c06\u521b\u5efa\u9759\u6001\u5e93\u6587\u4ef6 libmyhello.a\u3002<\/p>\n<pre># ar cr libmyhello.a hello.o<\/pre>\n<p>\u6211\u4eec\u540c\u6837\u8fd0\u884cls\u547d\u4ee4\u67e5\u770b\u7ed3\u679c\uff1a<\/p>\n<pre># ls\r\nhello.c hello.h hello.o libmyhello.a main.c<\/pre>\n<p>ls \u547d\u4ee4\u7ed3\u679c\u4e2d\u6709 libmyhello.a\u3002<\/p>\n<p><strong>\u7b2c4\u6b65\uff1a\u5728\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u9759\u6001\u5e93\uff1b<\/strong><\/p>\n<p>\u9759\u6001\u5e93\u5236\u4f5c\u5b8c\u4e86\uff0c\u5982\u4f55\u4f7f\u7528\u5b83\u5185\u90e8\u7684\u51fd\u6570\u5462\uff1f\u53ea\u9700\u8981\u5728\u4f7f\u7528\u5230\u8fd9\u4e9b\u516c\u7528\u51fd\u6570\u7684\u6e90\u7a0b\u5e8f\u4e2d\u5305\u542b\u8fd9\u4e9b\u516c\u7528\u51fd\u6570\u7684\u539f\u578b\u58f0\u660e\uff0c\u7136\u540e\u5728\u7528 gcc \u547d\u4ee4\u751f\u6210\u76ee\u6807\u6587\u4ef6\u65f6\u6307\u660e\u9759\u6001\u5e93\u540d\uff0cgcc \u5c06\u4f1a\u4ece\u9759\u6001\u5e93\u4e2d\u5c06\u516c\u7528\u51fd\u6570\u8fde\u63a5\u5230\u76ee\u6807\u6587\u4ef6\u4e2d\u3002\u6ce8\u610f\uff0cgcc \u4f1a\u5728\u9759\u6001\u5e93\u540d\u524d\u52a0\u4e0a\u524d\u7f00 lib\uff0c\u7136\u540e\u8ffd\u52a0\u6269\u5c55\u540d .a \u5f97\u5230\u7684\u9759\u6001\u5e93\u6587\u4ef6\u540d\u6765\u67e5\u627e\u9759\u6001\u5e93\u6587\u4ef6\u3002<\/p>\n<p>\u5728\u7a0b\u5e8f3: main.c \u4e2d\uff0c\u6211\u4eec\u5305\u542b\u4e86\u9759\u6001\u5e93\u7684\u5934\u6587\u4ef6 hello.h\uff0c\u7136\u540e\u5728\u4e3b\u7a0b\u5e8f main \u4e2d\u76f4\u63a5\u8c03\u7528\u516c\u7528\u51fd\u6570 hello\u3002\u4e0b\u9762\u5148\u751f\u6210\u76ee\u6807\u7a0b\u5e8f hello\uff0c\u7136\u540e\u8fd0\u884c hello \u7a0b\u5e8f\u770b\u770b\u7ed3\u679c\u5982\u4f55\u3002<\/p>\n<pre># gcc -o hello main.c -L. -lmyhello\r\n# .\/hello\r\nHello everyone!<\/pre>\n<p>\u6211\u4eec\u5220\u9664\u9759\u6001\u5e93\u6587\u4ef6\u8bd5\u8bd5\u516c\u7528\u51fd\u6570 hello \u662f\u5426\u771f\u7684\u8fde\u63a5\u5230\u76ee\u6807\u6587\u4ef6\u00a0hello \u4e2d\u4e86\u3002<\/p>\n<pre># rm libmyhello.a\r\nrm: remove regular file `libmyhello.a'? y\r\n# .\/hello\r\nHello everyone!<\/pre>\n<p>\u7a0b\u5e8f\u7167\u5e38\u8fd0\u884c\uff0c\u9759\u6001\u5e93\u4e2d\u7684\u516c\u7528\u51fd\u6570\u5df2\u7ecf\u8fde\u63a5\u5230\u76ee\u6807\u6587\u4ef6\u4e2d\u4e86\u3002<\/p>\n<p>\u6211\u4eec\u7ee7\u7eed\u770b\u770b\u5982\u4f55\u5728 Linux \u4e2d\u521b\u5efa\u52a8\u6001\u5e93\u3002\u6211\u4eec\u8fd8\u662f\u4ece .o \u6587\u4ef6\u5f00\u59cb\u3002<\/p>\n<p><strong>\u7b2c5\u6b65\uff1a\u7531 .o \u6587\u4ef6\u521b\u5efa\u52a8\u6001\u5e93\u6587\u4ef6\uff1b<\/strong><\/p>\n<p>\u52a8\u6001\u5e93\u6587\u4ef6\u540d\u547d\u540d\u89c4\u8303\u548c\u9759\u6001\u5e93\u6587\u4ef6\u540d\u547d\u540d\u89c4\u8303\u7c7b\u4f3c\uff0c\u4e5f\u662f\u5728\u52a8\u6001\u5e93\u540d\u589e\u52a0\u524d\u7f00 lib\uff0c\u4f46\u5176\u6587\u4ef6\u6269\u5c55\u540d\u4e3a .so\u3002\u4f8b\u5982\uff1a\u6211\u4eec\u5c06\u521b\u5efa\u7684\u52a8\u6001\u5e93\u540d\u4e3a myhello\uff0c\u5219\u52a8\u6001\u5e93\u6587\u4ef6\u540d\u5c31\u662f libmyhello.so\u3002\u7528 gcc \u6765\u521b\u5efa\u52a8\u6001\u5e93\u3002<\/p>\n<p>\u5728\u7cfb\u7edf\u63d0\u793a\u7b26\u4e0b\u952e\u5165\u4ee5\u4e0b\u547d\u4ee4\u5f97\u5230\u52a8\u6001\u5e93\u6587\u4ef6 libmyhello.so\u3002<\/p>\n<pre># gcc -shared -fPCI -o libmyhello.so hello.o<\/pre>\n<p>\u6211\u4eec\u7167\u6837\u4f7f\u7528 ls \u547d\u4ee4\u770b\u770b\u52a8\u6001\u5e93\u6587\u4ef6\u662f\u5426\u751f\u6210\u3002<\/p>\n<pre># ls\r\nhello.c hello.h hello.o libmyhello.so main.c<\/pre>\n<p><strong>\u7b2c6\u6b65\uff1a\u5728\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u52a8\u6001\u5e93\uff1b<\/strong><\/p>\n<p>\u5728\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u52a8\u6001\u5e93\u548c\u4f7f\u7528\u9759\u6001\u5e93\u5b8c\u5168\u4e00\u6837\uff0c\u4e5f\u662f\u5728\u4f7f\u7528\u5230\u8fd9\u4e9b\u516c\u7528\u51fd\u6570\u7684\u6e90\u7a0b\u5e8f\u4e2d\u5305\u542b\u8fd9\u4e9b\u516c\u7528\u51fd\u6570\u7684\u539f\u578b\u58f0\u660e\uff0c\u7136\u540e\u5728\u7528 gcc \u547d\u4ee4\u751f\u6210\u76ee\u6807\u6587\u4ef6\u65f6\u6307\u660e\u52a8\u6001\u5e93\u540d\u8fdb\u884c\u7f16\u8bd1\u3002\u6211\u4eec\u5148\u8fd0\u884c gcc \u547d\u4ee4\u751f\u6210\u76ee\u6807\u6587\u4ef6\uff0c\u518d\u8fd0\u884c\u5b83\u770b\u770b\u7ed3\u679c\u3002<\/p>\n<pre># gcc -o hello main.c -L. -lmyhello\r\n# .\/hello\r\n.\/hello: error while loading shared libraries: libmyhello.so: cannot open shared object file: No such file or directory<\/pre>\n<p>\u54e6\uff01\u51fa\u9519\u4e86\u3002\u5feb\u770b\u770b\u9519\u8bef\u63d0\u793a\uff0c\u539f\u6765\u662f\u627e\u4e0d\u5230\u52a8\u6001\u5e93\u6587\u4ef6 libmyhello.so\u3002\u7a0b\u5e8f\u5728\u8fd0\u884c\u65f6\uff0c\u4f1a\u5728 \/usr\/lib \u548c \/lib \u7b49\u76ee\u5f55\u4e2d\u67e5\u627e\u9700\u8981\u7684\u52a8\u6001\u5e93\u6587\u4ef6\u3002\u82e5\u627e\u5230\uff0c\u5219\u8f7d\u5165\u52a8\u6001\u5e93\uff0c\u5426\u5219\u5c06\u63d0\u793a\u7c7b\u4f3c\u4e0a\u8ff0\u9519\u8bef\u800c\u7ec8\u6b62\u7a0b\u5e8f\u8fd0\u884c\u3002\u6211\u4eec\u5c06\u6587\u4ef6libmyhello.so \u590d\u5236\u5230\u76ee\u5f55 \/usr\/lib \u4e2d\uff0c\u518d\u8bd5\u8bd5\u3002<\/p>\n<pre># mv libmyhello.so \/usr\/lib\r\n# .\/hello\r\nHello everyone!<\/pre>\n<p>\u6210\u529f\u4e86\u3002\u8fd9\u4e5f\u8fdb\u4e00\u6b65\u8bf4\u660e\u4e86\u52a8\u6001\u5e93\u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\u662f\u9700\u8981\u7684\u3002<\/p>\n<p>\u6211\u4eec\u56de\u8fc7\u5934\u770b\u770b\uff0c\u53d1\u73b0\u4f7f\u7528\u9759\u6001\u5e93\u548c\u4f7f\u7528\u52a8\u6001\u5e93\u7f16\u8bd1\u6210\u76ee\u6807\u7a0b\u5e8f\u4f7f\u7528\u7684 gcc \u547d\u4ee4\u5b8c\u5168\u4e00\u6837\uff0c\u90a3\u5f53\u9759\u6001\u5e93\u548c\u52a8\u6001\u5e93\u540c\u540d\u65f6\uff0cgcc \u547d\u4ee4\u4f1a\u4f7f\u7528\u54ea\u4e2a\u5e93\u6587\u4ef6\u5462\uff1f\u62b1\u7740\u5bf9\u95ee\u9898\u5fc5\u7a76\u5230\u5e95\u7684\u5fc3\u60c5\uff0c\u6765\u8bd5\u8bd5\u770b\u3002<\/p>\n<p>\u5148\u5220\u9664\u9664 .c \u548c .h \u5916\u7684\u6240\u6709\u6587\u4ef6\uff0c\u6062\u590d\u6210\u6211\u4eec\u521a\u521a\u7f16\u8f91\u5b8c\u4e3e\u4f8b\u7a0b\u5e8f\u72b6\u6001\u3002<\/p>\n<pre># rm -f hello hello.o \/usr\/lib\/libmyhello.so\r\n# ls\r\nhello.c hello.h main.c<\/pre>\n<p>\u5728\u6765\u521b\u5efa\u9759\u6001\u5e93\u6587\u4ef6 libmyhello.a \u548c\u52a8\u6001\u5e93\u6587\u4ef6 libmyhello.so\u3002<\/p>\n<pre># gcc -c hello.c\r\n# ar cr libmyhello.a hello.o\r\n# gcc -shared -fPCI -o libmyhello.so hello.o\r\n# ls\r\nhello.c hello.h hello.o libmyhello.a libmyhello.so main.c<\/pre>\n<p>\u901a\u8fc7\u4e0a\u8ff0\u6700\u540e\u4e00\u6761 ls \u547d\u4ee4\uff0c\u53ef\u4ee5\u53d1\u73b0\u9759\u6001\u5e93\u6587\u4ef6 libmyhello.a \u548c\u52a8\u6001\u5e93\u6587\u4ef6 libmyhello.so \u90fd\u5df2\u7ecf\u751f\u6210\uff0c\u5e76\u90fd\u5728\u5f53\u524d\u76ee\u5f55\u4e2d\u3002\u7136\u540e\uff0c\u6211\u4eec\u8fd0\u884c gcc \u547d\u4ee4\u6765\u4f7f\u7528\u51fd\u6570\u5e93 myhello \u751f\u6210\u76ee\u6807\u6587\u4ef6 hello\uff0c\u5e76\u8fd0\u884c\u7a0b\u5e8f\u00a0hello\u3002<\/p>\n<pre># gcc -o hello main.c -L. -lmyhello\r\n# .\/hello\r\n.\/hello: error while loading shared libraries: libmyhello.so: cannot open shared object file: No such file or directory<\/pre>\n<p>\u4ece\u7a0b\u5e8f hello \u8fd0\u884c\u7684\u7ed3\u679c\u4e2d\u5f88\u5bb9\u6613\u77e5\u9053\uff0c\u5f53\u9759\u6001\u5e93\u548c\u52a8\u6001\u5e93\u540c\u540d\u65f6\uff0c\u00a0gcc \u547d\u4ee4\u5c06\u4f18\u5148\u4f7f\u7528\u52a8\u6001\u5e93\u3002<\/p>\n<p>muse@ubuntu:~$ sudo su<\/p>\n<p>[sudo] password for muse:<\/p>\n<p>root@ubuntu:\/home\/muse# cd program\/<\/p>\n<p>root@ubuntu:\/home\/muse\/program# cd test\/<\/p>\n<p>root@ubuntu:\/home\/muse\/program\/test# gcc -c hello.c<\/p>\n<p>root@ubuntu:\/home\/muse\/program\/test# ls<\/p>\n<p>hello.c\u00a0<wbr><\/wbr>\u00a0hello.h\u00a0<wbr><\/wbr>\u00a0hello.o\u00a0<wbr><\/wbr>\u00a0main.c<\/p>\n<p>root@ubuntu:\/home\/muse\/program\/test# ar cr libhello.a hello.o<\/p>\n<p>root@ubuntu:\/home\/muse\/program\/test# ls<\/p>\n<p>hello.c\u00a0<wbr><\/wbr>\u00a0hello.h\u00a0<wbr><\/wbr>\u00a0hello.o\u00a0<wbr><\/wbr>\u00a0libhello.a\u00a0<wbr><\/wbr>\u00a0main.c<\/p>\n<p>root@ubuntu:\/home\/muse\/program\/test# gcc -o hello main.c -L. -lmyhello<\/p>\n<p>\/usr\/bin\/ld: cannot find -lmyhello<\/p>\n<p>collect2: ld returned 1 exit status<\/p>\n<p>root@ubuntu:\/home\/muse\/program\/test# gcc -o hello main.c -L. -lhello<\/p>\n<p>root@ubuntu:\/home\/muse\/program\/test# ls<\/p>\n<p>hello\u00a0<wbr><\/wbr>\u00a0hello.c\u00a0<wbr><\/wbr>\u00a0hello.h\u00a0<wbr><\/wbr>\u00a0hello.o\u00a0<wbr><\/wbr>\u00a0libhello.a\u00a0<wbr><\/wbr>\u00a0main.c<\/p>\n<p>root@ubuntu:\/home\/muse\/program\/test# .\/hello<\/p>\n<p>Hello everyone!\/nroot@ubuntu:\/home\/muse\/program\/test# rm libhello.a<\/p>\n<p>root@ubuntu:\/home\/muse\/program\/test# ls<\/p>\n<p>hello\u00a0<wbr><\/wbr>\u00a0hello.c\u00a0<wbr><\/wbr>\u00a0hello.h\u00a0<wbr><\/wbr>\u00a0hello.o\u00a0<wbr><\/wbr>\u00a0main.c<\/p>\n<p>root@ubuntu:\/home\/muse\/program\/test# .\/hello<\/p>\n<p>Hello everyone!\/nroot@ubuntu:\/home\/muse\/program\/test#<\/p>\n<p>root@ubuntu:\/home\/muse\/program\/test# gcc -shared -fPCI -o libmyhello.so hello.o root@ubuntu:\/home\/muse\/program\/test# ls<\/p>\n<p>hello\u00a0<wbr><\/wbr>\u00a0hello.c\u00a0<wbr><\/wbr>\u00a0hello.h\u00a0<wbr><\/wbr>\u00a0hello.o\u00a0<wbr><\/wbr>\u00a0libmyhello.so\u00a0<wbr><\/wbr>\u00a0main.c<\/p>\n<p>root@ubuntu:\/home\/muse\/program\/test# gcc -o hello main.c -L. -lmyhello<\/p>\n<p>root@ubuntu:\/home\/muse\/program\/test# ls<\/p>\n<p>hello\u00a0<wbr><\/wbr>\u00a0hello.c\u00a0<wbr><\/wbr>\u00a0hello.h\u00a0<wbr><\/wbr>\u00a0hello.o\u00a0<wbr><\/wbr>\u00a0libmyhello.so\u00a0<wbr><\/wbr>\u00a0main.c<\/p>\n<p>root@ubuntu:\/home\/muse\/program\/test# .\/hello<\/p>\n<p>.\/hello: error while loading shared libraries: libmyhello.so: cannot open shared object file: No such file or directory<\/p>\n<p>root@ubuntu:\/home\/muse\/program\/test# mv libmyhello.so \/usr\/lib<\/p>\n<p>root@ubuntu:\/home\/muse\/program\/test# .\/hello<\/p>\n<p>Hello everyone!\/nroot@ubuntu:\/home\/muse\/program\/test# rm -f hello hello.o \/usr\/lroot@ubuntu:\/home\/muse\/program\/test# ls<\/p>\n<p>hello.c\u00a0<wbr><\/wbr>\u00a0hello.h\u00a0<wbr><\/wbr>\u00a0main.c<\/p>\n<p>root@ubuntu:\/home\/muse\/program\/test# gcc -c hello.c<\/p>\n<p>root@ubuntu:\/home\/muse\/program\/test# ar cr libmyhello.a hello.o<\/p>\n<p>root@ubuntu:\/home\/muse\/program\/test# gcc -shared -fPCI -o libmyhello.so hello.o root@ubuntu:\/home\/muse\/program\/test# ls<\/p>\n<p>hello.c\u00a0<wbr><\/wbr>\u00a0hello.h\u00a0<wbr><\/wbr>\u00a0hello.o\u00a0<wbr><\/wbr>\u00a0libmyhello.a\u00a0<wbr><\/wbr>\u00a0libmyhello.so\u00a0<wbr><\/wbr>\u00a0main.c<\/p>\n<p>root@ubuntu:\/home\/muse\/program\/test#<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6211\u4eec\u901a\u5e38\u628a\u4e00\u4e9b\u516c\u7528\u51fd\u6570\u5236\u4f5c\u6210\u51fd\u6570\u5e93\uff0c\u4f9b\u5176\u5b83\u7a0b\u5e8f\u4f7f\u7528\u3002\u51fd\u6570\u5e93\u5206\u4e3a\u9759\u6001\u5e93\u548c\u52a8\u6001\u5e93\u4e24\u79cd\u3002\u9759\u6001\u5e93\u5728\u7a0b\u5e8f\u7f16\u8bd1\u65f6\u4f1a\u88ab\u8fde\u63a5\u5230 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[227],"tags":[602,284,283],"class_list":["post-881","post","type-post","status-publish","format-standard","hentry","category-linux","tag-linux","tag-284","tag-283"],"_links":{"self":[{"href":"http:\/\/www.menglanglang.cn\/index.php?rest_route=\/wp\/v2\/posts\/881","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.menglanglang.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.menglanglang.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.menglanglang.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.menglanglang.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=881"}],"version-history":[{"count":2,"href":"http:\/\/www.menglanglang.cn\/index.php?rest_route=\/wp\/v2\/posts\/881\/revisions"}],"predecessor-version":[{"id":2683,"href":"http:\/\/www.menglanglang.cn\/index.php?rest_route=\/wp\/v2\/posts\/881\/revisions\/2683"}],"wp:attachment":[{"href":"http:\/\/www.menglanglang.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=881"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.menglanglang.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=881"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.menglanglang.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=881"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}