{"id":40886,"date":"2025-05-30T12:26:26","date_gmt":"2025-05-30T04:26:26","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/40886.html"},"modified":"2025-05-30T12:26:26","modified_gmt":"2025-05-30T04:26:26","slug":"c-%e9%93%be%e6%8e%a5%e9%94%99%e8%af%af%ef%bc%9a%e5%a4%9a%e9%87%8d%e5%ae%9a%e4%b9%89%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/40886.html","title":{"rendered":"C++ \u94fe\u63a5\u9519\u8bef\uff1a\u591a\u91cd\u5b9a\u4e49\u95ee\u9898"},"content":{"rendered":"<h2>C&#043;&#043; \u94fe\u63a5\u9519\u8bef&#xff1a;\u591a\u91cd\u5b9a\u4e49\u95ee\u9898<\/h2>\n<h3>1. \u9519\u8bef\u539f\u56e0<\/h3>\n<p>\u94fe\u63a5\u5668\u53d1\u73b0\u540c\u4e00\u4e2a\u7b26\u53f7&#xff08;\u51fd\u6570\/\u53d8\u91cf&#xff09;\u5728\u591a\u4e2a\u7f16\u8bd1\u5355\u5143&#xff08;.obj \u6587\u4ef6&#xff09;\u4e2d\u88ab\u91cd\u590d\u5b9a\u4e49&#xff0c;\u5bfc\u81f4\u51b2\u7a81\u3002\u5e38\u89c1\u573a\u666f&#xff1a;<\/p>\n<hr \/>\n<h3>2. \u5e38\u89c1\u573a\u666f\u53ca\u89e3\u51b3\u65b9\u6848<\/h3>\n<h4>2.1 \u5934\u6587\u4ef6\u4e2d\u76f4\u63a5\u5b9a\u4e49\u51fd\u6570\/\u53d8\u91cf<\/h4>\n<h5>\u95ee\u9898\u63cf\u8ff0<\/h5>\n<p>\u5728\u5934\u6587\u4ef6&#xff08;.h&#xff09;\u4e2d\u76f4\u63a5\u5b9a\u4e49\u975e\u5185\u8054\u51fd\u6570\u6216\u5168\u5c40\u53d8\u91cf&#xff0c;\u4e14\u8be5\u5934\u6587\u4ef6\u88ab\u591a\u4e2a\u6e90\u6587\u4ef6\u5305\u542b\u3002<\/p>\n<p><span class=\"token comment\">\/\/ utils.h<\/span><br \/>\n<span class=\"token keyword\">void<\/span> <span class=\"token function\">badFunc<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span> <span class=\"token comment\">\/* \u5b9e\u73b0 *\/<\/span> <span class=\"token punctuation\">}<\/span>  <span class=\"token comment\">\/\/ \u9519\u8bef&#xff01;\u975e\u5185\u8054\u51fd\u6570\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u4e2d<\/span><br \/>\n<span class=\"token keyword\">int<\/span> g_value <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">50<\/span><span class=\"token punctuation\">;<\/span>              <span class=\"token comment\">\/\/ \u9519\u8bef&#xff01;\u5168\u5c40\u53d8\u91cf\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u4e2d<\/span><\/p>\n<h5>\u89e3\u51b3\u65b9\u6848<\/h5>\n<ul>\n<li>\n<p>\u58f0\u660e\u4e0e\u5b9a\u4e49\u5206\u79bb&#xff1a;<\/p>\n<p> <span class=\"token comment\">\/\/ utils.h<\/span><br \/>\n<span class=\"token keyword\">void<\/span> <span class=\"token function\">goodFunc<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>  <span class=\"token comment\">\/\/ \u4ec5\u58f0\u660e<\/span><br \/>\n<span class=\"token keyword\">extern<\/span> <span class=\"token keyword\">int<\/span> g_value<span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/ \u58f0\u660e<\/span><\/p>\n<p><span class=\"token comment\">\/\/ utils.cpp<\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&#034;utils.h&#034;<\/span><\/span><br \/>\n<span class=\"token keyword\">void<\/span> <span class=\"token function\">goodFunc<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span> <span class=\"token comment\">\/* \u5b9a\u4e49 *\/<\/span> <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token keyword\">int<\/span> g_value <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">42<\/span><span class=\"token punctuation\">;<\/span>    <span class=\"token comment\">\/\/ \u5b9a\u4e49<\/span>\n <\/li>\n<li>\n<p>\u4f7f\u7528 inline \u5173\u952e\u5b57&#xff08;\u9002\u5408\u7b80\u5355\u51fd\u6570&#xff09;&#xff1a;<\/p>\n<p> <span class=\"token comment\">\/\/ utils.h<\/span><br \/>\n<span class=\"token keyword\">inline<\/span> <span class=\"token keyword\">void<\/span> <span class=\"token function\">quickCalc<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span> <span class=\"token comment\">\/* \u5185\u8054\u5b9a\u4e49 *\/<\/span> <span class=\"token punctuation\">}<\/span>\n <\/li>\n<\/ul>\n<h5>\u666e\u901a\u51fd\u6570\u4e3a\u4ec0\u4e48\u4e0d\u80fd\u76f4\u63a5\u5199\u5728\u5934\u6587\u4ef6\u4e2d<\/h5>\n<p>\u5982\u679c\u666e\u901a\u51fd\u6570&#xff08;\u975e inline\u3001\u975e\u6a21\u677f&#xff09;\u7684\u5b9a\u4e49\u76f4\u63a5\u5199\u5728\u5934\u6587\u4ef6\u4e2d&#xff0c;\u4e14\u8be5\u5934\u6587\u4ef6\u88ab\u591a\u4e2a\u6e90\u6587\u4ef6\u5305\u542b&#xff0c;\u4f1a\u5bfc\u81f4\u6bcf\u4e2a\u6e90\u6587\u4ef6\u90fd\u751f\u6210\u4e00\u4efd\u8be5\u51fd\u6570\u7684\u4ee3\u7801\u3002\u94fe\u63a5\u65f6&#xff0c;\u94fe\u63a5\u5668\u4f1a\u53d1\u73b0\u591a\u4e2a\u76f8\u540c\u7684\u51fd\u6570\u5b9a\u4e49&#xff0c;\u4ece\u800c\u5f15\u53d1\u91cd\u5b9a\u4e49\u7684\u9519\u8bef<\/p>\n<hr \/>\n<h4>2.2 \u591a\u4e2a\u6e90\u6587\u4ef6\u5b9a\u4e49\u4e86\u76f8\u540c\u51fd\u6570\/\u53d8\u91cf<\/h4>\n<h5>\u95ee\u9898\u63cf\u8ff0<\/h5>\n<p>\u591a\u4e2a .cpp \u6587\u4ef6\u72ec\u7acb\u5b9e\u73b0\u4e86\u540c\u540d\u7684\u5168\u5c40\u51fd\u6570\u6216\u53d8\u91cf\u3002<\/p>\n<p><span class=\"token comment\">\/\/ file1.cpp<\/span><br \/>\n<span class=\"token keyword\">void<\/span> <span class=\"token function\">myFunc<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span> <span class=\"token comment\">\/* \u5b9e\u73b01 *\/<\/span> <span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token comment\">\/\/ file2.cpp<\/span><br \/>\n<span class=\"token keyword\">void<\/span> <span class=\"token function\">myFunc<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span> <span class=\"token comment\">\/* \u5b9e\u73b02 *\/<\/span> <span class=\"token punctuation\">}<\/span> <span class=\"token comment\">\/\/ \u9519\u8bef&#xff01;\u91cd\u590d\u5b9a\u4e49<\/span><\/p>\n<h5>\u89e3\u51b3\u65b9\u6848<\/h5>\n<ul>\n<li>\n<p>\u4fdd\u7559\u4e00\u4e2a\u5b9a\u4e49&#xff0c;\u5176\u4ed6\u6587\u4ef6\u6539\u4e3a\u58f0\u660e&#xff1a;<\/p>\n<p> <span class=\"token comment\">\/\/ file1.cpp<\/span><br \/>\n<span class=\"token keyword\">void<\/span> <span class=\"token function\">myFunc<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span> <span class=\"token comment\">\/* \u5b9e\u73b0 *\/<\/span> <span class=\"token punctuation\">}<\/span>  <span class=\"token comment\">\/\/ \u552f\u4e00\u5b9a\u4e49<\/span><\/p>\n<p><span class=\"token comment\">\/\/ file2.cpp<\/span><br \/>\n<span class=\"token keyword\">void<\/span> <span class=\"token function\">myFunc<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>  <span class=\"token comment\">\/\/ \u4ec5\u58f0\u660e&#xff08;\u901a\u8fc7\u5934\u6587\u4ef6\u66f4\u89c4\u8303&#xff09;<\/span>\n <\/li>\n<li>\n<p>\u4f7f\u7528 static \u9650\u5236\u4f5c\u7528\u57df&#xff1a;<\/p>\n<p> <span class=\"token comment\">\/\/ file1.cpp<\/span><br \/>\n<span class=\"token keyword\">static<\/span> <span class=\"token keyword\">void<\/span> <span class=\"token function\">localFunc<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span> <span class=\"token comment\">\/* \u4ec5\u5728\u6b64\u6587\u4ef6\u53ef\u89c1 *\/<\/span> <span class=\"token punctuation\">}<\/span>\n <\/li>\n<\/ul>\n<hr \/>\n<h4>2.3 \u91cd\u590d\u5305\u542b\u6e90\u6587\u4ef6<\/h4>\n<h5>\u95ee\u9898\u63cf\u8ff0<\/h5>\n<ul>\n<li>\u9519\u8bef\u5730\u4f7f\u7528 #include &#034;xxx.cpp&#034;\u3002<\/li>\n<li>\u6784\u5efa\u7cfb\u7edf\u91cd\u590d\u7f16\u8bd1\u540c\u4e00\u6e90\u6587\u4ef6&#xff08;\u5982 CMake \u4e2d\u91cd\u590d\u6dfb\u52a0&#xff09;\u3002<\/li>\n<\/ul>\n<h5>\u9519\u8bef\u793a\u4f8b<\/h5>\n<p><span class=\"token comment\">\/\/ main.cpp<\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&#034;utils.cpp&#034;<\/span>  <span class=\"token comment\">\/\/ \u9519\u8bef&#xff01;\u5305\u542b\u6e90\u6587\u4ef6<\/span><\/span><\/p>\n<h5>\u89e3\u51b3\u65b9\u6848<\/h5>\n<ul>\n<li>\u7981\u6b62\u7528 #include \u5305\u542b .cpp \u6587\u4ef6\u3002<\/li>\n<li>\u68c0\u67e5\u6784\u5efa\u914d\u7f6e&#xff0c;\u786e\u4fdd\u6e90\u6587\u4ef6\u53ea\u7f16\u8bd1\u4e00\u6b21&#xff1a;# CMakeLists.txt<br \/>\nadd_executable(app main.cpp utils.cpp)  # utils.cpp \u53ea\u6dfb\u52a0\u4e00\u6b21\n <\/li>\n<\/ul>\n<hr \/>\n<h4>2.4 \u5168\u5c40\u53d8\u91cf\u672a\u6b63\u786e\u4f7f\u7528 extern\/static<\/h4>\n<h5>\u95ee\u9898\u63cf\u8ff0<\/h5>\n<p>\u5168\u5c40\u53d8\u91cf\u5728\u5934\u6587\u4ef6\u4e2d\u76f4\u63a5\u5b9a\u4e49&#xff0c;\u5bfc\u81f4\u91cd\u590d\u5b9a\u4e49\u3002<\/p>\n<p><span class=\"token comment\">\/\/ config.h<\/span><br \/>\n<span class=\"token keyword\">int<\/span> g_value <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">42<\/span><span class=\"token punctuation\">;<\/span>  <span class=\"token comment\">\/\/ \u9519\u8bef&#xff01;\u5934\u6587\u4ef6\u4e2d\u76f4\u63a5\u5b9a\u4e49<\/span><\/p>\n<p><span class=\"token comment\">\/\/ file1.cpp<\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&#034;config.h&#034;<\/span>  <span class=\"token comment\">\/\/ \u5305\u542b\u540e\u751f\u6210 g_value \u5b9a\u4e49<\/span><\/span><\/p>\n<p><span class=\"token comment\">\/\/ file2.cpp<\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&#034;config.h&#034;<\/span>  <span class=\"token comment\">\/\/ \u518d\u6b21\u751f\u6210 g_value \u5b9a\u4e49<\/span><\/span><\/p>\n<h5>\u89e3\u51b3\u65b9\u6848<\/h5>\n<ul>\n<li>\u6b63\u786e\u4f7f\u7528 extern&#xff1a;<span class=\"token comment\">\/\/ config.h<\/span><br \/>\n<span class=\"token keyword\">extern<\/span> <span class=\"token keyword\">int<\/span> g_value<span class=\"token punctuation\">;<\/span>  <span class=\"token comment\">\/\/ \u58f0\u660e<\/span><\/p>\n<p><span class=\"token comment\">\/\/ config.cpp<\/span><br \/>\n<span class=\"token keyword\">int<\/span> g_value <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">42<\/span><span class=\"token punctuation\">;<\/span>    <span class=\"token comment\">\/\/ \u552f\u4e00\u5b9a\u4e49<\/span>\n <\/li>\n<li>\u4f7f\u7528 static&#xff08;\u6bcf\u4e2a\u6587\u4ef6\u72ec\u7acb\u526f\u672c&#xff09;&#xff1a;<span class=\"token comment\">\/\/ config.h<\/span><br \/>\n<span class=\"token keyword\">static<\/span> <span class=\"token keyword\">int<\/span> s_value <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">42<\/span><span class=\"token punctuation\">;<\/span>  <span class=\"token comment\">\/\/ \u6bcf\u4e2a\u5305\u542b\u8be5\u6587\u4ef6\u7684\u6e90\u6587\u4ef6\u6709\u72ec\u7acb\u526f\u672c<\/span>\n <\/li>\n<\/ul>\n<hr \/>\n<h4>2.5 \u7c7b\u9759\u6001\u6210\u5458\u53d8\u91cf\u672a\u6b63\u786e\u5b9a\u4e49<\/h4>\n<h5>\u95ee\u9898\u63cf\u8ff0<\/h5>\n<p>\u7c7b\u9759\u6001\u6210\u5458\u53d8\u91cf\u5728\u5934\u6587\u4ef6\u4e2d\u58f0\u660e&#xff0c;\u4f46\u5728\u591a\u4e2a\u6e90\u6587\u4ef6\u4e2d\u5b9a\u4e49\u3002<\/p>\n<p><span class=\"token comment\">\/\/ MyClass.h<\/span><br \/>\n<span class=\"token keyword\">class<\/span> <span class=\"token class-name\">MyClass<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token keyword\">public<\/span><span class=\"token operator\">:<\/span><br \/>\n    <span class=\"token keyword\">static<\/span> <span class=\"token keyword\">int<\/span> s_value<span class=\"token punctuation\">;<\/span>  <span class=\"token comment\">\/\/ \u58f0\u660e<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/ MyClass.cpp<\/span><br \/>\n<span class=\"token keyword\">int<\/span> MyClass<span class=\"token operator\">::<\/span>s_value <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">42<\/span><span class=\"token punctuation\">;<\/span>  <span class=\"token comment\">\/\/ \u6b63\u786e\u5b9a\u4e49<\/span><\/p>\n<p><span class=\"token comment\">\/\/ file2.cpp<\/span><br \/>\n<span class=\"token keyword\">int<\/span> MyClass<span class=\"token operator\">::<\/span>s_value <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">100<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/ \u9519\u8bef&#xff01;\u91cd\u590d\u5b9a\u4e49<\/span><\/p>\n<h5>\u89e3\u51b3\u65b9\u6848<\/h5>\n<ul>\n<li>\u786e\u4fdd\u9759\u6001\u6210\u5458\u53d8\u91cf\u53ea\u5728\u6e90\u6587\u4ef6\u4e2d\u5b9a\u4e49\u4e00\u6b21&#xff1a;<span class=\"token comment\">\/\/ MyClass.cpp<\/span><br \/>\n<span class=\"token keyword\">int<\/span> MyClass<span class=\"token operator\">::<\/span>s_value <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">50<\/span><span class=\"token punctuation\">;<\/span>  <span class=\"token comment\">\/\/ \u552f\u4e00\u5b9a\u4e49<\/span>\n <\/li>\n<\/ul>\n<hr \/>\n<h3>3. \u6a21\u677f\u4e0e\u5185\u8054\u51fd\u6570\u7684\u7279\u6b8a\u89c4\u5219<\/h3>\n<h4>3.1 \u6a21\u677f\u51fd\u6570\/\u7c7b<\/h4>\n<ul>\n<li>\u5fc5\u987b\u5c06\u5b9a\u4e49\u653e\u5728\u5934\u6587\u4ef6\u4e2d&#xff08;\u7f16\u8bd1\u5668\u9700\u770b\u5230\u5b8c\u6574\u5b9a\u4e49\u4ee5\u5b9e\u4f8b\u5316&#xff09;&#xff1a;<span class=\"token comment\">\/\/ mathutils.h<\/span><br \/>\n<span class=\"token keyword\">template<\/span> <span class=\"token operator\">&lt;<\/span><span class=\"token keyword\">typename<\/span> <span class=\"token class-name\">T<\/span><span class=\"token operator\">&gt;<\/span><br \/>\nT <span class=\"token function\">add<\/span><span class=\"token punctuation\">(<\/span>T a<span class=\"token punctuation\">,<\/span> T b<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span> <span class=\"token keyword\">return<\/span> a <span class=\"token operator\">&#043;<\/span> b<span class=\"token punctuation\">;<\/span> <span class=\"token punctuation\">}<\/span>\n <\/li>\n<\/ul>\n<h4>3.2 \u5185\u8054\u51fd\u6570<\/h4>\n<ul>\n<li>\n<p>\u9690\u5f0f\u5185\u8054&#xff1a;\u7c7b\u5185\u90e8\u76f4\u63a5\u5b9a\u4e49\u7684\u6210\u5458\u51fd\u6570&#xff08;\u5305\u62ec\u6784\u9020\u51fd\u6570&#xff09;\u81ea\u52a8\u5185\u8054\u3002<\/p>\n<p> <span class=\"token keyword\">class<\/span> <span class=\"token class-name\">Widget<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token keyword\">public<\/span><span class=\"token operator\">:<\/span><br \/>\n  <span class=\"token function\">Widget<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><span class=\"token punctuation\">}<\/span> <span class=\"token comment\">\/\/ \u9690\u5f0f\u5185\u8054<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span>\n <\/li>\n<li>\n<p>\u663e\u5f0f\u5185\u8054&#xff1a;\u7c7b\u5916\u90e8\u5b9a\u4e49\u4ecd\u9700 inline&#xff1a;<\/p>\n<p> <span class=\"token comment\">\/\/ Widget.h<\/span><br \/>\n<span class=\"token keyword\">class<\/span> <span class=\"token class-name\">Widget<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token keyword\">public<\/span><span class=\"token operator\">:<\/span><br \/>\n  <span class=\"token function\">Widget<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token keyword\">inline<\/span> <span class=\"token class-name\">Widget<\/span><span class=\"token operator\">::<\/span><span class=\"token function\">Widget<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><span class=\"token punctuation\">}<\/span> <span class=\"token comment\">\/\/ \u5fc5\u987b\u663e\u5f0f inline<\/span>\n <\/li>\n<\/ul>\n<h4>3.3 \u5173\u4e8e\u6a21\u677f<\/h4>\n<p>\u6a21\u677f\u7684\u4ee3\u7801\u9700\u8981\u653e\u5728\u5934\u6587\u4ef6\u4e2d&#xff0c;\u56e0\u4e3a\u6a21\u677f\u7684\u5b9e\u4f8b\u5316\u53d1\u751f\u5728\u7f16\u8bd1\u9636\u6bb5\u3002\u5f53\u7f16\u8bd1\u5668\u770b\u5230\u6a21\u677f\u88ab\u4f7f\u7528\u65f6&#xff08;\u4f8b\u5982\u8c03\u7528\u4e00\u4e2a\u6a21\u677f\u51fd\u6570\u6216\u5b9e\u4f8b\u5316\u4e00\u4e2a\u6a21\u677f\u7c7b&#xff09;&#xff0c;\u5b83\u9700\u8981\u6839\u636e\u6a21\u677f\u751f\u6210\u5177\u4f53\u7684\u4ee3\u7801&#xff08;\u79f0\u4e3a\u201c\u5b9e\u4f8b\u5316\u201d&#xff09;&#xff0c;\u5982\u679c\u6a21\u677f\u7684\u4ee3\u7801\u4e0d\u653e\u5728\u5934\u6587\u4ef6\u4e2d&#xff0c;\u5176\u4ed6\u6e90\u6587\u4ef6&#xff08;.cpp&#xff09;\u5c06\u65e0\u6cd5\u770b\u5230\u5b8c\u6574\u7684\u6a21\u677f\u5b9a\u4e49\u3002<\/p>\n<hr \/>\n<h3>4. \u6392\u67e5\u6b65\u9aa4<\/h3>\n<li>\u5b9a\u4f4d\u91cd\u590d\u7b26\u53f7&#xff1a;\u6839\u636e\u9519\u8bef\u4fe1\u606f\u4e2d\u7684\u51fd\u6570\/\u53d8\u91cf\u540d\u3002<\/li>\n<li>\u68c0\u67e5\u5934\u6587\u4ef6&#xff1a;\u786e\u8ba4\u662f\u5426\u76f4\u63a5\u5b9a\u4e49\u4e86\u975e\u5185\u8054\u51fd\u6570\u6216\u5168\u5c40\u53d8\u91cf\u3002<\/li>\n<li>\u68c0\u67e5\u6e90\u6587\u4ef6&#xff1a;\n<ul>\n<li>\u662f\u5426\u591a\u4e2a .cpp \u6587\u4ef6\u5b9a\u4e49\u4e86\u540c\u540d\u5168\u5c40\u51fd\u6570\/\u53d8\u91cf\u3002<\/li>\n<li>\u662f\u5426\u8bef\u7528 #include &#034;xxx.cpp&#034;\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u68c0\u67e5\u6784\u5efa\u7cfb\u7edf&#xff1a;\u786e\u4fdd\u672a\u91cd\u590d\u6dfb\u52a0\u6e90\u6587\u4ef6\u3002<\/li>\n<li>\u68c0\u67e5\u5168\u5c40\u53d8\u91cf\/\u9759\u6001\u6210\u5458&#xff1a;\u662f\u5426\u6b63\u786e\u5b9a\u4e49\u3002<\/li>\n<hr \/>\n<h3>\u603b\u7ed3<\/h3>\n<table>\n<tr>\u573a\u666f\u6b63\u786e\u505a\u6cd5\u9519\u8bef\u505a\u6cd5<\/tr>\n<tbody>\n<tr>\n<td>\u5934\u6587\u4ef6\u4e2d\u7684\u51fd\u6570<\/td>\n<td>\u58f0\u660e\u5728 .h&#xff0c;\u5b9a\u4e49\u5728 .cpp \u6216\u52a0 inline<\/td>\n<td>\u76f4\u63a5\u5b9a\u4e49\u975e\u5185\u8054\u51fd\u6570<\/td>\n<\/tr>\n<tr>\n<td>\u5168\u5c40\u53d8\u91cf<\/td>\n<td>\u5934\u6587\u4ef6\u7528 extern&#xff0c;\u6e90\u6587\u4ef6\u5b9a\u4e49<\/td>\n<td>\u5934\u6587\u4ef6\u4e2d\u76f4\u63a5\u5b9a\u4e49<\/td>\n<\/tr>\n<tr>\n<td>\u7c7b\u9759\u6001\u6210\u5458\u53d8\u91cf<\/td>\n<td>\u7c7b\u5185\u58f0\u660e&#xff0c;\u5355\u4e2a\u6e90\u6587\u4ef6\u5b9a\u4e49<\/td>\n<td>\u591a\u4e2a\u6e90\u6587\u4ef6\u91cd\u590d\u5b9a\u4e49<\/td>\n<\/tr>\n<tr>\n<td>\u6a21\u677f\u51fd\u6570<\/td>\n<td>\u5b9a\u4e49\u5728 .h<\/td>\n<td>\u5206\u79bb\u58f0\u660e\/\u5b9a\u4e49\u4e14\u4e0d\u5b9e\u4f8b\u5316<\/td>\n<\/tr>\n<tr>\n<td>\u6784\u9020\u51fd\u6570<\/td>\n<td>\u7c7b\u5185\u90e8\u5b9a\u4e49&#xff08;\u9690\u5f0f\u5185\u8054&#xff09;<\/td>\n<td>\u7c7b\u5916\u90e8\u5b9a\u4e49\u672a\u52a0 inline<\/td>\n<\/tr>\n<tr>\n<td>\u91cd\u590d\u7f16\u8bd1\u95ee\u9898<\/td>\n<td>\u786e\u4fdd\u6bcf\u4e2a .cpp \u53ea\u7f16\u8bd1\u4e00\u6b21<\/td>\n<td>\u9519\u8bef\u5305\u542b .cpp \u6216\u91cd\u590d\u6dfb\u52a0<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1k\u6b21\uff0c\u70b9\u8d5e14\u6b21\uff0c\u6536\u85cf25\u6b21\u3002\u5728\u5934\u6587\u4ef6\uff08.h\uff09\u4e2d\u76f4\u63a5\u5b9a\u4e49\u975e\u5185\u8054\u51fd\u6570\u6216\u5168\u5c40\u53d8\u91cf\uff0c\u4e14\u8be5\u5934\u6587\u4ef6\u88ab\u591a\u4e2a\u6e90\u6587\u4ef6\u5305\u542b\u3002\/\/ utils.hvoid badFunc() { \/* \u5b9e\u73b0 *\/ } \/\/ \u9519\u8bef\uff01\u975e\u5185\u8054\u51fd\u6570\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u4e2d\/\/ \u9519\u8bef\uff01\u5168\u5c40\u53d8\u91cf\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u4e2d\u591a\u4e2a.cpp\u6587\u4ef6\u72ec\u7acb\u5b9e\u73b0\u4e86\u540c\u540d\u7684\u5168\u5c40\u51fd\u6570\u6216\u53d8\u91cf\u3002void myFunc() { \/* \u5b9e\u73b01 *\/ }void myFunc() { \/* \u5b9e\u73b02 *\/ } \/\/ \u9519\u8bef\uff01\u91cd\u590d\u5b9a\u4e49\u7c7b\u9759\u6001\u6210\u5458\u53d8\u91cf\u5728\u5934\u6587\u4ef6\u4e2d\u58f0\u660e\uff0c\u4f46\u5728\u591a\u4e2a\u6e90\u6587\u4ef6\u4e2d\u5b9a\u4e49\u3002public:\/\/ \u58f0\u660e\/\/ \u6b63\u786e\u5b9a\u4e49\u3002_c++\u91cd\u590d\u5b9a\u4e49<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[55,190],"topic":[],"class_list":["post-40886","post","type-post","status-publish","format-standard","hentry","category-server","tag-c","tag-190"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>C++ \u94fe\u63a5\u9519\u8bef\uff1a\u591a\u91cd\u5b9a\u4e49\u95ee\u9898 - \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\/40886.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C++ \u94fe\u63a5\u9519\u8bef\uff1a\u591a\u91cd\u5b9a\u4e49\u95ee\u9898 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1k\u6b21\uff0c\u70b9\u8d5e14\u6b21\uff0c\u6536\u85cf25\u6b21\u3002\u5728\u5934\u6587\u4ef6\uff08.h\uff09\u4e2d\u76f4\u63a5\u5b9a\u4e49\u975e\u5185\u8054\u51fd\u6570\u6216\u5168\u5c40\u53d8\u91cf\uff0c\u4e14\u8be5\u5934\u6587\u4ef6\u88ab\u591a\u4e2a\u6e90\u6587\u4ef6\u5305\u542b\u3002\/\/ utils.hvoid badFunc() { \/* \u5b9e\u73b0 *\/ } \/\/ \u9519\u8bef\uff01\u975e\u5185\u8054\u51fd\u6570\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u4e2d\/\/ \u9519\u8bef\uff01\u5168\u5c40\u53d8\u91cf\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u4e2d\u591a\u4e2a.cpp\u6587\u4ef6\u72ec\u7acb\u5b9e\u73b0\u4e86\u540c\u540d\u7684\u5168\u5c40\u51fd\u6570\u6216\u53d8\u91cf\u3002void myFunc() { \/* \u5b9e\u73b01 *\/ }void myFunc() { \/* \u5b9e\u73b02 *\/ } \/\/ \u9519\u8bef\uff01\u91cd\u590d\u5b9a\u4e49\u7c7b\u9759\u6001\u6210\u5458\u53d8\u91cf\u5728\u5934\u6587\u4ef6\u4e2d\u58f0\u660e\uff0c\u4f46\u5728\u591a\u4e2a\u6e90\u6587\u4ef6\u4e2d\u5b9a\u4e49\u3002public:\/\/ \u58f0\u660e\/\/ \u6b63\u786e\u5b9a\u4e49\u3002_c++\u91cd\u590d\u5b9a\u4e49\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/40886.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-05-30T04:26:26+00:00\" \/>\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=\"1 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/40886.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/40886.html\",\"name\":\"C++ \u94fe\u63a5\u9519\u8bef\uff1a\u591a\u91cd\u5b9a\u4e49\u95ee\u9898 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-05-30T04:26:26+00:00\",\"dateModified\":\"2025-05-30T04:26:26+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/40886.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/40886.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/40886.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C++ \u94fe\u63a5\u9519\u8bef\uff1a\u591a\u91cd\u5b9a\u4e49\u95ee\u9898\"}]},{\"@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":"C++ \u94fe\u63a5\u9519\u8bef\uff1a\u591a\u91cd\u5b9a\u4e49\u95ee\u9898 - \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\/40886.html","og_locale":"zh_CN","og_type":"article","og_title":"C++ \u94fe\u63a5\u9519\u8bef\uff1a\u591a\u91cd\u5b9a\u4e49\u95ee\u9898 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1k\u6b21\uff0c\u70b9\u8d5e14\u6b21\uff0c\u6536\u85cf25\u6b21\u3002\u5728\u5934\u6587\u4ef6\uff08.h\uff09\u4e2d\u76f4\u63a5\u5b9a\u4e49\u975e\u5185\u8054\u51fd\u6570\u6216\u5168\u5c40\u53d8\u91cf\uff0c\u4e14\u8be5\u5934\u6587\u4ef6\u88ab\u591a\u4e2a\u6e90\u6587\u4ef6\u5305\u542b\u3002\/\/ utils.hvoid badFunc() { \/* \u5b9e\u73b0 *\/ } \/\/ \u9519\u8bef\uff01\u975e\u5185\u8054\u51fd\u6570\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u4e2d\/\/ \u9519\u8bef\uff01\u5168\u5c40\u53d8\u91cf\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u4e2d\u591a\u4e2a.cpp\u6587\u4ef6\u72ec\u7acb\u5b9e\u73b0\u4e86\u540c\u540d\u7684\u5168\u5c40\u51fd\u6570\u6216\u53d8\u91cf\u3002void myFunc() { \/* \u5b9e\u73b01 *\/ }void myFunc() { \/* \u5b9e\u73b02 *\/ } \/\/ \u9519\u8bef\uff01\u91cd\u590d\u5b9a\u4e49\u7c7b\u9759\u6001\u6210\u5458\u53d8\u91cf\u5728\u5934\u6587\u4ef6\u4e2d\u58f0\u660e\uff0c\u4f46\u5728\u591a\u4e2a\u6e90\u6587\u4ef6\u4e2d\u5b9a\u4e49\u3002public:\/\/ \u58f0\u660e\/\/ \u6b63\u786e\u5b9a\u4e49\u3002_c++\u91cd\u590d\u5b9a\u4e49","og_url":"https:\/\/www.wsisp.com\/helps\/40886.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-05-30T04:26:26+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"1 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/40886.html","url":"https:\/\/www.wsisp.com\/helps\/40886.html","name":"C++ \u94fe\u63a5\u9519\u8bef\uff1a\u591a\u91cd\u5b9a\u4e49\u95ee\u9898 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-05-30T04:26:26+00:00","dateModified":"2025-05-30T04:26:26+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/40886.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/40886.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/40886.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"C++ \u94fe\u63a5\u9519\u8bef\uff1a\u591a\u91cd\u5b9a\u4e49\u95ee\u9898"}]},{"@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\/40886","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=40886"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/40886\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=40886"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=40886"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=40886"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=40886"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}