{"id":43359,"date":"2025-06-12T08:20:55","date_gmt":"2025-06-12T00:20:55","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/43359.html"},"modified":"2025-06-12T08:20:55","modified_gmt":"2025-06-12T00:20:55","slug":"linux%e7%8e%af%e5%a2%83%e4%b8%8bc%e7%bc%96%e7%a8%8b%e7%bb%bc%e5%90%88%e6%95%99%e7%a8%8b%ef%bc%9a%e5%9f%ba%e7%a1%80%e3%80%81%e6%9c%8d%e5%8a%a1%e5%99%a8%e4%b8%8e%e4%b8%93%e9%a2%98%e6%b7%b1%e5%85%a5","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/43359.html","title":{"rendered":"Linux\u73af\u5883\u4e0bC++\u7f16\u7a0b\u7efc\u5408\u6559\u7a0b\uff1a\u57fa\u7840\u3001\u670d\u52a1\u5668\u4e0e\u4e13\u9898\u6df1\u5165"},"content":{"rendered":"<p>  \u672c\u6587\u8fd8\u6709\u914d\u5957\u7684\u7cbe\u54c1\u8d44\u6e90&#xff0c;\u70b9\u51fb\u83b7\u53d6  <img decoding=\"async\" alt=\"menu-r.4af5f7ec.gif\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250612002053-684a1d65d7153.gif\" style=\"width:16px;margin-left:4px;vertical-align:text-bottom;cursor:text\" \/> <\/p>\n<p> \u7b80\u4ecb&#xff1a;\u672c\u6559\u7a0b\u5408\u96c6\u8be6\u7ec6\u8bb2\u89e3\u4e86\u5728Linux\u73af\u5883\u4e0b\u8fdb\u884cC&#043;&#043;\u7f16\u7a0b\u7684\u5404\u4e2a\u65b9\u9762\u3002\u4ece\u57fa\u7840\u8bed\u6cd5\u3001\u670d\u52a1\u5668\u5f00\u53d1\u5230\u9ad8\u7ea7\u4e13\u9898&#xff0c;\u6559\u7a0b\u5e2e\u52a9\u7a0b\u5e8f\u5458\u4eec\u5728Linux\u7cfb\u7edf\u4e0a\u6709\u6548\u638c\u63e1C&#043;&#043;\u5f00\u53d1\u3002\u5185\u5bb9\u5305\u62ecC&#043;&#043;\u57fa\u7840\u6982\u5ff5\u3001GCC\u7f16\u8bd1\u8c03\u8bd5\u3001\u5185\u5b58\u7ba1\u7406\u3001socket\u7f16\u7a0b\u3001\u591a\u7ebf\u7a0b\u4e0e\u5f02\u6b65I\/O\u6a21\u578b\u3001STL\u5e94\u7528\u3001\u6a21\u677f\u5143\u7f16\u7a0b\u3001\u73b0\u4ee3C&#043;&#043;\u65b0\u7279\u6027\u3001\u7248\u672c\u63a7\u5236&#xff08;\u5982Git&#xff09;\u3001\u9879\u76ee\u6784\u5efa\u5de5\u5177&#xff08;\u5982Makefile\/CMake&#xff09;\u4ee5\u53ca\u8c03\u8bd5\u6280\u5de7\u548c\u6027\u80fd\u5206\u6790\u5de5\u5177&#xff08;\u5982GDB\u3001gprof\u548cvalgrind&#xff09;\u3002\u901a\u8fc7\u5b9e\u8df5\u4e0e\u7406\u8bba\u5b66\u4e60\u7684\u7ed3\u5408&#xff0c;\u672c\u6559\u7a0b\u65e8\u5728\u63d0\u4f9b\u7ed9\u521d\u5b66\u8005\u53ca\u6709\u7ecf\u9a8c\u7684\u5f00\u53d1\u8005\u5168\u9762\u800c\u6df1\u5165\u7684\u5b66\u4e60\u8d44\u6e90\u3002 <img decoding=\"async\" alt=\"\u57fa\u4e8eLinux\u7684C&#043;&#043; \u6559\u7a0b\u5408\u96c6&#xff0c; \u5305\u62ecC&#043;&#043;\u57fa\u7840&#xff0c; C&#043;&#043;\u670d\u52a1\u5668&#xff0c; C&#043;&#043;\u4e13\u9898\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250612002053-684a1d65ded95.png\" \/> <\/p>\n<h2> 1. C&#043;&#043;\u57fa\u7840\u8bed\u6cd5\u548c\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b <\/h2>\n<h3> 1.1 C&#043;&#043;\u7f16\u7a0b\u7b80\u4ecb <\/h3>\n<p> C&#043;&#043;\u662f\u4e00\u79cd\u5e7f\u6cdb\u4f7f\u7528\u7684\u7f16\u7a0b\u8bed\u8a00&#xff0c;\u56e0\u5176\u6027\u80fd\u4f18\u5f02&#xff0c;\u7279\u522b\u662f\u5728\u7cfb\u7edf\u7f16\u7a0b\u3001\u6e38\u620f\u5f00\u53d1\u3001\u5d4c\u5165\u5f0f\u7cfb\u7edf\u7b49\u9886\u57df\u5907\u53d7\u9752\u7750\u3002\u5b83\u5141\u8bb8\u5f00\u53d1\u8005\u901a\u8fc7\u9762\u5411\u5bf9\u8c61\u7684\u65b9\u5f0f\u5bf9\u4ee3\u7801\u8fdb\u884c\u5c01\u88c5\u3001\u7ee7\u627f\u548c\u591a\u6001\u5904\u7406&#xff0c;\u6781\u5927\u63d0\u5347\u4e86\u4ee3\u7801\u7684\u53ef\u91cd\u7528\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002 <\/p>\n<h3> 1.2 C&#043;&#043;\u57fa\u7840\u8bed\u6cd5\u6982\u89c8 <\/h3>\n<p> C&#043;&#043;\u7684\u57fa\u7840\u8bed\u6cd5\u5305\u542b\u6570\u636e\u7c7b\u578b\u3001\u8fd0\u7b97\u7b26\u3001\u63a7\u5236\u7ed3\u6784\u7b49\u3002\u638c\u63e1\u8fd9\u4e9b\u57fa\u7840\u77e5\u8bc6\u662f\u8fdb\u884c\u66f4\u590d\u6742\u7f16\u7a0b\u7684\u524d\u63d0\u3002\u6570\u636e\u7c7b\u578b\u5982\u6574\u578b\u3001\u6d6e\u70b9\u578b\u3001\u5b57\u7b26\u578b\u548c\u5e03\u5c14\u578b\u662f\u6784\u5efa\u7a0b\u5e8f\u7684\u57fa\u672c\u5143\u7d20&#xff0c;\u800c\u63a7\u5236\u7ed3\u6784\u5982\u6761\u4ef6\u8bed\u53e5\u548c\u5faa\u73af\u8bed\u53e5\u5219\u662f\u7a0b\u5e8f\u6d41\u7a0b\u63a7\u5236\u7684\u5173\u952e\u3002 <\/p>\n<h3> 1.3 \u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u57fa\u7840 <\/h3>\n<p> \u9762\u5411\u5bf9\u8c61\u7f16\u7a0b&#xff08;OOP&#xff09;\u662fC&#043;&#043;\u7684\u6838\u5fc3\u7279\u6027\u4e4b\u4e00&#xff0c;\u5b83\u901a\u8fc7\u7c7b\u548c\u5bf9\u8c61\u7684\u6982\u5ff5\u6765\u7ec4\u7ec7\u4ee3\u7801\u3002\u7406\u89e3\u7c7b&#xff08;class&#xff09;\u7684\u5b9a\u4e49\u3001\u5bf9\u8c61\u7684\u521b\u5efa\u3001\u6210\u5458\u53d8\u91cf\u548c\u6210\u5458\u51fd\u6570\u7684\u4f7f\u7528\u662f\u5b66\u4e60OOP\u7684\u57fa\u7840\u3002\u7ee7\u627f\u548c\u591a\u6001\u7b49\u7279\u6027\u8fdb\u4e00\u6b65\u589e\u5f3a\u4e86\u4ee3\u7801\u7684\u62bd\u8c61\u80fd\u529b\u548c\u590d\u7528\u6027\u3002 <\/p>\n<p>\/\/ \u793a\u4f8b&#xff1a;\u5b9a\u4e49\u4e00\u4e2a\u7b80\u5355\u7684\u7c7b<br \/>\nclass Example {<br \/>\npublic:<br \/>\n    \/\/ \u6784\u9020\u51fd\u6570<br \/>\n    Example() {<br \/>\n        \/\/ &#8230;<br \/>\n    }<\/p>\n<p>    \/\/ \u6210\u5458\u51fd\u6570<br \/>\n    void function() {<br \/>\n        \/\/ &#8230;<br \/>\n    }<br \/>\n};<\/p>\n<p> \u4e0a\u8ff0\u4ee3\u7801\u6bb5\u5c55\u793a\u4e86\u5982\u4f55\u5728C&#043;&#043;\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u7c7b&#xff0c;\u5e76\u5728\u5176\u4e2d\u5305\u542b\u4e86\u4e00\u4e2a\u6784\u9020\u51fd\u6570\u548c\u4e00\u4e2a\u6210\u5458\u51fd\u6570\u3002\u901a\u8fc7\u8fd9\u4e2a\u4f8b\u5b50&#xff0c;\u6211\u4eec\u53ef\u4ee5\u5f00\u59cb\u63a2\u7d22C&#043;&#043;\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u7684\u4e16\u754c\u3002 <\/p>\n<h2> 2. GCC\u7f16\u8bd1\u5668\u7684\u4f7f\u7528\u548c\u8c03\u8bd5\u6280\u5de7 <\/h2>\n<h3> 2.1 GCC\u7f16\u8bd1\u5668\u7684\u5b89\u88c5\u4e0e\u914d\u7f6e <\/h3>\n<p> GCC\u7f16\u8bd1\u5668\u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u7f16\u8bd1\u5668\u96c6\u5408&#xff0c;\u5b83\u652f\u6301\u5305\u62ecC&#043;&#043;\u5728\u5185\u7684\u591a\u79cd\u7f16\u7a0b\u8bed\u8a00\u3002\u5728Linux\u7cfb\u7edf\u4e2d&#xff0c;GCC\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7\u5305\u7ba1\u7406\u5668\u6765\u5b89\u88c5\u3002\u4f8b\u5982&#xff0c;\u5728\u57fa\u4e8eDebian\u7684\u7cfb\u7edf\u4e2d&#xff0c;\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5GCC&#xff1a; <\/p>\n<p>sudo apt-get update<br \/>\nsudo apt-get install build-essential<\/p>\n<p> \u5b89\u88c5\u5b8c\u6210\u540e&#xff0c;\u9a8c\u8bc1GCC\u7248\u672c&#xff1a; <\/p>\n<p>gcc &#8211;version<\/p>\n<h3> 2.2 GCC\u7f16\u8bd1\u8fc7\u7a0b\u8be6\u89e3 <\/h3>\n<p> GCC\u7f16\u8bd1\u5668\u5c06\u6e90\u4ee3\u7801\u7f16\u8bd1\u6210\u673a\u5668\u7801\u7684\u8fc7\u7a0b\u901a\u5e38\u5305\u62ec\u56db\u4e2a\u6b65\u9aa4&#xff1a;\u9884\u5904\u7406&#xff08;Preprocessing&#xff09;\u3001\u7f16\u8bd1&#xff08;Compilation&#xff09;\u3001\u6c47\u7f16&#xff08;Assembly&#xff09;\u548c\u94fe\u63a5&#xff08;Linking&#xff09;\u3002\u4ee5\u4e0b\u662f\u6bcf\u4e2a\u6b65\u9aa4\u7684\u89e3\u91ca&#xff1a; <\/p>\n<h4> 2.2.1 \u9884\u5904\u7406 <\/h4>\n<p> \u9884\u5904\u7406\u9636\u6bb5\u4e3b\u8981\u662f\u5904\u7406\u6e90\u4ee3\u7801\u6587\u4ef6\u4e2d\u7684\u9884\u5904\u7406\u6307\u4ee4&#xff0c;\u6bd4\u5982\u5b8f\u5b9a\u4e49  #define  \u3001\u6587\u4ef6\u5305\u542b  #include  \u4ee5\u53ca\u6761\u4ef6\u7f16\u8bd1\u6307\u4ee4\u3002\u4f7f\u7528  -E  \u9009\u9879\u53ef\u4ee5\u4ec5\u6267\u884c\u9884\u5904\u7406\u6b65\u9aa4&#xff1a; <\/p>\n<p>gcc -E hello.c -o hello.i<\/p>\n<h4> 2.2.2 \u7f16\u8bd1 <\/h4>\n<p> \u7f16\u8bd1\u9636\u6bb5\u5c06\u9884\u5904\u7406\u540e\u7684\u6587\u4ef6\u8f6c\u6362\u4e3a\u6c47\u7f16\u4ee3\u7801\u3002\u4f7f\u7528  -S  \u9009\u9879\u53ef\u4ee5\u4ec5\u6267\u884c\u7f16\u8bd1\u6b65\u9aa4&#xff1a; <\/p>\n<p>gcc -S hello.i -o hello.s<\/p>\n<h4> 2.2.3 \u6c47\u7f16 <\/h4>\n<p> \u6c47\u7f16\u9636\u6bb5\u5c06\u6c47\u7f16\u4ee3\u7801\u8f6c\u6362\u4e3a\u673a\u5668\u8bed\u8a00&#xff0c;\u751f\u6210\u76ee\u6807\u6587\u4ef6\u3002\u4f7f\u7528  -c  \u9009\u9879\u53ef\u4ee5\u4ec5\u6267\u884c\u6c47\u7f16\u6b65\u9aa4&#xff1a; <\/p>\n<p>gcc -c hello.s -o hello.o<\/p>\n<h4> 2.2.4 \u94fe\u63a5 <\/h4>\n<p> \u94fe\u63a5\u9636\u6bb5\u5c06\u4e00\u4e2a\u6216\u591a\u4e2a\u76ee\u6807\u6587\u4ef6\u5408\u5e76\u6210\u4e00\u4e2a\u5355\u72ec\u7684\u53ef\u6267\u884c\u6587\u4ef6\u3002\u4f7f\u7528\u65e0\u9009\u9879\u76f4\u63a5\u7f16\u8bd1\u4e00\u4e2a\u5b8c\u6574\u7684\u7a0b\u5e8f&#xff1a; <\/p>\n<p>gcc hello.c -o hello<\/p>\n<h3> 2.3 GCC\u7f16\u8bd1\u9009\u9879 <\/h3>\n<p> GCC\u63d0\u4f9b\u4e86\u5927\u91cf\u7684\u7f16\u8bd1\u9009\u9879&#xff0c;\u5141\u8bb8\u5f00\u53d1\u8005\u5bf9\u7f16\u8bd1\u8fc7\u7a0b\u8fdb\u884c\u8be6\u7ec6\u7684\u63a7\u5236\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u7f16\u8bd1\u9009\u9879&#xff1a; <\/p>\n<ul>\n<li>  -Wall  &#xff1a;\u663e\u793a\u6240\u6709\u8b66\u544a\u4fe1\u606f\u3002 <\/li>\n<li>  -g  &#xff1a;\u751f\u6210\u8c03\u8bd5\u4fe1\u606f&#xff0c;\u8fd9\u5bf9\u4e8e\u4f7f\u7528GDB\u7b49\u8c03\u8bd5\u5668\u662f\u5fc5\u9700\u7684\u3002 <\/li>\n<li>  -O2  &#xff1a;\u4f18\u5316\u4ee3\u7801&#xff0c;\u63d0\u9ad8\u6267\u884c\u6548\u7387\u3002 <\/li>\n<li>  -std&#061;c&#043;&#043;11  &#xff1a;\u542f\u7528C&#043;&#043;11\u6807\u51c6\u3002 <\/li>\n<\/ul>\n<h3> 2.4 GDB\u8c03\u8bd5\u5668\u7684\u4f7f\u7528 <\/h3>\n<p> GDB&#xff08;GNU Debugger&#xff09;\u662fGCC\u7684\u597d\u4f34\u4fa3&#xff0c;\u5b83\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u8c03\u8bd5\u5de5\u5177\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528GDB\u6765\u8c03\u8bd5\u7a0b\u5e8f\u7684\u57fa\u672c\u6b65\u9aa4&#xff1a; <\/p>\n<h4> 2.4.1 \u542f\u52a8GDB <\/h4>\n<p> \u5728\u7f16\u8bd1\u7a0b\u5e8f\u65f6&#xff0c;\u9700\u8981\u6dfb\u52a0  -g  \u9009\u9879\u4ee5\u751f\u6210\u8c03\u8bd5\u4fe1\u606f&#xff1a; <\/p>\n<p>gcc -g -o my_program my_program.c<\/p>\n<p> \u7136\u540e\u4f7f\u7528GDB\u542f\u52a8\u8c03\u8bd5&#xff1a; <\/p>\n<p>gdb .\/my_program<\/p>\n<h4> 2.4.2 \u8bbe\u7f6e\u65ad\u70b9 <\/h4>\n<p> \u5728GDB\u4e2d\u8bbe\u7f6e\u65ad\u70b9\u4ee5\u4fbf\u7a0b\u5e8f\u5728\u6307\u5b9a\u7684\u884c\u6216\u51fd\u6570\u5904\u6682\u505c\u6267\u884c&#xff1a; <\/p>\n<p>(gdb) break main<br \/>\n(gdb) break 10<\/p>\n<h4> 2.4.3 \u8fd0\u884c\u7a0b\u5e8f <\/h4>\n<p> \u8fd0\u884c\u7a0b\u5e8f\u5e76\u4f20\u5165\u6240\u9700\u7684\u53c2\u6570&#xff1a; <\/p>\n<p>(gdb) run [args]<\/p>\n<h4> 2.4.4 \u5355\u6b65\u6267\u884c <\/h4>\n<p> \u5355\u6b65\u6267\u884c\u7a0b\u5e8f&#xff0c;\u89c2\u5bdf\u53d8\u91cf\u7684\u53d8\u5316&#xff1a; <\/p>\n<p>(gdb) step<br \/>\n(gdb) next<\/p>\n<h4> 2.4.5 \u67e5\u770b\u548c\u4fee\u6539\u53d8\u91cf <\/h4>\n<p> \u67e5\u770b\u548c\u4fee\u6539\u7a0b\u5e8f\u8fd0\u884c\u65f6\u7684\u53d8\u91cf&#xff1a; <\/p>\n<p>(gdb) print variable_name<br \/>\n(gdb) set variable_name &#061; value<\/p>\n<h4> 2.4.6 \u7ee7\u7eed\u6267\u884c\u548c\u9000\u51faGDB <\/h4>\n<p> \u5728\u65ad\u70b9\u5904\u505c\u6b62\u540e&#xff0c;\u53ef\u4ee5\u4f7f\u7528  continue  \u547d\u4ee4\u7ee7\u7eed\u6267\u884c\u7a0b\u5e8f&#xff0c;\u6216\u8005\u4f7f\u7528  quit  \u547d\u4ee4\u9000\u51faGDB&#xff1a; <\/p>\n<p>(gdb) continue<br \/>\n(gdb) quit<\/p>\n<h4> 2.4.7 GDB\u5e38\u7528\u547d\u4ee4\u6982\u89c8 <\/h4>\n<p> | \u547d\u4ee4 | \u8bf4\u660e | | &#8212;&#8212;&#8212;&#8211; | &#8212;&#8212;&#8212;&#8212;&#8211; | | break | \u8bbe\u7f6e\u65ad\u70b9 | | run | \u8fd0\u884c\u7a0b\u5e8f | | continue | \u7ee7\u7eed\u6267\u884c\u7a0b\u5e8f | | step | \u5355\u6b65\u8fdb\u5165 | | next | \u5355\u6b65\u8df3\u8fc7 | | print | \u6253\u5370\u53d8\u91cf\u503c | | set | \u4fee\u6539\u53d8\u91cf\u503c | | list | \u663e\u793a\u6e90\u4ee3\u7801 | | info | \u663e\u793a\u65ad\u70b9\u4fe1\u606f | | quit | \u9000\u51faGDB\u8c03\u8bd5\u5668 | <\/p>\n<h3> 2.5GCC\u9ad8\u7ea7\u7f16\u8bd1\u6280\u5de7 <\/h3>\n<p> GCC\u63d0\u4f9b\u4e86\u8bb8\u591a\u9ad8\u7ea7\u7f16\u8bd1\u6280\u5de7&#xff0c;\u53ef\u4ee5\u5bf9\u4ee3\u7801\u7684\u6027\u80fd\u8fdb\u884c\u4f18\u5316\u6216\u6dfb\u52a0\u7279\u5b9a\u7684\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u9ad8\u7ea7\u7f16\u8bd1\u6280\u5de7\u7684\u7b80\u8981\u4ecb\u7ecd&#xff1a; <\/p>\n<h4> 2.5.1 \u4f18\u5316\u7f16\u8bd1 <\/h4>\n<p> GCC\u5141\u8bb8\u5f00\u53d1\u8005\u6839\u636e\u9700\u8981\u9009\u62e9\u4e0d\u540c\u7a0b\u5ea6\u7684\u4f18\u5316\u3002\u4f8b\u5982&#xff0c;\u4f7f\u7528  -O2  \u8fdb\u884c\u4e2d\u7b49\u4f18\u5316&#xff0c;\u4f7f\u7528  -O3  \u8fdb\u884c\u66f4\u9ad8\u5f3a\u5ea6\u7684\u4f18\u5316&#xff1a; <\/p>\n<p>gcc -O2 -o optimized_program my_program.c<\/p>\n<h4> 2.5.2 \u9759\u6001\u548c\u52a8\u6001\u94fe\u63a5 <\/h4>\n<p> GCC\u7f16\u8bd1\u65f6\u53ef\u4ee5\u9009\u62e9\u9759\u6001\u94fe\u63a5\u6216\u52a8\u6001\u94fe\u63a5\u5e93\u6587\u4ef6\u3002\u9759\u6001\u94fe\u63a5\u5c06\u5e93\u6587\u4ef6\u76f4\u63a5\u5305\u542b\u5728\u53ef\u6267\u884c\u6587\u4ef6\u4e2d&#xff0c;\u800c\u52a8\u6001\u94fe\u63a5\u5219\u9700\u8981\u5728\u8fd0\u884c\u65f6\u52a0\u8f7d\u5e93\u6587\u4ef6&#xff1a; <\/p>\n<p>gcc -static -o static_program my_program.c<br \/>\ngcc -o dynamic_program my_program.c -lm<\/p>\n<h4> 2.5.3 \u8b66\u544a\u548c\u9519\u8bef\u5904\u7406 <\/h4>\n<p> GCC\u63d0\u4f9b\u4e86\u591a\u79cd\u8b66\u544a\u9009\u9879&#xff0c;\u53ef\u4ee5\u68c0\u6d4b\u6f5c\u5728\u7684\u4ee3\u7801\u95ee\u9898&#xff0c;\u5e76\u5728\u7f16\u8bd1\u65f6\u663e\u793a\u51fa\u6765&#xff1a; <\/p>\n<p>gcc -Wall -Wextra -o my_program my_program.c<\/p>\n<h3> 2.6 GCC\u7f16\u8bd1\u5668\u7684\u6269\u5c55\u4f7f\u7528 <\/h3>\n<p> GCC\u652f\u6301\u8bb8\u591a\u9ad8\u7ea7\u7279\u6027&#xff0c;\u4f8b\u5982\u7f16\u8bd1\u5668\u63d2\u4ef6\u3001GCC\u6269\u5c55\u8bed\u8a00\u7279\u6027\u4ee5\u53ca\u4e0e\u5176\u4ed6\u5de5\u5177\u7684\u96c6\u6210\u3002\u5f00\u53d1\u8005\u53ef\u4ee5\u5229\u7528\u8fd9\u4e9b\u7279\u6027\u6765\u6269\u5c55GCC\u7684\u529f\u80fd&#xff0c;\u4ee5\u9002\u5e94\u7279\u5b9a\u7684\u5f00\u53d1\u9700\u6c42\u3002 <\/p>\n<h4> 2.6.1 GCC\u6269\u5c55\u8bed\u8a00\u7279\u6027 <\/h4>\n<p> GCC\u652f\u6301C&#043;&#043;11\/14\/17\u7b49\u7248\u672c\u7684\u7279\u6027&#xff0c;\u5f00\u53d1\u8005\u53ef\u4ee5\u901a\u8fc7\u7279\u5b9a\u7684\u7f16\u8bd1\u9009\u9879\u542f\u7528\u8fd9\u4e9b\u7279\u6027&#xff1a; <\/p>\n<p>gcc -std&#061;c&#043;&#043;17 -o my_program my_program.c<\/p>\n<h4> 2.6.2 \u63d2\u4ef6\u7cfb\u7edf <\/h4>\n<p> GCC\u7684\u63d2\u4ef6\u7cfb\u7edf\u5141\u8bb8\u5f00\u53d1\u8005\u6dfb\u52a0\u81ea\u5b9a\u4e49\u7f16\u8bd1\u5668\u884c\u4e3a&#xff0c;\u4f8b\u5982\u8bed\u6cd5\u5206\u6790\u3001\u4f18\u5316\u68c0\u67e5\u7b49&#xff1a; <\/p>\n<p>gcc -fplugin&#061;my_plugin.so -o my_program my_program.c<\/p>\n<h4> 2.6.3 \u96c6\u6210\u5de5\u5177 <\/h4>\n<p> GCC\u53ef\u4ee5\u4e0e\u5176\u4ed6\u5f00\u53d1\u5de5\u5177\u96c6\u6210&#xff0c;\u4f8b\u5982\u7248\u672c\u63a7\u5236\u3001\u4ee3\u7801\u5206\u6790\u5de5\u5177\u7b49&#xff0c;\u4ece\u800c\u63d0\u5347\u5f00\u53d1\u6548\u7387&#xff1a; <\/p>\n<p>gcc -fanalyzer -o my_program my_program.c<\/p>\n<p> GCC\u7f16\u8bd1\u5668\u4f5c\u4e3aC&#043;&#043;\u5f00\u53d1\u8005\u7684\u6838\u5fc3\u5de5\u5177&#xff0c;\u5176\u91cd\u8981\u6027\u4e0d\u8a00\u800c\u55bb\u3002\u719f\u7ec3\u638c\u63e1GCC\u7684\u4f7f\u7528\u548c\u8c03\u8bd5\u6280\u5de7\u5bf9\u4e8e\u63d0\u5347\u5f00\u53d1\u6548\u7387\u548c\u4ee3\u7801\u8d28\u91cf\u81f3\u5173\u91cd\u8981\u3002\u901a\u8fc7\u672c\u7ae0\u7684\u5b66\u4e60&#xff0c;\u8bfb\u8005\u5e94\u8be5\u80fd\u591f\u66f4\u597d\u5730\u5229\u7528GCC\u7f16\u8bd1\u5668\u6765\u6784\u5efa\u548c\u8c03\u8bd5C&#043;&#043;\u7a0b\u5e8f\u3002\u5728\u63a5\u4e0b\u6765\u7684\u7ae0\u8282\u4e2d&#xff0c;\u6211\u4eec\u5c06\u6df1\u5165Linux\u5185\u5b58\u7ba1\u7406\u53ca\u6307\u9488\u64cd\u4f5c&#xff0c;\u63a2\u7d22\u66f4\u591aC&#043;&#043;\u5f00\u53d1\u4e2d\u7684\u6838\u5fc3\u6982\u5ff5\u548c\u6280\u672f\u3002 <\/p>\n<h2> 3. Linux\u5185\u5b58\u7ba1\u7406\u53ca\u6307\u9488\u64cd\u4f5c <\/h2>\n<p> Linux\u4f5c\u4e3a\u4e00\u4e2a\u591a\u7528\u6237\u3001\u591a\u4efb\u52a1\u7684\u64cd\u4f5c\u7cfb\u7edf&#xff0c;\u5b83\u7684\u5185\u5b58\u7ba1\u7406\u673a\u5236\u662f\u5176\u6838\u5fc3\u529f\u80fd\u4e4b\u4e00\u3002\u7406\u89e3Linux\u5185\u5b58\u7ba1\u7406\u5bf9\u4e8e\u7f16\u5199\u9ad8\u6027\u80fd\u7684C&#043;&#043;\u7a0b\u5e8f\u81f3\u5173\u91cd\u8981&#xff0c;\u5c24\u5176\u662f\u5728\u8fdb\u884c\u7cfb\u7edf\u7ea7\u7f16\u7a0b\u548c\u5d4c\u5165\u5f0f\u5f00\u53d1\u65f6\u66f4\u662f\u5982\u6b64\u3002\u6b64\u5916&#xff0c;\u6307\u9488\u4f5c\u4e3aC&#043;&#043;\u4e2d\u4e00\u79cd\u57fa\u7840\u800c\u5f3a\u5927\u7684\u7279\u6027&#xff0c;\u5176\u6b63\u786e\u4f7f\u7528\u548c\u7ba1\u7406\u76f4\u63a5\u5173\u7cfb\u5230\u7a0b\u5e8f\u7684\u5b89\u5168\u6027\u548c\u6548\u7387\u3002\u672c\u7ae0\u5c06\u8be6\u7ec6\u63a2\u8ba8Linux\u73af\u5883\u4e0b\u7684\u5185\u5b58\u7ba1\u7406\u53ca\u6307\u9488\u64cd\u4f5c&#xff0c;\u5e2e\u52a9\u5f00\u53d1\u8005\u66f4\u597d\u5730\u638c\u63e1\u8fd9\u4e9b\u57fa\u7840\u800c\u53c8\u5173\u952e\u7684\u77e5\u8bc6\u3002 <\/p>\n<h3> 3.1 Linux\u5185\u5b58\u7ba1\u7406\u6982\u8ff0 <\/h3>\n<h4> 3.1.1 \u865a\u62df\u5185\u5b58\u548c\u7269\u7406\u5185\u5b58 <\/h4>\n<p> \u5728Linux\u7cfb\u7edf\u4e2d&#xff0c;\u6bcf\u4e2a\u8fdb\u7a0b\u90fd\u8fd0\u884c\u5728\u81ea\u5df1\u7684\u865a\u62df\u5185\u5b58\u7a7a\u95f4\u4e2d\u3002\u865a\u62df\u5185\u5b58\u7a7a\u95f4\u662f\u8fde\u7eed\u7684\u5730\u5740\u8303\u56f4&#xff0c;\u800c\u7269\u7406\u5185\u5b58\u5219\u662f\u7269\u7406\u4e0a\u7684RAM\u3002\u64cd\u4f5c\u7cfb\u7edf\u901a\u8fc7\u865a\u62df\u5185\u5b58\u7ba1\u7406\u6280\u672f&#xff08;VM&#xff09;\u5c06\u865a\u62df\u5185\u5b58\u6620\u5c04\u5230\u7269\u7406\u5185\u5b58\u4e0a&#xff0c;\u5b9e\u73b0\u8fdb\u7a0b\u95f4\u5185\u5b58\u7684\u9694\u79bb\u548c\u4fdd\u62a4\u3002 <\/p>\n<h4> 3.1.2 \u5185\u5b58\u5206\u914d\u65b9\u5f0f <\/h4>\n<p> Linux\u7cfb\u7edf\u63d0\u4f9b\u4e86\u591a\u79cd\u5185\u5b58\u5206\u914d\u65b9\u5f0f&#xff0c;\u5305\u62ec  brk()  \u7cfb\u7edf\u8c03\u7528\u3001  mmap()  \u7cfb\u7edf\u8c03\u7528\u4ee5\u53ca\u901a\u8fc7\u5185\u5b58\u6620\u5c04\u6587\u4ef6\u8fdb\u884c\u5185\u5b58\u5206\u914d\u7b49\u3002  brk()  \u7528\u4e8e\u8c03\u6574\u8fdb\u7a0b\u5806\u7a7a\u95f4\u7684\u5927\u5c0f&#xff0c;\u800c  mmap()  \u5219\u7528\u4e8e\u5c06\u6587\u4ef6\u5185\u5bb9\u6620\u5c04\u5230\u8fdb\u7a0b\u7684\u5730\u5740\u7a7a\u95f4&#xff0c;\u5b9e\u73b0\u6587\u4ef6\u5230\u5185\u5b58\u7684\u6570\u636e\u4f20\u8f93\u3002 <\/p>\n<h4> 3.1.3 \u5185\u5b58\u5206\u914d\u7b56\u7565 <\/h4>\n<p> Linux\u5185\u6838\u7684\u5185\u5b58\u5206\u914d\u7b56\u7565\u5305\u542b\u4f19\u4f34\u7cfb\u7edf&#xff08;Buddy System&#xff09;\u548cslab\u5206\u914d\u5668\u7b49\u3002\u4f19\u4f34\u7cfb\u7edf\u8d1f\u8d23\u7ba1\u7406\u5927\u5757\u5185\u5b58\u5206\u914d&#xff0c;\u800cslab\u5206\u914d\u5668\u5219\u4e13\u6ce8\u4e8e\u5904\u7406\u5c0f\u5757\u5185\u5b58\u5206\u914d&#xff0c;\u5982\u5185\u6838\u5bf9\u8c61\u7684\u5206\u914d\u3002 <\/p>\n<h4> 3.1.4 \u7f3a\u9875\u4e2d\u65ad\u548c\u5185\u5b58\u6620\u5c04 <\/h4>\n<p> \u5f53\u8fdb\u7a0b\u8bbf\u95ee\u4e00\u4e2a\u5c1a\u672a\u6620\u5c04\u5230\u7269\u7406\u5185\u5b58\u7684\u865a\u62df\u5185\u5b58\u9875\u9762\u65f6&#xff0c;\u4f1a\u89e6\u53d1\u7f3a\u9875\u4e2d\u65ad&#xff08;page fault&#xff09;\u3002\u5185\u6838\u6b64\u65f6\u9700\u8981\u4ece\u4ea4\u6362\u533a\u52a0\u8f7d\u9875\u9762\u5230\u7269\u7406\u5185\u5b58&#xff0c;\u5e76\u5efa\u7acb\u865a\u62df\u5185\u5b58\u5230\u7269\u7406\u5185\u5b58\u7684\u6620\u5c04\u5173\u7cfb\u3002 <\/p>\n<h3> 3.2 \u5185\u5b58\u6cc4\u6f0f\u7684\u68c0\u6d4b\u548c\u9884\u9632 <\/h3>\n<h4> 3.2.1 \u5185\u5b58\u6cc4\u6f0f\u7684\u5f71\u54cd <\/h4>\n<p> \u5185\u5b58\u6cc4\u6f0f\u662f\u6307\u7a0b\u5e8f\u5728\u7533\u8bf7\u5185\u5b58\u540e\u672a\u80fd\u91ca\u653e\u5df2\u4e0d\u518d\u4f7f\u7528\u7684\u5185\u5b58&#xff0c;\u5bfc\u81f4\u53ef\u7528\u5185\u5b58\u9010\u6e10\u51cf\u5c11&#xff0c;\u6700\u7ec8\u53ef\u80fd\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83\u6216\u7cfb\u7edf\u8d44\u6e90\u8017\u5c3d\u3002 <\/p>\n<h4> 3.2.2 \u5185\u5b58\u6cc4\u6f0f\u7684\u68c0\u6d4b\u5de5\u5177 <\/h4>\n<p> Linux\u7cfb\u7edf\u4e0b&#xff0c;\u53ef\u4ee5\u4f7f\u7528  valgrind  \u8fd9\u6837\u7684\u5185\u5b58\u6cc4\u6f0f\u68c0\u6d4b\u5de5\u5177\u6765\u5e2e\u52a9\u5b9a\u4f4d\u5185\u5b58\u6cc4\u6f0f\u95ee\u9898\u3002  valgrind  \u901a\u8fc7\u8fd0\u884c\u65f6\u8ddf\u8e2a\u5185\u5b58\u5206\u914d\u548c\u91ca\u653e\u6765\u68c0\u6d4b\u672a\u91ca\u653e\u7684\u5185\u5b58\u3002 <\/p>\n<h4> 3.2.3 \u9884\u9632\u5185\u5b58\u6cc4\u6f0f\u7684\u7f16\u7a0b\u6280\u5de7 <\/h4>\n<p> \u4e3a\u4e86\u907f\u514d\u5185\u5b58\u6cc4\u6f0f&#xff0c;\u5efa\u8bae\u4f7f\u7528\u667a\u80fd\u6307\u9488&#xff08;\u5982  std::unique_ptr  \u548c  std::shared_ptr  &#xff09;\u6765\u81ea\u52a8\u7ba1\u7406\u8d44\u6e90\u7684\u751f\u547d\u5468\u671f&#xff0c;\u51cf\u5c11\u624b\u52a8  new  \u548c  delete  \u64cd\u4f5c\u3002 <\/p>\n<h3> 3.3 \u6307\u9488\u64cd\u4f5c\u8be6\u89e3 <\/h3>\n<h4> 3.3.1 \u6307\u9488\u7684\u57fa\u7840\u77e5\u8bc6 <\/h4>\n<p> \u6307\u9488\u662f\u4e00\u4e2a\u5b58\u50a8\u5185\u5b58\u5730\u5740\u7684\u53d8\u91cf\u3002\u5728C&#043;&#043;\u4e2d&#xff0c;\u6307\u9488\u53ef\u4ee5\u6307\u5411\u4efb\u4f55\u7c7b\u578b\u7684\u6570\u636e&#xff0c;\u5e76\u4e14\u53ef\u4ee5\u901a\u8fc7\u6307\u9488\u8bbf\u95ee\u548c\u64cd\u4f5c\u8fd9\u4e9b\u6570\u636e\u3002 <\/p>\n<h4> 3.3.2 \u6307\u9488\u4e0e\u6570\u7ec4 <\/h4>\n<p> \u6307\u9488\u53ef\u4ee5\u4e0e\u6570\u7ec4\u76f8\u4e92\u8f6c\u6362\u3002\u901a\u8fc7\u6307\u9488\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\u7684\u6548\u7387\u901a\u5e38\u9ad8\u4e8e\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee&#xff0c;\u8fd9\u662f\u56e0\u4e3a\u6307\u9488\u76f4\u63a5\u4e0e\u5185\u5b58\u5730\u5740\u6253\u4ea4\u9053\u3002 <\/p>\n<h4> 3.3.3 \u6307\u9488\u4e0e\u51fd\u6570 <\/h4>\n<p> \u51fd\u6570\u53c2\u6570\u53ef\u4ee5\u901a\u8fc7\u503c\u4f20\u9012&#xff0c;\u4e5f\u53ef\u4ee5\u901a\u8fc7\u6307\u9488\u4f20\u9012\u3002\u5f53\u4f20\u9012\u6307\u9488\u65f6&#xff0c;\u51fd\u6570\u5185\u90e8\u5bf9\u6307\u9488\u7684\u64cd\u4f5c\u5c06\u5f71\u54cd\u5b9e\u9645\u7684\u53d8\u91cf\u503c&#xff0c;\u8fd9\u4e00\u70b9\u5728C&#043;&#043;\u4e2d\u5c24\u4e3a\u91cd\u8981&#xff0c;\u56e0\u4e3a\u8fd9\u53ef\u4ee5\u7528\u4e8e\u4fee\u6539\u4f20\u5165\u51fd\u6570\u7684\u6570\u636e\u3002 <\/p>\n<h4> 3.3.4 \u6307\u9488\u4e0e\u52a8\u6001\u5185\u5b58\u5206\u914d <\/h4>\n<p> \u52a8\u6001\u5185\u5b58\u5206\u914d\u901a\u8fc7\u6307\u9488\u6765\u64cd\u4f5c&#xff0c;\u4f7f\u7528  new  \u548c  delete  &#xff08;\u6216  new[]  \u548c  delete[]  &#xff09;\u6765\u5206\u522b\u5206\u914d\u548c\u91ca\u653e\u5185\u5b58\u3002\u52a8\u6001\u5206\u914d\u7684\u5185\u5b58\u5728\u4f7f\u7528\u5b8c\u6bd5\u540e\u5fc5\u987b\u663e\u5f0f\u91ca\u653e&#xff0c;\u5426\u5219\u4f1a\u9020\u6210\u5185\u5b58\u6cc4\u6f0f\u3002 <\/p>\n<h4> 3.3.5 \u6307\u9488\u4e0e\u6307\u9488\u7684\u6307\u9488 <\/h4>\n<p> \u6307\u9488\u7684\u6307\u9488&#xff0c;\u6216\u8005\u8bf4\u591a\u7ea7\u6307\u9488&#xff0c;\u662f\u6307\u5b58\u50a8\u53e6\u4e00\u4e2a\u6307\u9488\u5730\u5740\u7684\u6307\u9488\u3002\u5728\u5904\u7406\u6307\u5411\u52a8\u6001\u5206\u914d\u6570\u7ec4\u7684\u6307\u9488\u65f6&#xff0c;\u6216\u8005\u5728C&#043;&#043;\u4e2d\u5b9e\u73b0\u591a\u7ef4\u6570\u7ec4\u65f6&#xff0c;\u591a\u7ea7\u6307\u9488\u7ecf\u5e38\u88ab\u7528\u5230\u3002 <\/p>\n<h4> 3.3.6 \u6307\u9488\u7684\u5b89\u5168\u95ee\u9898 <\/h4>\n<p> \u6307\u9488\u64cd\u4f5c\u4e2d\u7684\u672a\u521d\u59cb\u5316\u3001\u60ac\u7a7a\u548c\u8d8a\u754c\u7b49\u95ee\u9898\u662f\u9020\u6210\u7a0b\u5e8f\u5d29\u6e83\u548c\u5b89\u5168\u6f0f\u6d1e\u7684\u5e38\u89c1\u539f\u56e0\u3002\u5728\u7f16\u5199C&#043;&#043;\u4ee3\u7801\u65f6&#xff0c;\u5e94\u59cb\u7ec8\u68c0\u67e5\u6307\u9488\u662f\u5426\u4e3a  nullptr  &#xff0c;\u5e76\u4e14\u786e\u4fdd\u5176\u6307\u5411\u6709\u6548\u4e14\u672a\u8d8a\u754c\u7684\u5185\u5b58\u533a\u57df\u3002 <\/p>\n<h3> 3.4 Linux\u4e0b\u7684\u5185\u5b58\u7ba1\u7406\u5b9e\u8df5 <\/h3>\n<h4> 3.4.1 \u4f7f\u7528  malloc  \u548c  free  \u5206\u914d\u548c\u91ca\u653e\u5185\u5b58 <\/h4>\n<p> \u5728Linux\u73af\u5883\u4e2d&#xff0c;\u53ef\u4ee5\u901a\u8fc7C\u6807\u51c6\u5e93\u51fd\u6570  malloc  \u6765\u52a8\u6001\u5206\u914d\u5185\u5b58&#xff0c;\u4f7f\u7528  free  \u6765\u91ca\u653e\u5185\u5b58\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b&#xff1a; <\/p>\n<p>#include &lt;stdlib.h&gt;<\/p>\n<p>int main() {<br \/>\n    int *ptr &#061; (int*)malloc(sizeof(int)); \/\/ \u5206\u914d\u5185\u5b58<br \/>\n    if (ptr &#061;&#061; NULL) {<br \/>\n        \/\/ \u5185\u5b58\u5206\u914d\u5931\u8d25\u5904\u7406<br \/>\n        return 1;<br \/>\n    }<br \/>\n    *ptr &#061; 10; \/\/ \u4f7f\u7528\u5185\u5b58<br \/>\n    free(ptr); \/\/ \u91ca\u653e\u5185\u5b58<br \/>\n    return 0;<br \/>\n}<\/p>\n<h4> 3.4.2 \u4f7f\u7528  mmap  \u6620\u5c04\u6587\u4ef6\u5230\u5185\u5b58 <\/h4>\n<p> \u901a\u8fc7  mmap  \u51fd\u6570\u53ef\u4ee5\u5c06\u6587\u4ef6\u5185\u5bb9\u6620\u5c04\u5230\u8fdb\u7a0b\u7684\u5730\u5740\u7a7a\u95f4&#xff0c;\u4ee3\u7801\u793a\u4f8b\u5982\u4e0b&#xff1a; <\/p>\n<p>#include &lt;sys\/mman.h&gt;<br \/>\n#include &lt;unistd.h&gt;<\/p>\n<p>int main() {<br \/>\n    int fd &#061; open(&#034;testfile.txt&#034;, O_RDONLY); \/\/ \u6253\u5f00\u6587\u4ef6<br \/>\n    if (fd &#061;&#061; -1) {<br \/>\n        \/\/ \u6587\u4ef6\u6253\u5f00\u5931\u8d25\u5904\u7406<br \/>\n        return 1;<br \/>\n    }<br \/>\n    off_t size &#061; lseek(fd, 0, SEEK_END); \/\/ \u83b7\u53d6\u6587\u4ef6\u5927\u5c0f<br \/>\n    void *addr &#061; mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0); \/\/ \u6620\u5c04\u6587\u4ef6<br \/>\n    close(fd); \/\/ \u5173\u95ed\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u6587\u4ef6\u6620\u5c04\u4ecd\u53ef\u8bbf\u95ee<\/p>\n<p>    \/\/ \u4f7f\u7528\u6620\u5c04\u7684\u5185\u5b58addr<\/p>\n<p>    munmap(addr, size); \/\/ \u53d6\u6d88\u6620\u5c04<br \/>\n    return 0;<br \/>\n}<\/p>\n<h4> 3.4.3 \u4f7f\u7528  valgrind  \u68c0\u6d4b\u5185\u5b58\u6cc4\u6f0f <\/h4>\n<p> \u4f7f\u7528  valgrind  \u7684\u547d\u4ee4\u5982\u4e0b&#xff1a; <\/p>\n<p>valgrind &#8211;leak-check&#061;full .\/a.out<\/p>\n<p>  valgrind  \u4f1a\u663e\u793a\u7a0b\u5e8f\u7684\u5185\u5b58\u6cc4\u6f0f\u60c5\u51b5&#xff0c;\u5305\u62ec\u6cc4\u6f0f\u5185\u5b58\u7684\u4f4d\u7f6e\u548c\u5927\u5c0f\u3002 <\/p>\n<h4> 3.4.4 \u4f7f\u7528\u667a\u80fd\u6307\u9488\u907f\u514d\u5185\u5b58\u6cc4\u6f0f <\/h4>\n<p> C&#043;&#043;11\u5f15\u5165\u7684\u667a\u80fd\u6307\u9488\u53ef\u4ee5\u81ea\u52a8\u7ba1\u7406\u5185\u5b58\u7684\u751f\u547d\u5468\u671f&#xff0c;\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b&#xff1a; <\/p>\n<p>#include &lt;memory&gt;<\/p>\n<p>int main() {<br \/>\n    std::unique_ptr&lt;int&gt; ptr &#061; std::make_unique&lt;int&gt;(10); \/\/ \u4f7f\u7528make_unique\u81ea\u52a8\u7ba1\u7406\u5185\u5b58<br \/>\n    \/\/ \u5f53ptr\u79bb\u5f00\u4f5c\u7528\u57df\u65f6&#xff0c;\u52a8\u6001\u5206\u914d\u7684\u5185\u5b58\u4f1a\u81ea\u52a8\u91ca\u653e<br \/>\n    return 0;<br \/>\n}<\/p>\n<p> \u901a\u8fc7\u672c\u7ae0\u7684\u4ecb\u7ecd&#xff0c;\u6211\u4eec\u4e86\u89e3\u4e86Linux\u5185\u5b58\u7ba1\u7406\u7684\u57fa\u672c\u6982\u5ff5\u3001\u6307\u9488\u64cd\u4f5c\u7684\u7ec6\u8282\u4ee5\u53ca\u5b9e\u9645\u64cd\u4f5c\u4e2d\u7684\u5185\u5b58\u6cc4\u6f0f\u95ee\u9898\u548c\u89e3\u51b3\u65b9\u6cd5\u3002\u638c\u63e1\u8fd9\u4e9b\u77e5\u8bc6\u5bf9\u4e8e\u5f00\u53d1\u9ad8\u6548\u4e14\u5b89\u5168\u7684C&#043;&#043;\u5e94\u7528\u7a0b\u5e8f\u81f3\u5173\u91cd\u8981\u3002 <\/p>\n<h2> 4. \u57fa\u4e8esocket\u7684\u7f51\u7edc\u7f16\u7a0b\u4e0e\u5e76\u53d1\u5904\u7406 <\/h2>\n<h3> 4.1 \u7f51\u7edc\u7f16\u7a0b\u57fa\u7840\u6982\u5ff5 <\/h3>\n<p> \u7f51\u7edc\u7f16\u7a0b\u6d89\u53ca\u5728\u4e0d\u540c\u8ba1\u7b97\u673a\u6216\u7f51\u7edc\u4e2d\u7684\u8bbe\u5907\u4e4b\u95f4\u4ea4\u6362\u6570\u636e\u3002\u5728C&#043;&#043;\u4e2d&#xff0c;\u8fd9\u4e00\u8fc7\u7a0b\u901a\u5e38\u662f\u901a\u8fc7\u5957\u63a5\u5b57&#xff08;sockets&#xff09;\u5b8c\u6210\u7684&#xff0c;\u5b83\u662f\u4e00\u79cd\u5141\u8bb8\u7a0b\u5e8f\u901a\u8fc7\u7f51\u7edc\u901a\u4fe1\u7684\u7aef\u70b9\u3002TCP\u548cUDP\u662f\u4e24\u79cd\u5e38\u7528\u7684\u534f\u8bae&#xff0c;\u5176\u4e2dTCP&#xff08;\u4f20\u8f93\u63a7\u5236\u534f\u8bae&#xff09;\u63d0\u4f9b\u4e86\u4e00\u79cd\u53ef\u9760\u7684\u3001\u6709\u5e8f\u7684\u3001\u65e0\u91cd\u590d\u7684\u5b57\u8282\u6d41\u4f20\u8f93\u670d\u52a1&#xff0c;\u800cUDP&#xff08;\u7528\u6237\u6570\u636e\u62a5\u534f\u8bae&#xff09;\u5219\u662f\u65e0\u8fde\u63a5\u7684&#xff0c;\u63d0\u4f9b\u4e86\u4e00\u79cd\u5feb\u901f\u4f46\u53ef\u80fd\u4e0d\u5b8c\u6574\u7684\u6570\u636e\u4f20\u8f93\u670d\u52a1\u3002 <\/p>\n<h4> \u8868\u683c1&#xff1a;TCP\u4e0eUDP\u5bf9\u6bd4 <\/h4>\n<p> | \u7279\u6027 | TCP | UDP | | &#8212; | &#8212; | &#8212; | | \u8fde\u63a5\u6027 | \u9762\u5411\u8fde\u63a5 | \u65e0\u8fde\u63a5 | | \u6570\u636e\u4f20\u8f93 | \u53ef\u9760\u4f20\u8f93 | \u4e0d\u53ef\u9760\u4f20\u8f93 | | \u901f\u5ea6 | \u8f83\u6162 | \u8f83\u5feb | | \u987a\u5e8f\u4fdd\u8bc1 | \u4fdd\u8bc1\u6570\u636e\u987a\u5e8f | \u4e0d\u4fdd\u8bc1\u6570\u636e\u987a\u5e8f | | \u91cd\u590d\u6570\u636e | \u65e0\u91cd\u590d\u6570\u636e | \u53ef\u80fd\u51fa\u73b0\u91cd\u590d\u6570\u636e | | \u4f8b\u5b50\u5e94\u7528 | \u7f51\u9875\u6d4f\u89c8\u3001\u6587\u4ef6\u4f20\u8f93 | \u6d41\u5a92\u4f53\u3001\u5728\u7ebf\u6e38\u620f | <\/p>\n<p> \u5728\u7406\u89e3\u4e86\u8fd9\u4e24\u79cd\u534f\u8bae\u4e4b\u540e&#xff0c;\u5f00\u53d1\u8005\u53ef\u4ee5\u6839\u636e\u5e94\u7528\u9700\u6c42\u9009\u62e9\u9002\u5f53\u7684\u534f\u8bae\u3002\u4f8b\u5982&#xff0c;\u5bf9\u4e8e\u9700\u8981\u9ad8\u53ef\u9760\u6027\u7684\u5e94\u7528&#xff0c;\u5982\u7535\u5b50\u90ae\u4ef6&#xff0c;TCP\u53ef\u80fd\u662f\u66f4\u597d\u7684\u9009\u62e9&#xff1b;\u800c\u5bf9\u4e8e\u5b9e\u65f6\u6027\u8981\u6c42\u8f83\u9ad8\u7684\u5e94\u7528&#xff0c;\u5982\u5728\u7ebf\u6e38\u620f&#xff0c;UDP\u53ef\u80fd\u662f\u66f4\u5408\u9002\u7684\u9009\u62e9\u3002 <\/p>\n<h4> \u4ee3\u78011&#xff1a;TCP\u670d\u52a1\u5668\u57fa\u7840 <\/h4>\n<p>#include &lt;sys\/socket.h&gt;<br \/>\n#include &lt;netinet\/in.h&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;cstring&gt;<\/p>\n<p>int main() {<br \/>\n    int server_fd, new_socket;<br \/>\n    struct sockaddr_in address;<br \/>\n    int opt &#061; 1;<br \/>\n    int addrlen &#061; sizeof(address);<br \/>\n    char buffer[1024] &#061; {0};<br \/>\n    \/\/ \u521b\u5efa\u5957\u63a5\u5b57<br \/>\n    if ((server_fd &#061; socket(AF_INET, SOCK_STREAM, 0)) &#061;&#061; 0) {<br \/>\n        perror(&#034;socket failed&#034;);<br \/>\n        exit(EXIT_FAILURE);<br \/>\n    }<\/p>\n<p>    \/\/ \u7ed1\u5b9a\u5957\u63a5\u5b57\u5230\u7aef\u53e38080<br \/>\n    if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &amp;opt, sizeof(opt))) {<br \/>\n        perror(&#034;setsockopt&#034;);<br \/>\n        exit(EXIT_FAILURE);<br \/>\n    }<br \/>\n    address.sin_family &#061; AF_INET;<br \/>\n    address.sin_addr.s_addr &#061; INADDR_ANY;<br \/>\n    address.sin_port &#061; htons(8080);<\/p>\n<p>    if (bind(server_fd, (struct sockaddr *)&amp;address, sizeof(address)) &lt; 0) {<br \/>\n        perror(&#034;bind failed&#034;);<br \/>\n        exit(EXIT_FAILURE);<br \/>\n    }<\/p>\n<p>    \/\/ \u76d1\u542c\u7aef\u53e3<br \/>\n    if (listen(server_fd, 3) &lt; 0) {<br \/>\n        perror(&#034;listen&#034;);<br \/>\n        exit(EXIT_FAILURE);<br \/>\n    }<\/p>\n<p>    \/\/ \u63a5\u53d7\u8fde\u63a5<br \/>\n    if ((new_socket &#061; accept(server_fd, (struct sockaddr *)&amp;address, (socklen_t*)&amp;addrlen)) &lt; 0) {<br \/>\n        perror(&#034;accept&#034;);<br \/>\n        exit(EXIT_FAILURE);<br \/>\n    }<\/p>\n<p>    \/\/ \u8bfb\u53d6\u6570\u636e<br \/>\n    read(new_socket, buffer, 1024);<br \/>\n    printf(&#034;%s\\\\n&#034;, buffer);<\/p>\n<p>    \/\/ \u5173\u95ed\u5957\u63a5\u5b57<br \/>\n    close(server_fd);<br \/>\n    return 0;<br \/>\n}<\/p>\n<p> \u5728\u8fd9\u4e2aTCP\u670d\u52a1\u5668\u793a\u4f8b\u4ee3\u7801\u4e2d&#xff0c;\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2a\u5957\u63a5\u5b57&#xff0c;\u7136\u540e\u5c06\u5176\u7ed1\u5b9a\u5230\u4e00\u4e2aIP\u5730\u5740\u548c\u7aef\u53e3\u4e0a&#xff0c;\u63a5\u7740\u76d1\u542c\u6765\u81ea\u5ba2\u6237\u7aef\u7684\u8fde\u63a5\u8bf7\u6c42\u3002\u5f53\u4e00\u4e2a\u8fde\u63a5\u88ab\u63a5\u53d7\u540e&#xff0c;\u670d\u52a1\u5668\u8bfb\u53d6\u5ba2\u6237\u7aef\u53d1\u9001\u7684\u6570\u636e&#xff0c;\u7136\u540e\u5173\u95ed\u5957\u63a5\u5b57\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u6d89\u53ca\u4e86\u591a\u4e2a\u7cfb\u7edf\u8c03\u7528&#xff0c;\u6bcf\u4e2a\u8c03\u7528\u90fd\u6709\u5176\u7279\u5b9a\u7684\u53c2\u6570\u548c\u8fd4\u56de\u503c&#xff0c;\u9700\u8981\u4ed4\u7ec6\u5904\u7406\u3002 <\/p>\n<h3> 4.2 \u5e76\u53d1\u5904\u7406\u673a\u5236 <\/h3>\n<p> \u5e76\u53d1\u5904\u7406\u662f\u7f51\u7edc\u7f16\u7a0b\u4e2d\u7684\u4e00\u4e2a\u9ad8\u7ea7\u4e3b\u9898\u3002\u5728\u7f51\u7edc\u670d\u52a1\u5668\u4e2d&#xff0c;\u53ef\u80fd\u540c\u65f6\u6709\u6570\u767e\u751a\u81f3\u6570\u5343\u4e2a\u5ba2\u6237\u7aef\u8fde\u63a5\u3002\u4e3a\u6bcf\u4e2a\u8fde\u63a5\u90fd\u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b\u5c06\u6d88\u8017\u5de8\u5927\u7684\u7cfb\u7edf\u8d44\u6e90\u3002\u56e0\u6b64&#xff0c;\u4f7f\u7528\u7ebf\u7a0b\u6c60\u6216\u4e8b\u4ef6\u9a71\u52a8\u6a21\u578b\u7b49\u6280\u672f\u53ef\u4ee5\u6709\u6548\u5730\u5904\u7406\u5927\u91cf\u5e76\u53d1\u8fde\u63a5\u3002 <\/p>\n<h4> \u8868\u683c2&#xff1a;\u5e76\u53d1\u6a21\u578b\u6bd4\u8f83 <\/h4>\n<p> | \u5e76\u53d1\u6a21\u578b | \u63cf\u8ff0 | \u4f18\u70b9 | \u7f3a\u70b9 | | &#8212; | &#8212; | &#8212; | &#8212; | | \u591a\u7ebf\u7a0b | \u4e3a\u6bcf\u4e2a\u8fde\u63a5\u521b\u5efa\u4e00\u4e2a\u65b0\u7ebf\u7a0b | \u76f4\u89c2\u3001\u6613\u4e8e\u5b9e\u73b0 | \u8d44\u6e90\u6d88\u8017\u5927\u3001\u7ebf\u7a0b\u95f4\u540c\u6b65\u590d\u6742 | | \u7ebf\u7a0b\u6c60 | \u4e00\u7ec4\u7ebf\u7a0b\u91cd\u590d\u7528\u4e8e\u670d\u52a1\u591a\u4e2a\u8bf7\u6c42 | \u7ebf\u7a0b\u590d\u7528\u3001\u51cf\u5c11\u4e0a\u4e0b\u6587\u5207\u6362 | \u9700\u8981\u7cbe\u7ec6\u7684\u7ebf\u7a0b\u7ba1\u7406 | | \u4e8b\u4ef6\u9a71\u52a8 | \u4e8b\u4ef6&#xff08;\u5982\u65b0\u8fde\u63a5\u3001\u6570\u636e\u63a5\u6536&#xff09;\u89e6\u53d1\u56de\u8c03 | \u9ad8\u6548\u3001\u8d44\u6e90\u6d88\u8017\u4f4e | \u7f16\u7a0b\u6a21\u578b\u590d\u6742&#xff0c;\u8c03\u8bd5\u56f0\u96be | <\/p>\n<h4> mermaid\u6d41\u7a0b\u56fe&#xff1a;\u4e8b\u4ef6\u9a71\u52a8\u6a21\u578b\u793a\u4f8b <\/h4>\n<p>graph TD<br \/>\n    A[\u76d1\u542c\u4e8b\u4ef6] &#8211;&gt;|\u65b0\u8fde\u63a5| B[\u5904\u7406\u8fde\u63a5]<br \/>\n    A &#8211;&gt;|\u6570\u636e\u63a5\u6536| C[\u5904\u7406\u6570\u636e]<br \/>\n    B &#8211;&gt; D[\u8fdb\u5165\u4e8b\u4ef6\u961f\u5217]<br \/>\n    C &#8211;&gt; D<br \/>\n    D &#8211;&gt; E[\u6267\u884c\u56de\u8c03\u51fd\u6570]<br \/>\n    E &#8211;&gt;|\u5b8c\u6210| A<\/p>\n<p> \u5728\u4e8b\u4ef6\u9a71\u52a8\u6a21\u578b\u4e2d&#xff0c;\u670d\u52a1\u5668\u76d1\u542c\u5404\u79cd\u4e8b\u4ef6&#xff0c;\u5e76\u5c06\u8fd9\u4e9b\u4e8b\u4ef6\u6392\u961f\u3002\u5f53\u4e00\u4e2a\u4e8b\u4ef6\u53d1\u751f\u65f6&#xff0c;\u5982\u65b0\u8fde\u63a5\u6216\u6570\u636e\u63a5\u6536&#xff0c;\u5b83\u5c06\u89e6\u53d1\u76f8\u5e94\u7684\u56de\u8c03\u51fd\u6570&#xff0c;\u7136\u540e\u8fd4\u56de\u5230\u76d1\u542c\u72b6\u6001\u3002\u8fd9\u79cd\u65b9\u5f0f\u7279\u522b\u9002\u5408\u5904\u7406\u5927\u91cf\u5e76\u53d1\u8fde\u63a5&#xff0c;\u56e0\u4e3a\u5b83\u4e0d\u9700\u8981\u4e3a\u6bcf\u4e2a\u8fde\u63a5\u5206\u914d\u4e00\u4e2a\u7ebf\u7a0b\u6216\u8fdb\u7a0b\u3002 <\/p>\n<h4> \u4ee3\u78012&#xff1a;\u4e8b\u4ef6\u9a71\u52a8\u6a21\u578b\u4f2a\u4ee3\u7801 <\/h4>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;sys\/epoll.h&gt;<\/p>\n<p>int main() {<br \/>\n    int epoll_fd &#061; epoll_create1(0);<br \/>\n    struct epoll_event event;<br \/>\n    struct epoll_event *events;<\/p>\n<p>    \/\/ \u5206\u914d\u4e8b\u4ef6\u5217\u8868\u5927\u5c0f<br \/>\n    events &#061; new struct epoll_event[1000];<\/p>\n<p>    \/\/ \u5c06\u5957\u63a5\u5b57\u52a0\u5165epoll<br \/>\n    event.data.fd &#061; server_fd;<br \/>\n    event.events &#061; EPOLLIN;<br \/>\n    epoll_ctl(epoll_fd, EPOLL_CTL_ADD, server_fd, &amp;event);<\/p>\n<p>    \/\/ \u7b49\u5f85\u4e8b\u4ef6\u53d1\u751f<br \/>\n    int nfds &#061; epoll_wait(epoll_fd, events, 1000, -1);<\/p>\n<p>    \/\/ \u5904\u7406\u4e8b\u4ef6<br \/>\n    for(int n &#061; 0; n &lt; nfds; &#043;&#043;n) {<br \/>\n        if (events[n].events &amp; EPOLLIN) {<br \/>\n            \/\/ \u5904\u7406\u65b0\u7684\u8fde\u63a5\u6216\u6570\u636e<br \/>\n        }<br \/>\n    }<\/p>\n<p>    \/\/ \u6e05\u7406<br \/>\n    close(server_fd);<br \/>\n    delete[] events;<br \/>\n    close(epoll_fd);<br \/>\n    return 0;<br \/>\n}<\/p>\n<p> \u4e0a\u8ff0\u793a\u4f8b\u4f7f\u7528\u4e86epoll&#xff0c;\u8fd9\u662fLinux\u7279\u6709\u7684\u9ad8\u6548\u7684\u4e8b\u4ef6\u76d1\u542c\u673a\u5236&#xff0c;\u5b83\u5141\u8bb8\u4e00\u4e2a\u8fdb\u7a0b\u5728\u591a\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e0a\u7b49\u5f85I\/O\u4e8b\u4ef6\u3002\u5728\u8fd9\u4e2a\u4f2a\u4ee3\u7801\u4e2d&#xff0c;\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2aepoll\u5b9e\u4f8b&#xff0c;\u5e76\u5c06\u670d\u52a1\u5668\u7684\u76d1\u542c\u5957\u63a5\u5b57\u52a0\u5165\u5230epoll\u3002\u7136\u540e&#xff0c;\u6211\u4eec\u7b49\u5f85epoll\u4e8b\u4ef6\u53d1\u751f\u3002\u4e00\u65e6\u6709\u4e8b\u4ef6\u53d1\u751f&#xff0c;\u6211\u4eec\u5c31\u53ef\u4ee5\u5904\u7406\u65b0\u7684\u8fde\u63a5\u6216\u8bfb\u5199\u6570\u636e\u3002 <\/p>\n<h3> 4.3 \u7f51\u7edc\u7f16\u7a0b\u5b9e\u8df5\u6280\u5de7 <\/h3>\n<p> \u4e3a\u4e86\u6709\u6548\u5730\u8fdb\u884c\u7f51\u7edc\u7f16\u7a0b&#xff0c;\u5f00\u53d1\u8005\u5e94\u8be5\u638c\u63e1\u4e00\u4e9b\u5b9e\u8df5\u6280\u5de7\u3002\u9996\u5148&#xff0c;\u4f7f\u7528\u975e\u963b\u585e\u5957\u63a5\u5b57\u53ef\u4ee5\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u7684\u54cd\u5e94\u80fd\u529b\u3002\u5176\u6b21&#xff0c;\u5408\u7406\u4f7f\u7528\u7f13\u51b2\u533a\u53ef\u4ee5\u51cf\u5c11\u6570\u636e\u4f20\u8f93\u7684\u6b21\u6570\u548c\u5ef6\u8fdf\u3002\u6b64\u5916&#xff0c;\u5bf9\u6570\u636e\u8fdb\u884c\u9002\u5f53\u7684\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316&#xff0c;\u53ef\u4ee5\u786e\u4fdd\u5728\u4e0d\u540c\u7cfb\u7edf\u95f4\u4f20\u8f93\u7684\u517c\u5bb9\u6027\u548c\u5b89\u5168\u6027\u3002 <\/p>\n<h4> \u4ee3\u78013&#xff1a;\u975e\u963b\u585eTCP\u5957\u63a5\u5b57\u793a\u4f8b <\/h4>\n<p>#include &lt;fcntl.h&gt;<br \/>\n#include &lt;unistd.h&gt;<\/p>\n<p>\/\/ \u5c06\u5957\u63a5\u5b57\u8bbe\u7f6e\u4e3a\u975e\u963b\u585e\u6a21\u5f0f<br \/>\nint flags &#061; fcntl(new_socket, F_GETFL, 0);<br \/>\nfcntl(new_socket, F_SETFL, flags | O_NONBLOCK);<\/p>\n<p>\/\/ \u5c1d\u8bd5\u975e\u963b\u585e\u8bfb\u53d6<br \/>\nint n &#061; read(new_socket, buffer, 1024);<br \/>\nif (n &lt; 0) {<br \/>\n    if (errno &#061;&#061; EWOULDBLOCK) {<br \/>\n        \/\/ \u6ca1\u6709\u6570\u636e\u53ef\u8bfb&#xff0c;\u4e0d\u963b\u585e<br \/>\n    } else {<br \/>\n        \/\/ \u53d1\u751f\u9519\u8bef<br \/>\n    }<br \/>\n}<\/p>\n<p> \u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d&#xff0c;\u6211\u4eec\u9996\u5148\u83b7\u53d6\u4e86\u5957\u63a5\u5b57\u7684\u5f53\u524d\u6807\u5fd7&#xff0c;\u7136\u540e\u6dfb\u52a0\u4e86  O_NONBLOCK  \u6807\u5fd7&#xff0c;\u4f7f\u5f97\u5957\u63a5\u5b57\u53d8\u4e3a\u975e\u963b\u585e\u6a21\u5f0f\u3002\u5f53\u5c1d\u8bd5\u8bfb\u53d6\u6570\u636e\u65f6&#xff0c;\u5982\u679c\u6ca1\u6709\u6570\u636e\u53ef\u8bfb&#xff0c;\u51fd\u6570\u5c06\u7acb\u5373\u8fd4\u56de&#xff0c;\u5e76\u4e14  errno  \u88ab\u8bbe\u7f6e\u4e3a  EWOULDBLOCK  \u3002 <\/p>\n<h3> 4.4 \u7f51\u7edc\u5b89\u5168\u6ce8\u610f\u4e8b\u9879 <\/h3>\n<p> \u5728\u8fdb\u884c\u7f51\u7edc\u7f16\u7a0b\u65f6&#xff0c;\u5b89\u5168\u6027\u662f\u4e0d\u53ef\u5ffd\u89c6\u7684\u91cd\u8981\u65b9\u9762\u3002\u5f00\u53d1\u8005\u5e94\u8be5\u4e86\u89e3\u5e38\u89c1\u7684\u7f51\u7edc\u653b\u51fb\u624b\u6bb5&#xff0c;\u5982\u62d2\u7edd\u670d\u52a1&#xff08;DoS&#xff09;\u653b\u51fb\u3001\u4e2d\u95f4\u4eba&#xff08;MITM&#xff09;\u653b\u51fb\u7b49&#xff0c;\u5e76\u91c7\u53d6\u76f8\u5e94\u63aa\u65bd\u8fdb\u884c\u9632\u62a4\u3002\u4f7f\u7528\u52a0\u5bc6\u901a\u4fe1\u3001\u8eab\u4efd\u9a8c\u8bc1\u548c\u6388\u6743\u673a\u5236\u662f\u4fdd\u969c\u7f51\u7edc\u5b89\u5168\u7684\u5e38\u7528\u624b\u6bb5\u3002 <\/p>\n<h4> \u8868\u683c3&#xff1a;\u7f51\u7edc\u5b89\u5168\u9632\u62a4\u7b56\u7565 <\/h4>\n<p> | \u7b56\u7565 | \u63cf\u8ff0 | | &#8212; | &#8212; | | \u52a0\u5bc6\u901a\u4fe1 | \u4f7f\u7528SSL\/TLS\u7b49\u52a0\u5bc6\u534f\u8bae\u4fdd\u62a4\u6570\u636e\u4f20\u8f93 | | \u8eab\u4efd\u9a8c\u8bc1 | \u9a8c\u8bc1\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u7684\u8eab\u4efd&#xff0c;\u5982\u4f7f\u7528\u7528\u6237\u540d\u548c\u5bc6\u7801 | | \u6388\u6743\u673a\u5236 | \u786e\u4fdd\u7528\u6237\u53ea\u80fd\u8bbf\u95ee\u4ed6\u4eec\u88ab\u6388\u6743\u7684\u6570\u636e\u6216\u670d\u52a1 | | \u9632DoS\u653b\u51fb | \u9650\u5236\u8fde\u63a5\u901f\u7387\u3001\u4f7f\u7528\u9632\u706b\u5899\u548c\u5165\u4fb5\u68c0\u6d4b\u7cfb\u7edf | | \u9632MITM\u653b\u51fb | \u4f7f\u7528\u52a0\u5bc6\u548c\u8bc1\u4e66\u9a8c\u8bc1\u673a\u5236 | <\/p>\n<p> \u901a\u8fc7\u5728\u8bbe\u8ba1\u548c\u5b9e\u73b0\u9636\u6bb5\u8003\u8651\u8fd9\u4e9b\u7b56\u7565&#xff0c;\u7f51\u7edc\u5e94\u7528\u53ef\u4ee5\u63d0\u9ad8\u5176\u5b89\u5168\u6027&#xff0c;\u51cf\u5c11\u906d\u53d7\u7f51\u7edc\u653b\u51fb\u7684\u98ce\u9669\u3002 <\/p>\n<p> \u7f51\u7edc\u7f16\u7a0b\u662f\u4e00\u4e2a\u590d\u6742\u4e14\u4e0d\u65ad\u53d1\u5c55\u7684\u9886\u57df&#xff0c;\u6d89\u53ca\u5230\u7684\u77e5\u8bc6\u548c\u6280\u80fd\u975e\u5e38\u5e7f\u6cdb\u3002C&#043;&#043;\u5f00\u53d1\u8005\u5728\u638c\u63e1\u4e86\u57fa\u7840\u7684\u7f51\u7edc\u7f16\u7a0b\u6982\u5ff5\u548c\u5e76\u53d1\u5904\u7406\u6280\u672f\u4e4b\u540e&#xff0c;\u8fd8\u9700\u8981\u4e0d\u65ad\u5730\u5b66\u4e60\u548c\u5b9e\u8df5&#xff0c;\u4ee5\u5e94\u5bf9\u65e5\u76ca\u590d\u6742\u7684\u7f51\u7edc\u5e94\u7528\u5f00\u53d1\u9700\u6c42\u3002\u5728\u672c\u7ae0\u4e2d&#xff0c;\u6211\u4eec\u4ecb\u7ecd\u4e86socket\u7f16\u7a0b\u7684\u57fa\u7840&#xff0c;\u63a2\u8ba8\u4e86\u5e76\u53d1\u5904\u7406\u7684\u4e0d\u540c\u6a21\u578b&#xff0c;\u5e76\u5206\u4eab\u4e86\u4e00\u4e9b\u5b9e\u7528\u7684\u7f51\u7edc\u7f16\u7a0b\u6280\u5de7\u3002\u5e0c\u671b\u8fd9\u4e9b\u77e5\u8bc6\u80fd\u591f\u5e2e\u52a9\u4f60\u5728\u7f51\u7edc\u5e94\u7528\u5f00\u53d1\u4e2d\u53d6\u5f97\u6210\u529f\u3002 <\/p>\n<h2> 5. C&#043;&#043;\u6807\u51c6\u6a21\u677f\u5e93&#xff08;STL&#xff09;\u7684\u4f7f\u7528 <\/h2>\n<h3> 5.1 STL\u57fa\u7840\u6982\u5ff5\u4ecb\u7ecd <\/h3>\n<p> C&#043;&#043;\u6807\u51c6\u6a21\u677f\u5e93&#xff08;STL&#xff09;\u662f\u4e00\u7cfb\u5217\u6a21\u677f\u7c7b\u548c\u51fd\u6570\u7684\u96c6\u5408&#xff0c;\u5b83\u88ab\u8bbe\u8ba1\u7528\u6765\u5904\u7406\u5e38\u89c1\u7684\u7f16\u7a0b\u4efb\u52a1&#xff0c;\u5982\u6570\u636e\u7ed3\u6784\u7684\u5b9e\u73b0\u548c\u7b97\u6cd5\u7684\u5e94\u7528\u3002STL\u7531\u4ee5\u4e0b\u51e0\u90e8\u5206\u7ec4\u6210&#xff1a; <\/p>\n<ul>\n<li> \u5bb9\u5668&#xff08;Containers&#xff09;&#xff1a;\u7528\u4e8e\u5b58\u50a8\u6570\u636e\u3002 <\/li>\n<li> \u8fed\u4ee3\u5668&#xff08;Iterators&#xff09;&#xff1a;\u63d0\u4f9b\u8bbf\u95ee\u5bb9\u5668\u5185\u5143\u7d20\u7684\u65b9\u5f0f\u3002 <\/li>\n<li> \u7b97\u6cd5&#xff08;Algorithms&#xff09;&#xff1a;\u63d0\u4f9b\u6570\u636e\u64cd\u4f5c\u529f\u80fd\u3002 <\/li>\n<li> \u9002\u914d\u5668&#xff08;Adapters&#xff09;&#xff1a;\u5bb9\u5668\u548c\u8fed\u4ee3\u5668\u7684\u7279\u6b8a\u5f62\u5f0f\u3002 <\/li>\n<li> \u5206\u914d\u5668&#xff08;Allocators&#xff09;&#xff1a;\u7528\u4e8e\u7ba1\u7406\u5185\u5b58\u5206\u914d\u3002 <\/li>\n<\/ul>\n<p> \u7406\u89e3\u8fd9\u4e9b\u57fa\u7840\u6982\u5ff5&#xff0c;\u662f\u5229\u7528STL\u89e3\u51b3\u95ee\u9898\u7684\u7b2c\u4e00\u6b65\u3002 <\/p>\n<h3> 5.2 \u5bb9\u5668\u7684\u4f7f\u7528 <\/h3>\n<p> STL\u63d0\u4f9b\u4e86\u591a\u79cd\u5bb9\u5668&#xff0c;\u6bcf\u79cd\u5bb9\u5668\u90fd\u6709\u5176\u7279\u5b9a\u7684\u7528\u9014\u548c\u6027\u80fd\u7279\u5f81\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u5bb9\u5668\u548c\u5b83\u4eec\u7684\u57fa\u672c\u4f7f\u7528\u793a\u4f8b&#xff1a; <\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;vector&gt;<br \/>\n#include &lt;list&gt;<br \/>\n#include &lt;map&gt;<\/p>\n<p>int main() {<br \/>\n    \/\/ \u5411\u91cf&#xff08;Vector&#xff09;\u7684\u4f7f\u7528<br \/>\n    std::vector&lt;int&gt; vec &#061; {1, 2, 3, 4, 5};<br \/>\n    vec.push_back(6); \/\/ \u5728\u672b\u5c3e\u6dfb\u52a0\u5143\u7d20<\/p>\n<p>    \/\/ \u5217\u8868&#xff08;List&#xff09;\u7684\u4f7f\u7528<br \/>\n    std::list&lt;int&gt; lst;<br \/>\n    lst.push_back(1);<br \/>\n    lst.push_front(0);<\/p>\n<p>    \/\/ \u6620\u5c04&#xff08;Map&#xff09;\u7684\u4f7f\u7528<br \/>\n    std::map&lt;std::string, int&gt; m;<br \/>\n    m[&#034;one&#034;] &#061; 1;<br \/>\n    m[&#034;two&#034;] &#061; 2;<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<p> \u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d&#xff0c;\u6211\u4eec\u5c55\u793a\u4e86\u5982\u4f55\u521d\u59cb\u5316\u548c\u64cd\u4f5c\u5411\u91cf&#xff08;Vector&#xff09;\u3001\u5217\u8868&#xff08;List&#xff09;\u548c\u6620\u5c04&#xff08;Map&#xff09;\u3002\u8fd9\u4e9b\u5bb9\u5668\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6210\u5458\u51fd\u6570\u6765\u6dfb\u52a0\u3001\u5220\u9664\u3001\u8bbf\u95ee\u5143\u7d20\u7b49\u3002 <\/p>\n<h3> 5.3 \u8fed\u4ee3\u5668\u4e0e\u7b97\u6cd5\u7684\u8fd0\u7528 <\/h3>\n<p> \u8fed\u4ee3\u5668\u662fSTL\u7684\u57fa\u77f3&#xff0c;\u5b83\u5141\u8bb8\u7b97\u6cd5\u72ec\u7acb\u4e8e\u5bb9\u5668\u7684\u5177\u4f53\u7c7b\u578b\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528\u8fed\u4ee3\u5668\u548c\u7b97\u6cd5\u7684\u793a\u4f8b&#xff1a; <\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;vector&gt;<br \/>\n#include &lt;algorithm&gt;<\/p>\n<p>int main() {<br \/>\n    std::vector&lt;int&gt; vec &#061; {5, 2, 8, 3, 1};<br \/>\n    std::sort(vec.begin(), vec.end()); \/\/ \u4f7f\u7528\u8fed\u4ee3\u5668\u4f5c\u4e3a\u53c2\u6570\u7684\u7b97\u6cd5\u793a\u4f8b<\/p>\n<p>    for (auto it &#061; vec.begin(); it !&#061; vec.end(); &#043;&#043;it) {<br \/>\n        std::cout &lt;&lt; *it &lt;&lt; &#039; &#039;; \/\/ \u4f7f\u7528\u8fed\u4ee3\u5668\u904d\u5386\u5bb9\u5668<br \/>\n    }<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<p> \u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d&#xff0c;\u6211\u4eec\u4f7f\u7528\u4e86  std::sort  \u7b97\u6cd5\u548c\u8fed\u4ee3\u5668\u5bf9\u5411\u91cf\u8fdb\u884c\u6392\u5e8f\u3002\u8fed\u4ee3\u5668\u5728\u7b97\u6cd5\u548c\u5bb9\u5668\u4e4b\u95f4\u8d77\u5230\u6865\u6881\u4f5c\u7528&#xff0c;\u4f7f\u5f97\u7b97\u6cd5\u53ef\u4ee5\u8de8\u4e0d\u540c\u7c7b\u578b\u7684\u5bb9\u5668\u5de5\u4f5c\u3002 <\/p>\n<h3> 5.4 STL\u9ad8\u7ea7\u7528\u6cd5 <\/h3>\n<p> STL\u4e0d\u4ec5\u9002\u7528\u4e8e\u7b80\u5355\u7684\u6570\u636e\u64cd\u4f5c&#xff0c;\u5b83\u8fd8\u53ef\u4ee5\u5904\u7406\u66f4\u590d\u6742\u7684\u4efb\u52a1\u3002\u4e00\u4e9b\u9ad8\u7ea7\u7528\u6cd5\u5305\u62ec&#xff1a; <\/p>\n<ul>\n<li>  \u51fd\u6570\u5bf9\u8c61&#xff08;Function Objects&#xff09;  &#xff1a;\u53ef\u4ee5\u50cf\u666e\u901a\u51fd\u6570\u4e00\u6837\u88ab\u8c03\u7528\u7684\u7c7b\u5bf9\u8c61\u3002 <\/li>\n<li>  lambda\u8868\u8fbe\u5f0f  &#xff1a;\u5728C&#043;&#043;11\u4e2d\u5f15\u5165&#xff0c;\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u7b80\u6d01\u7684\u65b9\u5f0f\u6765\u5b9a\u4e49\u51fd\u6570\u5bf9\u8c61\u3002 <\/li>\n<li>  \u7b97\u6cd5\u7684\u81ea\u5b9a\u4e49\u6bd4\u8f83  &#xff1a;\u901a\u8fc7\u4f20\u9012\u81ea\u5b9a\u4e49\u6bd4\u8f83\u51fd\u6570\u6216lambda\u8868\u8fbe\u5f0f\u6765\u63a7\u5236\u7b97\u6cd5\u7684\u884c\u4e3a\u3002 <\/li>\n<\/ul>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;vector&gt;<br \/>\n#include &lt;algorithm&gt;<\/p>\n<p>int main() {<br \/>\n    std::vector&lt;int&gt; vec &#061; {1, 2, 3, 4, 5};<\/p>\n<p>    \/\/ \u4f7f\u7528lambda\u8868\u8fbe\u5f0f\u627e\u5230\u7b2c\u4e00\u4e2a\u5927\u4e8e3\u7684\u5143\u7d20<br \/>\n    auto it &#061; std::find_if(vec.begin(), vec.end(), [](int x) { return x &gt; 3; });<br \/>\n    if (it !&#061; vec.end()) {<br \/>\n        std::cout &lt;&lt; &#034;Found &#034; &lt;&lt; *it &lt;&lt; &#034; which is greater than 3&#034; &lt;&lt; std::endl;<br \/>\n    }<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<p> \u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d&#xff0c;\u6211\u4eec\u6f14\u793a\u4e86\u4f7f\u7528lambda\u8868\u8fbe\u5f0f\u548c  std::find_if  \u7b97\u6cd5\u6765\u67e5\u627e\u5411\u91cf\u4e2d\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002 <\/p>\n<p> STL\u7684\u6df1\u5165\u4f7f\u7528\u6709\u52a9\u4e8e\u7f16\u5199\u66f4\u4f18\u96c5\u3001\u66f4\u9ad8\u6548\u7684C&#043;&#043;\u4ee3\u7801\u3002\u638c\u63e1STL\u4e0d\u4ec5\u80fd\u591f\u63d0\u5347\u5f00\u53d1\u8005\u7684\u4ee3\u7801\u80fd\u529b&#xff0c;\u8fd8\u80fd\u8ba9\u4ed6\u4eec\u80fd\u591f\u66f4\u7075\u6d3b\u5730\u5904\u7406\u5404\u79cd\u7f16\u7a0b\u6311\u6218\u3002 <\/p>\n<p>  \u672c\u6587\u8fd8\u6709\u914d\u5957\u7684\u7cbe\u54c1\u8d44\u6e90&#xff0c;\u70b9\u51fb\u83b7\u53d6  <img decoding=\"async\" alt=\"menu-r.4af5f7ec.gif\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250612002053-684a1d65d7153.gif\" style=\"width:16px;margin-left:4px;vertical-align:text-bottom;cursor:text\" \/> <\/p>\n<p> \u7b80\u4ecb&#xff1a;\u672c\u6559\u7a0b\u5408\u96c6\u8be6\u7ec6\u8bb2\u89e3\u4e86\u5728Linux\u73af\u5883\u4e0b\u8fdb\u884cC&#043;&#043;\u7f16\u7a0b\u7684\u5404\u4e2a\u65b9\u9762\u3002\u4ece\u57fa\u7840\u8bed\u6cd5\u3001\u670d\u52a1\u5668\u5f00\u53d1\u5230\u9ad8\u7ea7\u4e13\u9898&#xff0c;\u6559\u7a0b\u5e2e\u52a9\u7a0b\u5e8f\u5458\u4eec\u5728Linux\u7cfb\u7edf\u4e0a\u6709\u6548\u638c\u63e1C&#043;&#043;\u5f00\u53d1\u3002\u5185\u5bb9\u5305\u62ecC&#043;&#043;\u57fa\u7840\u6982\u5ff5\u3001GCC\u7f16\u8bd1\u8c03\u8bd5\u3001\u5185\u5b58\u7ba1\u7406\u3001socket\u7f16\u7a0b\u3001\u591a\u7ebf\u7a0b\u4e0e\u5f02\u6b65I\/O\u6a21\u578b\u3001STL\u5e94\u7528\u3001\u6a21\u677f\u5143\u7f16\u7a0b\u3001\u73b0\u4ee3C&#043;&#043;\u65b0\u7279\u6027\u3001\u7248\u672c\u63a7\u5236&#xff08;\u5982Git&#xff09;\u3001\u9879\u76ee\u6784\u5efa\u5de5\u5177&#xff08;\u5982Makefile\/CMake&#xff09;\u4ee5\u53ca\u8c03\u8bd5\u6280\u5de7\u548c\u6027\u80fd\u5206\u6790\u5de5\u5177&#xff08;\u5982GDB\u3001gprof\u548cvalgrind&#xff09;\u3002\u901a\u8fc7\u5b9e\u8df5\u4e0e\u7406\u8bba\u5b66\u4e60\u7684\u7ed3\u5408&#xff0c;\u672c\u6559\u7a0b\u65e8\u5728\u63d0\u4f9b\u7ed9\u521d\u5b66\u8005\u53ca\u6709\u7ecf\u9a8c\u7684\u5f00\u53d1\u8005\u5168\u9762\u800c\u6df1\u5165\u7684\u5b66\u4e60\u8d44\u6e90\u3002 <\/p>\n<p>  \u672c\u6587\u8fd8\u6709\u914d\u5957\u7684\u7cbe\u54c1\u8d44\u6e90&#xff0c;\u70b9\u51fb\u83b7\u53d6  <img decoding=\"async\" alt=\"menu-r.4af5f7ec.gif\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250612002053-684a1d65d7153.gif\" style=\"width:16px;margin-left:4px;vertical-align:text-bottom;cursor:text\" \/> <\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb758\u6b21\uff0c\u70b9\u8d5e23\u6b21\uff0c\u6536\u85cf13\u6b21\u3002C++\u662f\u4e00\u79cd\u5e7f\u6cdb\u4f7f\u7528\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u56e0\u5176\u6027\u80fd\u4f18\u5f02\uff0c\u7279\u522b\u662f\u5728\u7cfb\u7edf\u7f16\u7a0b\u3001\u6e38\u620f\u5f00\u53d1\u3001\u5d4c\u5165\u5f0f\u7cfb\u7edf\u7b49\u9886\u57df\u5907\u53d7\u9752\u7750\u3002\u5b83\u5141\u8bb8\u5f00\u53d1\u8005\u901a\u8fc7\u9762\u5411\u5bf9\u8c61\u7684\u65b9\u5f0f\u5bf9\u4ee3\u7801\u8fdb\u884c\u5c01\u88c5\u3001\u7ee7\u627f\u548c\u591a\u6001\u5904\u7406\uff0c\u6781\u5927\u63d0\u5347\u4e86\u4ee3\u7801\u7684\u53ef\u91cd\u7528\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u5728Linux\u7cfb\u7edf\u4e2d\uff0c\u6bcf\u4e2a\u8fdb\u7a0b\u90fd\u8fd0\u884c\u5728\u81ea\u5df1\u7684\u865a\u62df\u5185\u5b58\u7a7a\u95f4\u4e2d\u3002\u865a\u62df\u5185\u5b58\u7a7a\u95f4\u662f\u8fde\u7eed\u7684\u5730\u5740\u8303\u56f4\uff0c\u800c\u7269\u7406\u5185\u5b58\u5219\u662f\u7269\u7406\u4e0a\u7684RAM\u3002\u64cd\u4f5c\u7cfb\u7edf\u901a\u8fc7\u865a\u62df\u5185\u5b58\u7ba1\u7406\u6280\u672f\uff08VM\uff09\u5c06\u865a\u62df\u5185\u5b58\u6620\u5c04\u5230\u7269\u7406\u5185\u5b58\u4e0a\uff0c\u5b9e\u73b0\u8fdb\u7a0b\u95f4\u5185\u5b58\u7684\u9694\u79bb\u548c\u4fdd\u62a4\u3002<\/p>\n","protected":false},"author":2,"featured_media":43357,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"topic":[],"class_list":["post-43359","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Linux\u73af\u5883\u4e0bC++\u7f16\u7a0b\u7efc\u5408\u6559\u7a0b\uff1a\u57fa\u7840\u3001\u670d\u52a1\u5668\u4e0e\u4e13\u9898\u6df1\u5165 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/43359.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Linux\u73af\u5883\u4e0bC++\u7f16\u7a0b\u7efc\u5408\u6559\u7a0b\uff1a\u57fa\u7840\u3001\u670d\u52a1\u5668\u4e0e\u4e13\u9898\u6df1\u5165 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb758\u6b21\uff0c\u70b9\u8d5e23\u6b21\uff0c\u6536\u85cf13\u6b21\u3002C++\u662f\u4e00\u79cd\u5e7f\u6cdb\u4f7f\u7528\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u56e0\u5176\u6027\u80fd\u4f18\u5f02\uff0c\u7279\u522b\u662f\u5728\u7cfb\u7edf\u7f16\u7a0b\u3001\u6e38\u620f\u5f00\u53d1\u3001\u5d4c\u5165\u5f0f\u7cfb\u7edf\u7b49\u9886\u57df\u5907\u53d7\u9752\u7750\u3002\u5b83\u5141\u8bb8\u5f00\u53d1\u8005\u901a\u8fc7\u9762\u5411\u5bf9\u8c61\u7684\u65b9\u5f0f\u5bf9\u4ee3\u7801\u8fdb\u884c\u5c01\u88c5\u3001\u7ee7\u627f\u548c\u591a\u6001\u5904\u7406\uff0c\u6781\u5927\u63d0\u5347\u4e86\u4ee3\u7801\u7684\u53ef\u91cd\u7528\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u5728Linux\u7cfb\u7edf\u4e2d\uff0c\u6bcf\u4e2a\u8fdb\u7a0b\u90fd\u8fd0\u884c\u5728\u81ea\u5df1\u7684\u865a\u62df\u5185\u5b58\u7a7a\u95f4\u4e2d\u3002\u865a\u62df\u5185\u5b58\u7a7a\u95f4\u662f\u8fde\u7eed\u7684\u5730\u5740\u8303\u56f4\uff0c\u800c\u7269\u7406\u5185\u5b58\u5219\u662f\u7269\u7406\u4e0a\u7684RAM\u3002\u64cd\u4f5c\u7cfb\u7edf\u901a\u8fc7\u865a\u62df\u5185\u5b58\u7ba1\u7406\u6280\u672f\uff08VM\uff09\u5c06\u865a\u62df\u5185\u5b58\u6620\u5c04\u5230\u7269\u7406\u5185\u5b58\u4e0a\uff0c\u5b9e\u73b0\u8fdb\u7a0b\u95f4\u5185\u5b58\u7684\u9694\u79bb\u548c\u4fdd\u62a4\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/43359.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-06-12T00:20:55+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250612002053-684a1d65d7153.gif\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/43359.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/43359.html\",\"name\":\"Linux\u73af\u5883\u4e0bC++\u7f16\u7a0b\u7efc\u5408\u6559\u7a0b\uff1a\u57fa\u7840\u3001\u670d\u52a1\u5668\u4e0e\u4e13\u9898\u6df1\u5165 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-06-12T00:20:55+00:00\",\"dateModified\":\"2025-06-12T00:20:55+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/43359.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/43359.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/43359.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Linux\u73af\u5883\u4e0bC++\u7f16\u7a0b\u7efc\u5408\u6559\u7a0b\uff1a\u57fa\u7840\u3001\u670d\u52a1\u5668\u4e0e\u4e13\u9898\u6df1\u5165\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Linux\u73af\u5883\u4e0bC++\u7f16\u7a0b\u7efc\u5408\u6559\u7a0b\uff1a\u57fa\u7840\u3001\u670d\u52a1\u5668\u4e0e\u4e13\u9898\u6df1\u5165 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/43359.html","og_locale":"zh_CN","og_type":"article","og_title":"Linux\u73af\u5883\u4e0bC++\u7f16\u7a0b\u7efc\u5408\u6559\u7a0b\uff1a\u57fa\u7840\u3001\u670d\u52a1\u5668\u4e0e\u4e13\u9898\u6df1\u5165 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb758\u6b21\uff0c\u70b9\u8d5e23\u6b21\uff0c\u6536\u85cf13\u6b21\u3002C++\u662f\u4e00\u79cd\u5e7f\u6cdb\u4f7f\u7528\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u56e0\u5176\u6027\u80fd\u4f18\u5f02\uff0c\u7279\u522b\u662f\u5728\u7cfb\u7edf\u7f16\u7a0b\u3001\u6e38\u620f\u5f00\u53d1\u3001\u5d4c\u5165\u5f0f\u7cfb\u7edf\u7b49\u9886\u57df\u5907\u53d7\u9752\u7750\u3002\u5b83\u5141\u8bb8\u5f00\u53d1\u8005\u901a\u8fc7\u9762\u5411\u5bf9\u8c61\u7684\u65b9\u5f0f\u5bf9\u4ee3\u7801\u8fdb\u884c\u5c01\u88c5\u3001\u7ee7\u627f\u548c\u591a\u6001\u5904\u7406\uff0c\u6781\u5927\u63d0\u5347\u4e86\u4ee3\u7801\u7684\u53ef\u91cd\u7528\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u5728Linux\u7cfb\u7edf\u4e2d\uff0c\u6bcf\u4e2a\u8fdb\u7a0b\u90fd\u8fd0\u884c\u5728\u81ea\u5df1\u7684\u865a\u62df\u5185\u5b58\u7a7a\u95f4\u4e2d\u3002\u865a\u62df\u5185\u5b58\u7a7a\u95f4\u662f\u8fde\u7eed\u7684\u5730\u5740\u8303\u56f4\uff0c\u800c\u7269\u7406\u5185\u5b58\u5219\u662f\u7269\u7406\u4e0a\u7684RAM\u3002\u64cd\u4f5c\u7cfb\u7edf\u901a\u8fc7\u865a\u62df\u5185\u5b58\u7ba1\u7406\u6280\u672f\uff08VM\uff09\u5c06\u865a\u62df\u5185\u5b58\u6620\u5c04\u5230\u7269\u7406\u5185\u5b58\u4e0a\uff0c\u5b9e\u73b0\u8fdb\u7a0b\u95f4\u5185\u5b58\u7684\u9694\u79bb\u548c\u4fdd\u62a4\u3002","og_url":"https:\/\/www.wsisp.com\/helps\/43359.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-06-12T00:20:55+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250612002053-684a1d65d7153.gif"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"7 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/43359.html","url":"https:\/\/www.wsisp.com\/helps\/43359.html","name":"Linux\u73af\u5883\u4e0bC++\u7f16\u7a0b\u7efc\u5408\u6559\u7a0b\uff1a\u57fa\u7840\u3001\u670d\u52a1\u5668\u4e0e\u4e13\u9898\u6df1\u5165 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-06-12T00:20:55+00:00","dateModified":"2025-06-12T00:20:55+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/43359.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/43359.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/43359.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Linux\u73af\u5883\u4e0bC++\u7f16\u7a0b\u7efc\u5408\u6559\u7a0b\uff1a\u57fa\u7840\u3001\u670d\u52a1\u5668\u4e0e\u4e13\u9898\u6df1\u5165"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/43359","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=43359"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/43359\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/43357"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=43359"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=43359"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=43359"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=43359"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}