$gcc -o test main.c -lmytest -L.\uff08\u5e93\u6240\u5728\u76ee\u5f55\uff0c.\u8868\u793a\u5f53\u524d\u76ee\u5f55\uff09<\/pre>\n\u5355L\u94fe\u63a5\u6cd5\uff08\u9700\u8981\u914d\u7f6e\u73af\u5883\u53d8\u91cfLIBRARY_PATH\uff0c\u628a\u5e93\u653e\u5728\u6240\u5728\u76ee\u5f55\u914d\u7f6e\uff09<\/p>\n
$gcc -o test main.c -lmytest<\/pre>\n\u524d\u9762\u6211\u4eec\u5df2\u7ecf\u5199\u597d\u4e86main.c\uff0c\u73b0\u5728\u6d4b\u8bd5\u4e00\u4e0b<\/p>\n
$ gcc -o test main.c -L. -lmytest\r\n$ ls -l<\/pre>\n<\/p>\n
$ .\/test<\/pre>\n<\/p>\n
\u8fd9\u91ccgcc\u7684\u53c2\u6570-L\u662f\u544a\u8bc9\u7f16\u8bd1\u5668\u5e93\u6587\u4ef6\u7684\u8def\u5f84\u662f\u5f53\u524d\u76ee\u5f55\uff0c-l\u662f\u544a\u8bc9\u7f16\u8bd1\u5668\u8981\u4f7f\u7528\u7684\u5e93\u7684\u540d\u5b57\u53ebmytest\u3002<\/p>\n
2.3\u9759\u6001\u5e93\u603b\u7ed3<\/strong><\/span><\/div>\n\u5728\u4e00\u4e2a\u5934\u6587\u4ef6\u4e2d\u58f0\u660e\u9759\u6001\u5e93\u6240\u5bfc\u51fa\u7684\u51fd\u6570\u3002
\n\u5728\u4e00\u4e2a\u6e90\u6587\u4ef6\u4e2d\u5b9e\u73b0\u9759\u6001\u5e93\u6240\u5bfc\u51fa\u7684\u51fd\u6570\u3002
\n\u7f16\u8bd1\u6e90\u6587\u4ef6\uff0c\u751f\u6210\u53ef\u6267\u884c\u4ee3\u7801\u3002
\n\u5c06\u53ef\u6267\u884c\u4ee3\u7801\u6240\u5728\u7684\u76ee\u6807\u6587\u4ef6\u52a0\u5165\u5230\u67d0\u4e2a\u9759\u6001\u5e93\u4e2d\uff0c\u5e76\u5c06\u9759\u6001\u5e93\u62f7\u8d1d\u5230\u7cfb\u7edf\u9ed8\u8ba4\u7684\u5b58\u653e\u5e93\u6587\u4ef6\u7684\u76ee\u5f55\u4e0b\u3002
\n\u4e0b\u9762\u901a\u8fc7\u4e00\u4e2a\u4f8b\u5b50\u6765\u8bf4\u660e\uff1a
\n\u6e90\u6587\u4ef6\uff1amylib.c<\/p>\n
#includevoid welcome(void)printf(\"welcome to libmylib\\n\");<\/pre>\n1>\u7f16\u8bd1mylib.c\u751f\u6210\u76ee\u6807\u6587\u4ef6\uff1a<\/p>\n
$gcc -o mylib.o -c mylib.c<\/pre>\n2>\u5c06\u76ee\u6807\u6587\u4ef6\u52a0\u5165\u5230\u9759\u6001\u5e93\u4e2d\uff1a<\/p>\n
$ar -rcs libmylib.a mylib.o<\/pre>\n\u3010\u6ce8\u3011\u5e93\u6587\u4ef6\u540d\u4ee5lib\u5f00\u5934\uff0c\u4ee5.a\u7ed3\u5c3e<\/p>\n
3>\u5c06\u9759\u6001\u5e93copy\u5230Linux\u7684\u5e93\u76ee\u5f55\uff08\/usr\/lib\u6216\u8005\/lib\uff09\u4e0b\uff1a\uff08\u4e5f\u53ef\u4ee5\u4e0d\u62f7\u8d1d\uff0c\u6ce8\u610f\u914d\u7f6e\u73af\u5883\u53d8\u91cf\uff09<\/p>\n
$sudo cp libmylib.a \/usr\/lib\/libmylib.a \/\/\u7ba1\u7406\u5458\u8eab\u4efd<\/pre>\n\u7f16\u5199\u8c03\u7528\u5e93\u51fd\u6570\u7684\u6d4b\u8bd5\u7a0b\u5e8ftest.c:<\/p>\n
#includeint main(void)printf(\"create and use library:\\n\");welcome();return 0;<\/pre>\n4>\u4f7f\u7528\u9759\u6001\u5e93\u7f16\u8bd1\uff1a<\/p>\n
$gcc -o test test.c -lmylib<\/pre>\n\u8fd9\u91cc\u6ce8\u610f\uff0c\u7f16\u8bd1\u65f6\u65e0\u9700\u5e26\u4e0a\u524d\u7f00\u548c\u540e\u7f00\u3002<\/p>\n
5>\u8fd0\u884c\u53ef\u6267\u884c\u7a0b\u5e8ftest:<\/p>\n
$.\/test<\/pre>\n<\/p>\n
\u5728Linux\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528ar\u547d\u4ee4\u6765\u521b\u5efa\u548c\u4fee\u6539\u9759\u6001\u5e93\u3002
\n\u8fd9\u4e9b\u5728linux\u4e0bman ar\u4e00\u4e0b\u5c31\u53ef\u4ee5\u5f97\u5230\u53c2\u6570\uff0c\u8fd9\u91cc\u8bf4\u660e\u51e0\u4e2a\u5e38\u7528\u7684<\/p>\n
d:\u4ece\u5e93\u4e2d\u5220\u9664\u6210\u5458\u6587\u4ef6\u3002<\/li>\nr:\u5728\u5e93\u4e2d\u52a0\u5165\u6210\u5458\u6587\u4ef6\uff0c\u82e5\u5b58\u5728\uff0c\u5219\u66ff\u6362\u3002<\/li>\nc:\u521b\u5efa\u4e00\u4e2a\u5e93\u3002<\/li>\ns:\u65e0\u8bbaar\u547d\u4ee4\u662f\u5426\u4fee\u6539\u4e86\u5e93\u5185\u5bb9\uff0c\u90fd\u5f3a\u5236\u91cd\u65b0\u751f\u6210\u5e93\u7b26\u53f7\u8868\u3002<\/li>\n\u5176\u4ed6\u7684\u547d\u4ee4\u7528\u65f6\u518dman\u3002
\n\u3010\u6ce8\u3011gcc -static \u6587\u4ef6.c [-o \u6587\u4ef6]\/\/\u5168\u90e8\u4f7f\u7528\u9759\u6001\u5e93\u7684\u65b9\u6cd5<\/p>\n
3 Linux\u521b\u5efa\u548c\u4f7f\u7528\u52a8\u6001\u5e93<\/strong><\/div>\n\u52a8\u6001\u5e93\u7684\u57fa\u672c\u6982\u5ff5<\/p>\n
\u52a8\u6001\u5e93\u7684\u57fa\u672c\u6982\u5ff5<\/p>\n
1.\u52a8\u6001\u94fe\u63a5\u5e93\u662f\u7a0b\u5e8f\u8fd0\u884c\u65f6\u52a0\u8f7d\u7684\u5e93\uff0c\u5f53\u52a8\u6001\u94fe\u63a5\u5e93\u6b63\u786e\u5b89\u88c5\u540e\uff0c\u6240\u6709\u7684\u7a0b\u5e8f\u90fd\u53ef\u4ee5\u4f7f\u7528\u52a8\u6001\u5e93\u6765\u8fd0\u884c\u7a0b\u5e8f\u3002\u52a8\u6001\u94fe\u63a5\u5e93\u662f\u76ee\u6807\u6587\u4ef6\u7684\u96c6\u5408\uff0c\u76ee\u6807\u6587\u4ef6\u5728\u52a8\u6001\u94fe\u63a5\u5e93\u4e2d\u7684\u7ec4\u7ec7\u65b9\u5f0f\u662f\u6309\u7167\u7279\u6b8a\u65b9\u5f0f\u5f62\u6210\u7684\u3002\u5e93\u4e2d\u51fd\u6570\u548c\u53d8\u91cf\u7684\u5730\u5740\u662f\u76f8\u5bf9\u5730\u5740\uff0c\u4e0d\u662f\u7edd\u5bf9\u5730\u5740\uff0c\u5176\u771f\u5b9e\u5730\u5740\u5728\u8c03\u7528\u52a8\u6001\u5e93\u7684\u7a0b\u5e8f\u52a0\u8f7d\u65f6\u5f62\u6210\u3002<\/p>\n
2.\u52a8\u6001\u94fe\u63a5\u5e93\u7684\u540d\u79f0\u6709\u522b\u540d\uff08soname\uff09, \u771f\u540d(realname)\u548c\u94fe\u63a5\u540d\uff08linker name\uff09\u3002\u522b\u540d\u7531\u4e00\u4e2a\u524d\u7f00lib,\u7136\u540e\u662f\u5e93\u7684\u540d\u5b57\uff0c\u518d\u52a0\u4e0a\u4e00\u4e2a\u540e\u7f00\u201c.so\u201d\u6784\u6210(\"libxxx.so\")\u3002\u771f\u540d\u662f\u52a8\u6001\u94fe\u63a5\u5e93\u771f\u5b9e\u540d\u79f0\uff0c\u4e00\u822c\u603b\u662f\u5728\u522b\u540d\u7684\u57fa\u7840\u52a0\u4e0a\u4e00\u4e2a\u5c0f\u7248\u672c\u53f7\uff0c\u53d1\u5e03\u7248\u672c\u7b49\u6784\u6210\u3002\u9664\u6b64\u4e4b\u5916\uff0c\u8fd8\u6709\u4e00\u4e2a\u94fe\u63a5\u540d\uff0c\u5373\u7a0b\u5e8f\u94fe\u63a5\u65f6\u4f7f\u7528\u7684\u5e93\u7684\u540d\u5b57\u3002<\/p>\n
3.\u5728\u52a8\u6001\u94fe\u63a5\u5e93\u5b89\u88c5\u7684\u65f6\u5019\uff0c\u603b\u662f\u590d\u5236\u6587\u4ef6\u5230\u67d0\u4e2a\u76ee\u5f55\u4e0b\uff0c\u7136\u540e\u7528\u4e00\u4e2a\u8f6f\u8fde\u63a5\u751f\u6210\u522b\u540d\uff0c\u5728\u5e93\u6587\u4ef6\u8fdb\u884c\u66f4\u65b0\u7684\u65f6\u5019\uff0c\u4ec5\u4ec5\u66f4\u65b0\u8f6f\u94fe\u63a5\u5373\u53ef\u3002<\/p>\n
\u4e0b\u9762\u6211\u4eec\u901a\u8fc7\u4e00\u4e2a\u5b9e\u4f8b\u6765\u5b66\u4e60\u5982\u4f55\u751f\u6210\u52a8\u6001\u5e93\u548c\u4f7f\u7528\u52a8\u6001\u5e93\u3002<\/p>\n
3.1\u751f\u6210\u52a8\u6001\u5e93<\/strong><\/span><\/div>\n1\uff09\u5f53\u524d\u6587\u4ef6\u5939\u4e0b\u6709\u4e0b\u9762\u56db\u4e2a\u6587\u4ef6<\/p>\n
$ ls -l<\/pre>\n\u6587\u4ef6\u5185\u5bb9\u5206\u522b\u4e3a\uff1a<\/p>\n
$ cat mylib.h\r\n#ifndef _MYLIB_H_#define _MYLIB_H_#includevoid print1();void print2();#endif\r\n$ cat dy1.c\r\n#include \"mylib.h\"void print1()printf(\"My first shared lib!\\n\");\r\n$ cat dy2.c\r\n#include \"mylib.h\"void print2()printf(\"My second shared lib!\\n\");\r\n$ cat main.c\r\n#include \"mylib.h\"int main(int argc, char *argv[])print1();print2();return 0;<\/pre>\n2\uff09\u8fd9\u91cc\u6211\u4eec\u5c06dy1.c\u4e0edy2.c\u7528\u6765\u521b\u5efa\u52a8\u6001\u5e93<\/p>\n
$ gcc -fPIC -Wall -c dy1.c dy2.c\r\n$ gcc -shared -o libtest.so dy1.o dy2.o<\/pre>\n\u8fd9\u91cc -fPIC \u521b\u5efa\u4e0e\u5730\u5740\u65e0\u5173\u7684\u7f16\u8bd1\u7a0b\u5e8f\uff0c-shared\u6307\u5b9a\u751f\u6210\u52a8\u6001\u94fe\u63a5\u5e93\u3002
\n\u6211\u4eec\u4e5f\u53ef\u4ee5\u4e00\u6b65\u5230\u4f4d<\/p>\n
$ gcc -o libtest.so -fPIC -shared dy1.c dy2.c<\/pre>\n\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u4e0b\u9762\u5df2\u7ecf\u751f\u6210\u4e86\u4e00\u4e2alibtest.so<\/p>\n
$ ls -l<\/pre>\n3.2\u4f7f\u7528\u52a8\u6001\u94fe\u63a5\u5e93<\/strong><\/span><\/div>\n\u5728\u7f16\u8bd1\u7a0b\u5e8f\u65f6\uff0c\u4f7f\u7528\u52a8\u6001\u94fe\u63a5\u5e93\u548c\u9759\u6001\u5e93\u662f\u4e00\u81f4\u7684\uff0c\u4f7f\u7528\u201d-l\u5e93\u540d\u201d\u7684\u65b9\u5f0f\uff0c\u5728\u751f\u6210\u53ef\u6267\u884c\u6587\u4ef6\u7684\u65f6\u5019\u4f1a\u94fe\u63a5\u5e93\u6587\u4ef6\u3002\u4f7f\u7528\u5982\u4e0b\u547d\u4ee4\uff1a<\/p>\n
$ gcc -o test main.c -L. -ltest\r\n$ ls -l<\/pre>\n<\/p>\n
\u8fd9\u91cc -L \u6307\u5b9a\u52a8\u6001\u94fe\u63a5\u5e93\u7684\u8def\u52b2\uff0c-ldtest\u94fe\u63a5\u5e93\u51fd\u6570test \u3002-ltest\u662f\u52a8\u6001\u5e93\u7684\u8c03\u7528\u89c4\u5219\u3002Linux\u7cfb\u7edf\u4e0b\u7684\u52a8\u6001\u5e93\u547d\u540d\u65b9\u5f0f\u662flib*.so,\u800c\u5728\u94fe\u63a5\u65f6\u8868\u793a\u4f4d-l*,*\u662f\u81ea\u5df1\u547d\u540d\u7684\u5e93\u540d\u3002
\n\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u8fd9\u91cc\u5df2\u7ecf\u751f\u6210\u4e86test\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u6211\u4eec\u53ef\u4ee5\u6267\u884c\u4e00\u4e0b\uff1a<\/p>\n
$ .\/test<\/pre>\n\u53ef\u4ee5\u53d1\u73b0\u53d1\u751f\u4e86\u9519\u8bef\uff0c\u8fd9\u662f\u56e0\u4e3a\u7a0b\u5e8f\u8fd0\u884c\u65f6\u6ca1\u6709\u627e\u5230\u52a8\u6001\u94fe\u63a5\u5e93\u9020\u6210\u7684\u3002\u7a0b\u5e8f\u7f16\u8bd1\u65f6\u94fe\u63a5\u52a8\u6001\u5e93\u548c\u8fd0\u884c\u65f6\u4f7f\u7528\u52a8\u6001\u94fe\u63a5\u5e93\u7684\u6982\u5ff5\u662f\u4e0d\u540c\u7684\uff0c\u5728\u8fd0\u884c\u65f6\uff0c\u7a0b\u5e8f\u94fe\u63a5\u7684\u52a8\u6001\u94fe\u63a5\u5e93\u9700\u8981\u5728\u7cfb\u7edf\u76ee\u5f55\u4e0b\u624d\u884c\u3002
\n\u8fd9\u5c31\u5230\u4e86\u52a8\u6001\u5e93\u7684\u8def\u5f84\u95ee\u9898\uff0c\u6709\u4e09\u79cd\u65b9\u6cd5\uff1a<\/p>\n
1\uff09\u628a\u5e93\u62f7\u8d1d\u5230\/usr\/lib\u548c\/lib\u76ee\u5f55\u4e0b\uff1a<\/p>\n
$ sudo cp libtest.so \/lib<\/pre>\n\u3010\u6ce8\u3011\u8fd9\u91cc\u8981\u8d85\u7ea7\u7528\u6237\u6743\u9650sudo
\n\u6211\u4eec\u770b\u4e00\u4e0b\u6267\u884c\u7ed3\u679c\uff1a<\/p>\n
$ .\/test<\/pre>\n\u8fd9\u91cc\u6267\u884c\u7ed3\u679c\u6b63\u786e\u3002<\/p>\n
2\uff09\u5728 LD_LIBRARY_PATH \u73af\u5883\u53d8\u91cf\u4e2d\u52a0\u4e0a\u5e93\u6240\u5728\u8def\u5f84<\/p>\n
$ export LD_LIBRARY_PATH=`pwd`:$LD_LIBRARY_PATH<\/pre>\n\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff1alibtest.so \u7684\u8def\u5f84\u5df2\u7ecf\u5b58\u5728<\/p>\n
$ ldd test<\/pre>\n\u6211\u4eec\u53ef\u4ee5\u770b\u4e00\u4e0b\u7ed3\u679c\uff1a<\/p>\n
<\/p>\n
$ .\/test<\/pre>\n\u4e5f\u80fd\u5f97\u5230\u6b63\u786e\u7ed3\u679c\u3002<\/p>\n
<\/p>\n
3\uff09\u6dfb\u52a0\/etc\/ld.so.conf.d\/*.conf\u6587\u4ef6\u3002\u628a\u5e93\u6240\u5728\u7684\u8def\u5f84\u52a0\u5230\u6587\u4ef6\u672b\u5c3e\uff0c\u5e76\u6267\u884cldconfig\u5237\u65b0\u3002<\/p>\n
$ cd \/etc\/ld.so.conf.d\/\r\n$ ls\r\n$ sudo vi my.conf\r\n$ cat my.conf\r\n\/home\/bruceou\/demo\/libtest.so<\/pre>\n\u5728\/etc\/ld.so.conf.d\/\u4e0b\u5efa\u7acbmy.conf \u91cc\u9762\u53ea\u6709\u4e00\u53e5\/home\/bruceou\/demo\/libtest.so\u5373libtest.so\u7684\u8def\u5f84\uff0c\u7136\u540e\u6267\u884cldconfig\u5237\u65b0\u5373\u53ef\u3002<\/p>\n
3.3\u52a8\u6001\u5e93\u603b\u7ed3<\/strong><\/span><\/div>\n1.\u7f16\u8bd1\u52a8\u6001\u5e93
\n\u65b9\u6cd5\u4e00\uff1a
\n\u547d\u4ee4\uff1a<\/p>\n
gcc -fPIC -Wall -c .c<\/pre>\n\u547d\u4ee4\uff1a<\/p>\n
gcc -shared -o lib.so .o<\/pre>\n\u65b9\u6cd5\u4e8c\uff1a
\n\u547d\u4ee4\uff1a<\/p>\n
gcc .c .c .c -fPIC -shared -o lib.so<\/pre>\n2.\u5b89\u88c5\u8def\u5f84
\n\u547d\u4ee4\uff1a<\/p>\n
cp lib.so \/usr\/lib\/lib.so\/\/\u7ba1\u7406\u5458\u8eab\u4efd<\/pre>\n3.\u52a8\u6001\u5e93\u7684\u94fe\u63a5
\n\u547d\u4ee4\uff1a<\/p>\n
gcc -o .c -L. -l<\/pre>\n\u6216\u8005\uff1a<\/p>\n
\u547d\u4ee4\uff1agcc .c -L. -l -o<\/pre>\n4.\u8fd0\u884c\u53ef\u6267\u884c\u7a0b\u5e8f<\/p>\n","protected":false},"excerpt":{"rendered":"
\u9759\u6001\u5e93\uff08static library, Statically-linked library, LIB\uff09\u6216\u79f0\u9759\u6001 […]<\/p>\n","protected":false},"author":310,"featured_media":261625,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-261589","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/261589","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/310"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=261589"}],"version-history":[{"count":16,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/261589\/revisions"}],"predecessor-version":[{"id":261761,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/261589\/revisions\/261761"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/261625"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=261589"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=261589"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=261589"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}