{"id":266847,"date":"2023-03-10T22:11:39","date_gmt":"2023-03-10T14:11:39","guid":{"rendered":"https:\/\/lrxjmw.cn\/ljhyxcxscsyj.html"},"modified":"2023-03-10T22:11:39","modified_gmt":"2023-03-10T14:11:39","slug":"ljhyxcxscsyj","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/ljhyxcxscsyj.html","title":{"rendered":"\u8fde\u63a5\u548c\u8fd0\u884c\u7a0b\u5e8f\u8f93\u51fa\u5b9e\u9a8c\u89e3\u6790\u4f7f\u7528C\u8bed\u8a00\u7f16\u8bd1\u5668gcc\u8f6c\u6362\u4e3a\u53ef\u6267\u884c\u6587\u4ef6hello"},"content":{"rendered":"

HelloWorld\u6e90\u4ee3\u7801<\/p>\n

\n

#include <\/span>\n#include <\/span>\nint<\/span> main()\n{\n    printf<\/span>(\"Hello Worldn\"<\/span>);\n    exit<\/span>(0<\/span>);\n}<\/code><\/pre>\n<\/p>\n

\u7f16\u8bd1\u3001\u8fde\u63a5\u548c\u8fd0\u884c\u7a0b\u5e8f<\/p>\n

\n

$ <\/span>gcc -o hello hello.c\n$ <\/span>.\/hello<\/code><\/pre>\n<\/p>\n

\u8f93\u51fa<\/p>\n

\n

Hello World<\/code><\/pre>\n<\/p>\n

\u5b9e\u9a8c\u89e3\u6790<\/p>\n

\u4f7f\u7528C\u8bed\u8a00\u7f16\u8bd1\u5668gcc\u5c06C\u8bed\u8a00\u6e90\u4ee3\u7801\u8f6c\u6362\u4e3a\u53ef\u6267\u884c\u6587\u4ef6hello\u3002<\/p>\n

\u5e94\u7528\u7a0b\u5e8f\u7ba1\u7406\u6807\u51c6\u4f4d\u7f6e<\/p>\n

\/usr\/bin<\/p>\n

\/usr\/local\/bin<\/p>\n

\/opt<\/p>\n

\u5934\u6587\u4ef6<\/p>\n

\u4f7f\u7528\u5934\u6587\u4ef6\u6765\u63d0\u4f9b\u5bf9\u5e38\u91cf\u7684\u5b9a\u4e49\u548c\u5bf9\u7cfb\u7edf<\/a>\u51fd\u6570\u53ca\u5e93\u51fd\u6570\u8c03\u7528\u7684\u7533\u660e\u3002<\/p>\n

\u6807\u51c6\u4f4d\u7f6e<\/p>\n

\/usr\/include\u76ee\u5f55\u53ca\u5176\u5b50\u76ee\u5f55<\/p>\n

\u5305\u542b\u975e\u6807\u51c6\u4f4d\u7f6e\u5934\u6587\u4ef6<\/p>\n

\n

$ <\/span>gcc -I<\/span>\/usr\/linux\/include<\/span> hello.c<\/code><\/pre>\n<\/p>\n

\u641c\u7d22\u5305\u542b\u7279\u5b9a\u5b9a\u4e49\u548c\u51fd\u6570\u539f\u578b\u7684\u5934\u6587\u4ef6<\/p>\n

\n

$ <\/span>grep EXIT_<\/span> *.h<\/code><\/pre>\n<\/p>\n

\u5e93\u6587\u4ef6<\/p>\n

\u5e93\u662f\u4e00\u7ec4\u9884\u5148\u7f16\u8bd1\u597d\u7684\u51fd\u6570\u7684\u96c6\u5408\u5728linux\u4e0b\u7531o\u6587\u4ef6\u7f16\u8bd1\u751f\u6210cgi\u6587\u4ef6<\/strong>\u5728linux\u4e0b\u7531o\u6587\u4ef6\u7f16\u8bd1\u751f\u6210cgi\u6587\u4ef6<\/strong>\uff0c\u8fd9\u79cd\u51fd\u6570\u90fd\u662f\u6839\u636e\u53ef\u91cd\u7528\u7684\u539f\u5219\u7f16\u64b0\u7684\u3002<\/p>\n

\u6807\u51c6\u4f4d\u7f6e<\/p>\n

\n

\/lib<\/span>\u548c\/usr\/lib<\/span><\/code><\/pre>\n<\/p>\n

\u9ed8\u8ba4\u60c5\u51b5\u4e0blinux\u547d\u4ee4vi\uff0cC\u8bed\u8a00\u7f16\u8bd1\u5668\uff08\u8054\u63a5\u7a0b\u5e8f\uff09\u4ec5\u4ec5\u641c\u7d22C\u8bed\u8a00\u5e93\u3002\u867d\u7136\u628a\u5e93\u6587\u4ef6\u7f6e\u4e8e\u6807\u51c6\u76ee\u5f55\u4e2d\uff0c\u8fd8\u662f\u987b\u8981\u5728\u547d\u4ee4<\/a>\u884c\u4e2d\u660e\u6670\u6307\u660e\u3002<\/p>\n

\u547d\u540d\u89c4\u8303<\/p>\n

lib+\u5e93\u540d+.+\u5e93\u7c7b\u578b\uff08a\/o\uff09<\/p>\n

.a\u4ee3\u8868\u4f20\u7edf\u7684\u9759\u6001\u51fd\u6570\u5e93<\/p>\n

.so\u4ee3\u8868\u5171\u4eab\u51fd\u6570\u5e93<\/p>\n

\u6307\u5b9a\u5e93\u6587\u4ef6<\/p>\n

\n

$ gcc -o hello hello.c<\/span> \/usr\/lib\/libm.a<\/span>\n\u6216\u8005\n$ gcc -o hello hello.c<\/span> -lm<\/code><\/pre>\n<\/p>\n

\u5305\u542b\u975e\u6807\u51c6\u4f4d\u7f6e\u5e93\u6587\u4ef6<\/p>\n

\n

$ gcc -o<\/span> hello -L<\/span>\/usr\/linux\/lib hello.<\/span>c -lm<\/span><\/code><\/pre>\n<\/p>\n

\u4f7f\u7528\/usr\/linux\/lib\u76ee\u5f55\u4e2d\u7684libm\u5e93\u7248\u672c\u6765\u7f16\u8bd1\u548c\u94fe\u63a5\u7a0b\u5e8fhello\u3002<\/p>\n

\u9759\u6001\u5e93\u51fd\u6570\u6e90\u6587\u4ef6bill.c\u548cfred.c<\/p>\n

\n

#include <\/span>\nvoid<\/span> bill(char<\/span> *arg)\n{\n    printf<\/span>(\"bill: you passed %sn\"<\/span>, arg);\n}<\/code><\/pre>\n<\/p>\n

\n

#include <\/span>\nvoid<\/span> fred(int<\/span> arg)\n{\n    printf<\/span>(\"fred: you passed %dn\"<\/span>, arg);\n}<\/code><\/pre>\n<\/p>\n

\u751f\u6210\u76ee\u6807\u6587\u4ef6\uff08\u5373\u5c06\u5305\u542b\u5728\u5e93\u6587\u4ef6\u4e2d\uff09<\/p>\n

\u4f7f\u7528-c\u9009\u9879\uff0c\u5236\u6b62\u7f16\u8bd1\u5668\u521b\u5efa\u4e00\u4e2a\u5b8c\u6210\u7684\u7a0b\u5e8f<\/p>\n

\n

$ gcc -c bill.c<\/span> fred.c<\/span><\/code><\/pre>\n<\/p>\n

\u5934\u6587\u4ef6lib.h<\/p>\n

\n

\/*\n    This is lib.h. It declares the functions fred and bill for users\n*\/<\/span>\nvoid<\/span> bill(char<\/span> *);\nvoid<\/span> fred(int<\/span>);<\/code><\/pre>\n<\/p>\n

\u8c03\u7528\u7a0b\u5e8fprogram.c<\/p>\n

\n

#include <\/span>\n#include \"lib.h\"<\/span>\nint<\/span> main()\n{\n    bill(\"Hello World\"<\/span>);\n    exit(0<\/span>);\n}<\/code><\/pre>\n<\/p>\n

\u7f16\u8bd1\u548c\u6d4b\u8bd5\uff08\u663e\u5f0f\u6307\u5b9a\u76ee\u6807\u6587\u4ef6\uff09<\/p>\n

\u7f16\u8bd1\u4f60\u7684\u6587\u4ef6\u5e76\u5c06\u5176\u4e0e\u539f\u672c\u7f16\u8bd1\u597d\u7684\u76ee\u6807\u6a21\u5757bill.o\u94fe\u63a5\u3002<\/p>\n

linux \u7f16\u8bd1\u751f\u6210so\u6587\u4ef6_\u5728linux\u4e0b\u7531o\u6587\u4ef6\u7f16\u8bd1\u751f\u6210cgi\u6587\u4ef6_vs \u7f16\u8bd1\u751f\u6210\u7c7bxml\u6587\u4ef6<\/p>\n

\n

$ gcc -c program<\/span>.c\n$ gcc -o program<\/span> program<\/span>.o bill.o\n$ .\/program<\/span><\/code><\/pre>\n<\/p>\n

\u8f93\u51fa<\/p>\n

\n

Bill<\/span>: we passed Hello World<\/span><\/code><\/pre>\n<\/p>\n

\u521b\u5efa\u5e93\u6587\u4ef6<\/p>\n

\u4f7f\u7528ar\u7a0b\u5e8f\u521b\u5efa\u4e00\u4e2a\u5f52\u6863\u6587\u4ef6\u5e76\u5c06\u4f60\u7684\u76ee\u6807\u6587\u4ef6\u6dfb\u52a0\u8fdb\u53bb\u3002<\/p>\n

\n

$ ar crv libfoo.a<\/span> bill.o<\/span> fred.o<\/span><\/code><\/pre>\n<\/p>\n

\u7f16\u8bd1\u548c\u6d4b\u8bd5\uff08\u4f7f\u7528\u5e93\u6587\u4ef6\uff09<\/p>\n

\n

$ gcc -o program<\/span> program<\/span>.o libfoo.a\n$ .\/program<\/span><\/code><\/pre>\n<\/p>\n

\u67e5\u770b\u4ec0\u4e48\u51fd\u6570\u88ab\u5305\u542b\u5728\u76ee\u6807\u6587\u4ef6\u3001\u51fd\u6570\u5e93\u6216\u53ef\u6267\u884c\u6587\u4ef6\u91cc<\/p>\n

nm\u547d\u4ee4\uff1a\u663e\u793a\u4e8c\u8865\u7801\u76ee\u6807\u6587\u4ef6\u7684\u7b26\u53f7\u8868<\/p>\n

\n

$ <\/span>nm program |grep T<\/span><\/code><\/pre>\n<\/p>\n

\u8f93\u51fa<\/p>\n

\n

08048466<\/span> T<\/span> bill\n080484f4 T<\/span> _fini\n0804a000 d _GLOBAL_OFFSET_TABLE_\n080482cc T<\/span> _init\n         w _ITM_deregisterTMCloneTable\n         w _ITM_registerTMCloneTable\n08049f10 d __JCR_LIST__\n080484f0 T<\/span> __libc_csu_fini\n08048490<\/span> T<\/span> __libc_csu_init\n0804843b T<\/span> main\n08048340<\/span> T<\/span> _start\n0804a020 D __TMC_END__\n08048370<\/span> T<\/span> __x86.get_pc_thunk.bx<\/code><\/pre>\n<\/p>\n

\u5171\u4eab\u5e93\u9759\u6001\u5e93\u7684\u4e00\u4e2a\u7f3a\u70b9<\/p>\n

\u5f53\u4f60\u540c\u65f6\u8fd0\u884c\u8bb8\u591a\u5e94\u7528\u7a0b\u5e8f\u800c\u4e14\u5b83\u4eec\u90fd\u4f7f\u7528\u6765\u81ea\u540c\u4e00\u4e2a\u51fd\u6570\u5e93\u7684\u51fd\u6570\u65f6\uff0c\u663e\u5b58\u4e2d\u90fd\u4f1a\u6709\u540c\u4e00\u51fd\u6570\u7684\u591a\u4efd\u526f\u672c\uff0c\u8fd9\u5c06\u6d88\u8017\u5927\u91cf\u5b9d\u8d35\u7684\u663e\u5b58\u548cc\u76d8\u7a7a\u95f4\u3002<\/p>\n

\u5f53\u4e00\u4e2a\u7a0b\u5e8f\u4f7f\u7528\u5171\u4eab\u5e93\u65f6\uff0c\u5b83\u7684\u94fe\u63a5\u5f62\u5f0f\u662f\u8fd9\u6837\u7684\uff1a\u7a0b\u5e8f\u672c\u8eab\u4e0d\u518d\u5305\u542b\u51fd\u6570\u4ee3\u7801\uff0c\u800c\u662f\u5f15\u7528\u8fd0\u884c\u65f6\u53ef\u8bbf\u95ee\u7684\u5171\u4eab\u4ee3\u7801\u3002\u5f53\u7f16\u8bd1\u597d\u7684\u7a0b\u5e8f\u88ab\u88c5\u8f7d\u5230\u663e\u5b58\u4e2d\u6267\u884c\u65f6linux\u64cd\u4f5c\u7cfb\u7edf\u4e0b\u8f7d\uff0c\u51fd\u6570\u5f15\u7528\u88ab\u89e3\u91ca\u5e76\u5f62\u6210\u5bf9\u5171\u4eab\u5e93\u7684\u8c03\u7528\uff0c\u5047\u5982\u6709\u5fc5\u8981\uff0c\u5171\u4eab\u5e93\u624d\u88ab\u52a0\u8f7d\u5230\u663e\u5b58\u4e2d\u3002<\/p>\n

\u5bf9Linux<\/a>\u7cfb\u7edf\u6765\u8bf4\uff0c\u8d1f\u8d23\u88c5\u8f7d\u5171\u4eab\u5e93\u5e76\u89e3\u6790\u987e\u5ba2\u7a0b\u5e8f\u51fd\u6570\u5f15\u7528\u7684\u7a0b\u5e8f\uff08\u52a8\u6001\u52a0\u8f7d\u5668\uff09\u662fld.so\u3002<\/p>\n

\u5171\u4eab\u5e93\u7684<\/p>\n

\/etc\/ld.so.conf<\/p>\n

\u67e5\u770b\u7a0b\u5e8f\u6240\u987b\u8981\u7684\u5171\u4eab\u5e93<\/p>\n

\n

$ <\/span>ldd program<\/code><\/pre>\n<\/p>\n

\u8f93\u51fa<\/p>\n

\n

    linux-gate.so<\/span>.1<\/span> =>  (0xb771c000<\/span>)\n    libc.so<\/span>.6<\/span> => \/lib\/i386-linux-gnu\/libc.so<\/span>.6<\/span> (0xb754b000<\/span>)\n    \/lib\/ld<\/span>-linux.so<\/span>.2<\/span> (0x8007d000<\/span>)<\/code><\/pre>\n<\/p>\n

\u2014\u2014\u300aLinux\u7a0b\u5e8f\u8bbe\u8ba1\u7b2c\u56db\u7248\u300b<\/p>\n","protected":false},"excerpt":{"rendered":"

\u4f7f\u7528C\u8bed\u8a00\u7f16\u8bd1\u5668gcc\u5c06C\u8bed\u8a00\u6e90\u4ee3\u7801\u8f6c\u6362\u4e3a\u53ef\u6267\u884c\u6587\u4ef6hello\u3002\u4f7f\u7528\/usr\/linux\/lib\u76ee\u5f55\u4e2d\u7684libm\u5e93\u7248\u672c\u6765\u7f16\u8bd1\u548c\u94fe\u63a5\u7a0b\u5e8fhello\u3002\u4f7f\u7528-c\u9009\u9879\uff0c\u963b\u6b62\u7f16\u8bd1\u5668\u521b\u5efa\u4e00\u4e2a\u5b8c\u6210\u7684\u7a0b\u5e8f\u7f16\u8bd1\u548c\u6d4b\u8bd5\uff08\u663e\u5f0f\u6307\u5b9a\u76ee\u6807\u6587\u4ef6\uff09\u7f16\u8bd1\u4f60\u7684\u6587\u4ef6\u5e76\u5c06\u5176\u4e0e\u5148\u524d\u7f16\u8bd1\u597d\u7684\u76ee\u6807\u6a21\u5757bill.\u4f7f\u7528ar\u7a0b\u5e8f\u521b\u5efa\u4e00\u4e2a\u5f52\u6863\u6587\u4ef6\u5e76\u5c06\u4f60\u7684\u76ee\u6807\u6587\u4ef6\u6dfb\u52a0\u8fdb\u53bb\u3002\u7f16\u8bd1\u548c\u6d4b\u8bd5\uff08\u4f7f\u7528\u5e93\u6587\u4ef6\uff09<\/p>\n","protected":false},"author":1,"featured_media":266848,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[988],"tags":[999],"class_list":["post-266847","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tougao","tag-999"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/266847","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\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=266847"}],"version-history":[{"count":0,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/266847\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/266848"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=266847"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=266847"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=266847"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}