{"id":87,"date":"2025-03-07T14:34:16","date_gmt":"2025-03-07T06:34:16","guid":{"rendered":"https:\/\/cnsn.rr.nu\/?p=87"},"modified":"2025-03-07T14:34:16","modified_gmt":"2025-03-07T06:34:16","slug":"c%e5%87%bd%e6%95%b0%e8%ae%a9c%e4%bd%bf%e7%94%a8%ef%bc%8c%e6%9c%89%e5%87%a0%e7%a7%8d%e5%8a%9e%e6%b3%95%ef%bc%8c%e4%bc%98%e5%8a%a3%e7%82%b9%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/blog.cnsn.fun\/?p=87","title":{"rendered":"c++\u51fd\u6570\u8ba9c\u4f7f\u7528\uff0c\u6709\u51e0\u79cd\u529e\u6cd5\uff0c\u4f18\u52a3\u70b9\uff1f"},"content":{"rendered":"<p>\u5728C++\u4e2d\uff0c\u5982\u679c\u4f60\u5e0c\u671b\u8ba9C\u8bed\u8a00\u4ee3\u7801\u80fd\u591f\u8c03\u7528C++\u51fd\u6570\uff0c\u901a\u5e38\u6709\u51e0\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\uff0c\u5177\u4f53\u9009\u62e9\u53d6\u51b3\u4e8e\u4f60\u7684\u9700\u6c42\u548c\u9879\u76ee\u7ea6\u675f\u3002<\/p>\n<hr \/>\n<h3>\u65b9\u6cd51\uff1a\u4f7f\u7528 <code>extern &quot;C&quot;<\/code> \u94fe\u63a5\u58f0\u660e<\/h3>\n<p>\u8fd9\u662f\u6700\u5e38\u89c1\u7684\u65b9\u5f0f\uff0c\u901a\u8fc7\u5728C++\u51fd\u6570\u58f0\u660e\u524d\u52a0\u4e0a <code>extern &quot;C&quot;<\/code>\uff0c\u544a\u8bc9\u7f16\u8bd1\u5668\u4f7f\u7528C\u8bed\u8a00\u7684\u94fe\u63a5\u89c4\u5219\uff08\u5373\u4e0d\u8fdb\u884c\u540d\u79f0\u4fee\u9970\uff09\uff0c\u4ece\u800c\u8ba9C\u4ee3\u7801\u80fd\u591f\u8c03\u7528C++\u51fd\u6570\u3002<\/p>\n<h4>\u793a\u4f8b<\/h4>\n<pre><code class=\"language-cpp\">\/\/ C++\u4ee3\u7801 (example.cpp)\nextern &quot;C&quot; {\n    void my_function(int x) {\n        std::cout &lt;&lt; &quot;Value: &quot; &lt;&lt; x &lt;&lt; std::endl;\n    }\n}<\/code><\/pre>\n<pre><code class=\"language-c\">\/\/ C\u4ee3\u7801 (main.c)\nvoid my_function(int x); \/\/ \u58f0\u660eC++\u51fd\u6570\n\nint main() {\n    my_function(42); \/\/ \u8c03\u7528C++\u51fd\u6570\n    return 0;\n}<\/code><\/pre>\n<h4>\u4f18\u70b9<\/h4>\n<ol>\n<li><strong>\u7b80\u5355\u6613\u7528<\/strong>\uff1a\u53ea\u9700\u8981\u5728C++\u51fd\u6570\u58f0\u660e\u524d\u52a0\u4e0a <code>extern &quot;C&quot;<\/code> \u5373\u53ef\u3002<\/li>\n<li><strong>\u517c\u5bb9\u6027\u597d<\/strong>\uff1a\u9002\u7528\u4e8e\u5927\u591a\u6570\u573a\u666f\uff0c\u5c24\u5176\u662f\u9700\u8981\u66b4\u9732\u5c11\u91cf\u51fd\u6570\u7ed9C\u4ee3\u7801\u65f6\u3002<\/li>\n<\/ol>\n<h4>\u7f3a\u70b9<\/h4>\n<ol>\n<li><strong>\u4e0d\u652f\u6301C++\u7279\u6027<\/strong>\uff1a<code>extern &quot;C&quot;<\/code> \u51fd\u6570\u4e0d\u80fd\u4f7f\u7528C++\u7684\u7279\u6027\uff08\u5982\u91cd\u8f7d\u3001\u7c7b\u3001\u6a21\u677f\u7b49\uff09\u3002<\/li>\n<li><strong>\u540d\u79f0\u51b2\u7a81<\/strong>\uff1a\u5982\u679cC++\u4ee3\u7801\u4e2d\u6709\u91cd\u8f7d\u51fd\u6570\uff0c\u65e0\u6cd5\u901a\u8fc7 <code>extern &quot;C&quot;<\/code> \u66b4\u9732\u7ed9C\u4ee3\u7801\u3002<\/li>\n<\/ol>\n<hr \/>\n<h3>\u65b9\u6cd52\uff1a\u4f7f\u7528C++\u7c7b\u7684\u9759\u6001\u6210\u5458\u51fd\u6570<\/h3>\n<p>\u5982\u679c\u9700\u8981\u5728C++\u4e2d\u4f7f\u7528\u7c7b\uff0c\u53ef\u4ee5\u5c06\u7c7b\u7684\u9759\u6001\u6210\u5458\u51fd\u6570\u66b4\u9732\u7ed9C\u4ee3\u7801\u3002\u9759\u6001\u6210\u5458\u51fd\u6570\u6ca1\u6709 <code>this<\/code> \u6307\u9488\uff0c\u56e0\u6b64\u53ef\u4ee5\u50cf\u666e\u901aC\u51fd\u6570\u4e00\u6837\u8c03\u7528\u3002<\/p>\n<h4>\u793a\u4f8b<\/h4>\n<pre><code class=\"language-cpp\">\/\/ C++\u4ee3\u7801 (example.cpp)\nclass MyClass {\npublic:\n    static void my_function(int x) {\n        std::cout &lt;&lt; &quot;Value: &quot; &lt;&lt; x &lt;&lt; std::endl;\n    }\n};\n\nextern &quot;C&quot; {\n    void call_my_function(int x) {\n        MyClass::my_function(x);\n    }\n}<\/code><\/pre>\n<pre><code class=\"language-c\">\/\/ C\u4ee3\u7801 (main.c)\nvoid call_my_function(int x); \/\/ \u58f0\u660eC++\u51fd\u6570\n\nint main() {\n    call_my_function(42); \/\/ \u8c03\u7528C++\u9759\u6001\u6210\u5458\u51fd\u6570\n    return 0;\n}<\/code><\/pre>\n<h4>\u4f18\u70b9<\/h4>\n<ol>\n<li><strong>\u652f\u6301C++\u7c7b<\/strong>\uff1a\u53ef\u4ee5\u901a\u8fc7\u9759\u6001\u6210\u5458\u51fd\u6570\u95f4\u63a5\u8bbf\u95eeC++\u7c7b\u7684\u529f\u80fd\u3002<\/li>\n<li><strong>\u7075\u6d3b\u6027<\/strong>\uff1a\u53ef\u4ee5\u5728C++\u4ee3\u7801\u4e2d\u5c01\u88c5\u66f4\u590d\u6742\u7684\u903b\u8f91\u3002<\/li>\n<\/ol>\n<h4>\u7f3a\u70b9<\/h4>\n<ol>\n<li><strong>\u95f4\u63a5\u8c03\u7528<\/strong>\uff1a\u9700\u8981\u901a\u8fc7\u989d\u5916\u7684\u5305\u88c5\u51fd\u6570\u8c03\u7528C++\u7c7b\u7684\u529f\u80fd\u3002<\/li>\n<li><strong>\u590d\u6742\u6027\u589e\u52a0<\/strong>\uff1a\u5982\u679c\u7c7b\u529f\u80fd\u590d\u6742\uff0c\u53ef\u80fd\u9700\u8981\u7f16\u5199\u591a\u4e2a\u5305\u88c5\u51fd\u6570\u3002<\/li>\n<\/ol>\n<hr \/>\n<h3>\u65b9\u6cd53\uff1a\u4f7f\u7528C\u63a5\u53e3\u5c01\u88c5C++\u5bf9\u8c61<\/h3>\n<p>\u5982\u679c\u9700\u8981\u5728C\u4ee3\u7801\u4e2d\u4f7f\u7528C++\u5bf9\u8c61\uff0c\u53ef\u4ee5\u901a\u8fc7C\u63a5\u53e3\u5c01\u88c5C++\u5bf9\u8c61\u3002\u5177\u4f53\u505a\u6cd5\u662f\uff1a<\/p>\n<ol>\n<li>\u5728C++\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u7c7b\u3002<\/li>\n<li>\u63d0\u4f9b\u4e00\u7ec4C\u98ce\u683c\u7684\u51fd\u6570\u6765\u521b\u5efa\u3001\u64cd\u4f5c\u548c\u9500\u6bc1\u5bf9\u8c61\u3002<\/li>\n<li>\u5728C\u4ee3\u7801\u4e2d\u901a\u8fc7\u6307\u9488\u64cd\u4f5cC++\u5bf9\u8c61\u3002<\/li>\n<\/ol>\n<h4>\u793a\u4f8b<\/h4>\n<pre><code class=\"language-cpp\">\/\/ C++\u4ee3\u7801 (example.cpp)\nclass MyClass {\npublic:\n    void do_something(int x) {\n        std::cout &lt;&lt; &quot;Value: &quot; &lt;&lt; x &lt;&lt; std::endl;\n    }\n};\n\nextern &quot;C&quot; {\n    MyClass* create_my_class() {\n        return new MyClass();\n    }\n    void call_do_something(MyClass* obj, int x) {\n        obj-&gt;do_something(x);\n    }\n    void destroy_my_class(MyClass* obj) {\n        delete obj;\n    }\n}<\/code><\/pre>\n<pre><code class=\"language-c\">\/\/ C\u4ee3\u7801 (main.c)\ntypedef void* MyClassHandle; \/\/ \u5b9a\u4e49\u53e5\u67c4\u7c7b\u578b\n\nMyClassHandle create_my_class();\nvoid call_do_something(MyClassHandle obj, int x);\nvoid destroy_my_class(MyClassHandle obj);\n\nint main() {\n    MyClassHandle obj = create_my_class(); \/\/ \u521b\u5efaC++\u5bf9\u8c61\n    call_do_something(obj, 42); \/\/ \u8c03\u7528C++\u5bf9\u8c61\u7684\u65b9\u6cd5\n    destroy_my_class(obj); \/\/ \u9500\u6bc1C++\u5bf9\u8c61\n    return 0;\n}<\/code><\/pre>\n<h4>\u4f18\u70b9<\/h4>\n<ol>\n<li><strong>\u652f\u6301C++\u5bf9\u8c61<\/strong>\uff1a\u53ef\u4ee5\u5728C\u4ee3\u7801\u4e2d\u64cd\u4f5cC++\u5bf9\u8c61\u3002<\/li>\n<li><strong>\u5c01\u88c5\u6027\u597d<\/strong>\uff1aC++\u5bf9\u8c61\u7684\u5b9e\u73b0\u7ec6\u8282\u5bf9C\u4ee3\u7801\u5b8c\u5168\u9690\u85cf\u3002<\/li>\n<\/ol>\n<h4>\u7f3a\u70b9<\/h4>\n<ol>\n<li><strong>\u590d\u6742\u6027\u9ad8<\/strong>\uff1a\u9700\u8981\u7f16\u5199\u989d\u5916\u7684C\u63a5\u53e3\u6765\u5c01\u88c5C++\u5bf9\u8c61\u3002<\/li>\n<li><strong>\u5185\u5b58\u7ba1\u7406\u590d\u6742<\/strong>\uff1a\u9700\u8981\u624b\u52a8\u7ba1\u7406C++\u5bf9\u8c61\u7684\u5185\u5b58\uff08\u5982\u4f7f\u7528 <code>new<\/code> \u548c <code>delete<\/code>\uff09\u3002<\/li>\n<\/ol>\n<hr \/>\n<h3>\u65b9\u6cd54\uff1a\u4f7f\u7528\u56de\u8c03\u51fd\u6570<\/h3>\n<p>\u5982\u679cC++\u4ee3\u7801\u9700\u8981\u8c03\u7528C\u4ee3\u7801\uff0c\u53ef\u4ee5\u901a\u8fc7\u56de\u8c03\u51fd\u6570\u7684\u65b9\u5f0f\u5b9e\u73b0\u3002C\u4ee3\u7801\u63d0\u4f9b\u51fd\u6570\u6307\u9488\uff0cC++\u4ee3\u7801\u8c03\u7528\u8fd9\u4e9b\u51fd\u6570\u6307\u9488\u3002<\/p>\n<h4>\u793a\u4f8b<\/h4>\n<pre><code class=\"language-cpp\">\/\/ C++\u4ee3\u7801 (example.cpp)\nextern &quot;C&quot; {\n    typedef void (*Callback)(int); \/\/ \u5b9a\u4e49\u56de\u8c03\u51fd\u6570\u7c7b\u578b\n    void cpp_function(Callback callback, int x) {\n        callback(x); \/\/ \u8c03\u7528C\u4ee3\u7801\u63d0\u4f9b\u7684\u56de\u8c03\u51fd\u6570\n    }\n}<\/code><\/pre>\n<pre><code class=\"language-c\">\/\/ C\u4ee3\u7801 (main.c)\n#include &lt;stdio.h&gt;\n\nvoid cpp_function(void (*callback)(int), int x);\n\nvoid my_callback(int x) {\n    printf(&quot;Value: %d\\n&quot;, x);\n}\n\nint main() {\n    cpp_function(my_callback, 42); \/\/ \u8c03\u7528C++\u51fd\u6570\u5e76\u4f20\u9012\u56de\u8c03\u51fd\u6570\n    return 0;\n}<\/code><\/pre>\n<h4>\u4f18\u70b9<\/h4>\n<ol>\n<li><strong>\u7075\u6d3b\u6027\u9ad8<\/strong>\uff1a\u9002\u5408C++\u4ee3\u7801\u9700\u8981\u8c03\u7528C\u4ee3\u7801\u7684\u573a\u666f\u3002<\/li>\n<li><strong>\u89e3\u8026<\/strong>\uff1aC++\u4ee3\u7801\u548cC\u4ee3\u7801\u901a\u8fc7\u56de\u8c03\u51fd\u6570\u89e3\u8026\u3002<\/li>\n<\/ol>\n<h4>\u7f3a\u70b9<\/h4>\n<ol>\n<li><strong>\u590d\u6742\u6027\u589e\u52a0<\/strong>\uff1a\u9700\u8981\u5b9a\u4e49\u56de\u8c03\u51fd\u6570\u7c7b\u578b\u5e76\u7ba1\u7406\u51fd\u6570\u6307\u9488\u3002<\/li>\n<li><strong>\u8c03\u8bd5\u56f0\u96be<\/strong>\uff1a\u56de\u8c03\u51fd\u6570\u7684\u8c03\u7528\u94fe\u53ef\u80fd\u96be\u4ee5\u8c03\u8bd5\u3002<\/li>\n<\/ol>\n<hr \/>\n<h3>\u603b\u7ed3<\/h3>\n<table>\n<thead>\n<tr>\n<th>\u65b9\u6cd5<\/th>\n<th>\u4f18\u70b9<\/th>\n<th>\u7f3a\u70b9<\/th>\n<th>\u9002\u7528\u573a\u666f<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>extern &quot;C&quot;<\/code><\/td>\n<td>\u7b80\u5355\u6613\u7528\uff0c\u517c\u5bb9\u6027\u597d<\/td>\n<td>\u4e0d\u652f\u6301C++\u7279\u6027\uff0c\u540d\u79f0\u51b2\u7a81<\/td>\n<td>\u66b4\u9732\u5c11\u91cf\u7b80\u5355\u51fd\u6570\u7ed9C\u4ee3\u7801<\/td>\n<\/tr>\n<tr>\n<td>\u9759\u6001\u6210\u5458\u51fd\u6570<\/td>\n<td>\u652f\u6301C++\u7c7b\uff0c\u7075\u6d3b\u6027\u9ad8<\/td>\n<td>\u9700\u8981\u989d\u5916\u5305\u88c5\u51fd\u6570<\/td>\n<td>\u9700\u8981\u66b4\u9732\u7c7b\u7684\u9759\u6001\u529f\u80fd\u7ed9C\u4ee3\u7801<\/td>\n<\/tr>\n<tr>\n<td>C\u63a5\u53e3\u5c01\u88c5C++\u5bf9\u8c61<\/td>\n<td>\u652f\u6301C++\u5bf9\u8c61\uff0c\u5c01\u88c5\u6027\u597d<\/td>\n<td>\u590d\u6742\u6027\u9ad8\uff0c\u5185\u5b58\u7ba1\u7406\u590d\u6742<\/td>\n<td>\u9700\u8981\u5728C\u4ee3\u7801\u4e2d\u64cd\u4f5cC++\u5bf9\u8c61<\/td>\n<\/tr>\n<tr>\n<td>\u56de\u8c03\u51fd\u6570<\/td>\n<td>\u7075\u6d3b\u6027\u9ad8\uff0c\u89e3\u8026<\/td>\n<td>\u590d\u6742\u6027\u589e\u52a0\uff0c\u8c03\u8bd5\u56f0\u96be<\/td>\n<td>C++\u4ee3\u7801\u9700\u8981\u8c03\u7528C\u4ee3\u7801<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u6839\u636e\u4f60\u7684\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u5982\u679c\u53ea\u662f\u66b4\u9732\u7b80\u5355\u51fd\u6570\uff0c<code>extern &quot;C&quot;<\/code> \u662f\u6700\u7b80\u5355\u7684\u9009\u62e9\uff1b\u5982\u679c\u9700\u8981\u64cd\u4f5cC++\u5bf9\u8c61\uff0c\u5219\u63a8\u8350\u4f7f\u7528C\u63a5\u53e3\u5c01\u88c5\u7684\u65b9\u5f0f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728C++\u4e2d\uff0c\u5982\u679c\u4f60\u5e0c\u671b\u8ba9C\u8bed\u8a00\u4ee3\u7801\u80fd\u591f\u8c03\u7528C++\u51fd\u6570\uff0c\u901a\u5e38\u6709\u51e0\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\uff0c\u5177\u4f53\u9009\u62e9\u53d6\u51b3 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,8],"tags":[],"class_list":["post-87","post","type-post","status-publish","format-standard","hentry","category-c"],"_links":{"self":[{"href":"https:\/\/blog.cnsn.fun\/index.php?rest_route=\/wp\/v2\/posts\/87","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.cnsn.fun\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.cnsn.fun\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.cnsn.fun\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.cnsn.fun\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=87"}],"version-history":[{"count":0,"href":"https:\/\/blog.cnsn.fun\/index.php?rest_route=\/wp\/v2\/posts\/87\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.cnsn.fun\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=87"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.cnsn.fun\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=87"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.cnsn.fun\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=87"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}