{"id":1171889,"date":"2025-01-15T16:41:19","date_gmt":"2025-01-15T08:41:19","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1171889.html"},"modified":"2025-01-15T16:41:22","modified_gmt":"2025-01-15T08:41:22","slug":"python%e5%a6%82%e4%bd%95%e8%b0%83%e7%94%a8c-%e7%9a%84%e7%a8%8b%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1171889.html","title":{"rendered":"python\u5982\u4f55\u8c03\u7528c \u7684\u7a0b\u5e8f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26074122\/d3efbd91-5511-4404-8a1b-bc23e99cdeca.webp\" alt=\"python\u5982\u4f55\u8c03\u7528c \u7684\u7a0b\u5e8f\" \/><\/p>\n<p><p> <strong>Python\u8c03\u7528C\u7a0b\u5e8f\u7684\u65b9\u5f0f\u6709\u5f88\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528ctypes\u3001cffi\u3001SWIG\u3001\u4ee5\u53ca\u7f16\u5199Python\u6269\u5c55\u6a21\u5757\u7b49\u3002<\/strong> \u5728\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\uff0cctypes\u548ccffi\u662f\u8f83\u4e3a\u5e38\u7528\u548c\u7075\u6d3b\u7684\u65b9\u5f0f\uff0c\u800cSWIG\u548c\u6269\u5c55\u6a21\u5757\u5219\u9002\u5408\u4e8e\u9700\u8981\u66f4\u9ad8\u6027\u80fd\u548c\u66f4\u590d\u6742\u529f\u80fd\u7684\u573a\u666f\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u5f0f\uff0c\u5e76\u7ed9\u51fa\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528ctypes\u8c03\u7528C\u7a0b\u5e8f<\/h3>\n<\/p>\n<p><p>ctypes\u662fPython\u5185\u7f6e\u7684\u4e00\u4e2a\u5e93\uff0c\u5141\u8bb8\u4f60\u8c03\u7528\u52a8\u6001\u94fe\u63a5\u5e93\u4e2d\u7684C\u51fd\u6570\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528ctypes\u8c03\u7528C\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h4>1. \u7f16\u5199C\u4ee3\u7801\u5e76\u751f\u6210\u5171\u4eab\u5e93<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684C\u51fd\u6570\uff0c\u5e76\u7f16\u8bd1\u751f\u6210\u5171\u4eab\u5e93\u3002\u4f8b\u5982\uff0c\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684C\u7a0b\u5e8f<code>example.c<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-c\">\/\/ example.c<\/p>\n<p>#include &lt;stdio.h&gt;<\/p>\n<p>void hello() {<\/p>\n<p>    printf(&quot;Hello from C!\\n&quot;);<\/p>\n<p>}<\/p>\n<p>int add(int a, int b) {<\/p>\n<p>    return a + b;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u7f16\u8bd1\u8fd9\u4e2aC\u4ee3\u7801\u751f\u6210\u5171\u4eab\u5e93\uff08\u5728Linux\u4e0a\uff09\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">gcc -shared -o libexample.so -fPIC example.c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528ctypes\u52a0\u8f7d\u5171\u4eab\u5e93\u5e76\u8c03\u7528C\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u5728Python\u4e2d\u4f7f\u7528ctypes\u52a0\u8f7d\u8fd9\u4e2a\u5171\u4eab\u5e93\uff0c\u5e76\u8c03\u7528\u5176\u4e2d\u7684\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import ctypes<\/p>\n<h2><strong>\u52a0\u8f7d\u5171\u4eab\u5e93<\/strong><\/h2>\n<p>example = ctypes.CDLL(&#39;.\/libexample.so&#39;)<\/p>\n<h2><strong>\u8c03\u7528hello\u51fd\u6570<\/strong><\/h2>\n<p>example.hello()<\/p>\n<h2><strong>\u8c03\u7528add\u51fd\u6570<\/strong><\/h2>\n<p>result = example.add(2, 3)<\/p>\n<p>print(f&quot;Result of add: {result}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4f8b\u5b50\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528ctypes\u52a0\u8f7d\u5171\u4eab\u5e93\u5e76\u8c03\u7528C\u51fd\u6570\u3002ctypes\u5e93\u975e\u5e38\u7075\u6d3b\uff0c\u53ef\u4ee5\u5904\u7406\u5404\u79cdC\u6570\u636e\u7c7b\u578b\u548c\u7ed3\u6784\u4f53\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528cffi\u8c03\u7528C\u7a0b\u5e8f<\/h3>\n<\/p>\n<p><p>cffi\u662f\u53e6\u4e00\u4e2a\u5f3a\u5927\u7684\u5e93\uff0c\u5141\u8bb8\u4f60\u8c03\u7528C\u51fd\u6570\uff0c\u5e76\u4e14\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u5904\u7406\u590d\u6742\u7684\u6570\u636e\u7c7b\u578b\u548c\u7ed3\u6784\u4f53\u3002<\/p>\n<\/p>\n<p><h4>1. \u7f16\u5199C\u4ee3\u7801\u5e76\u751f\u6210\u5171\u4eab\u5e93<\/h4>\n<\/p>\n<p><p>\u4e0ectypes\u76f8\u540c\uff0c\u9996\u5148\u7f16\u5199C\u4ee3\u7801\u5e76\u751f\u6210\u5171\u4eab\u5e93\uff08\u8fd9\u91cc\u5047\u8bbe\u5df2\u7ecf\u6709\u4e00\u4e2a<code>example.c<\/code>\u5e76\u751f\u6210\u4e86<code>libexample.so<\/code>\uff09\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528cffi\u52a0\u8f7d\u5171\u4eab\u5e93\u5e76\u8c03\u7528C\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\u4f7f\u7528cffi\u8c03\u7528C\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from cffi import FFI<\/p>\n<p>ffi = FFI()<\/p>\n<h2><strong>\u5b9a\u4e49C\u51fd\u6570\u7684\u63a5\u53e3<\/strong><\/h2>\n<p>ffi.cdef(&quot;&quot;&quot;<\/p>\n<p>    void hello();<\/p>\n<p>    int add(int a, int b);<\/p>\n<p>&quot;&quot;&quot;)<\/p>\n<h2><strong>\u52a0\u8f7d\u5171\u4eab\u5e93<\/strong><\/h2>\n<p>example = ffi.dlopen(&#39;.\/libexample.so&#39;)<\/p>\n<h2><strong>\u8c03\u7528hello\u51fd\u6570<\/strong><\/h2>\n<p>example.hello()<\/p>\n<h2><strong>\u8c03\u7528add\u51fd\u6570<\/strong><\/h2>\n<p>result = example.add(2, 3)<\/p>\n<p>print(f&quot;Result of add: {result}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>cffi\u76f8\u6bd4\u4e8ectypes\uff0c\u66f4\u52a0\u7075\u6d3b\uff0c\u80fd\u591f\u5904\u7406\u66f4\u590d\u6742\u7684C\u6570\u636e\u7ed3\u6784\uff0c\u5e76\u4e14\u63d0\u4f9b\u4e86\u66f4\u597d\u7684\u9519\u8bef\u68c0\u67e5\u548c\u8c03\u8bd5\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528SWIG\u751f\u6210Python\u7ed1\u5b9a<\/h3>\n<\/p>\n<p><p>SWIG\uff08Simplified Wrapper and Interface Generator\uff09\u662f\u4e00\u4e2a\u7528\u4e8e\u8fde\u63a5C\/C++\u4ee3\u7801\u548c\u591a\u79cd\u9ad8\u7ea7\u7f16\u7a0b\u8bed\u8a00\u7684\u5de5\u5177\u3002\u5b83\u53ef\u4ee5\u81ea\u52a8\u751f\u6210Python\u7684C\/C++\u4ee3\u7801\u7ed1\u5b9a\u3002<\/p>\n<\/p>\n<p><h4>1. \u7f16\u5199C\u4ee3\u7801\u548c\u63a5\u53e3\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u9996\u5148\u7f16\u5199C\u4ee3\u7801\u548c\u4e00\u4e2aSWIG\u63a5\u53e3\u6587\u4ef6\u3002\u4f8b\u5982\uff0c<code>example.c<\/code>\u548c<code>example.i<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-c\">\/\/ example.c<\/p>\n<p>#include &lt;stdio.h&gt;<\/p>\n<p>void hello() {<\/p>\n<p>    printf(&quot;Hello from C!\\n&quot;);<\/p>\n<p>}<\/p>\n<p>int add(int a, int b) {<\/p>\n<p>    return a + b;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><pre><code class=\"language-c\">\/\/ example.i<\/p>\n<p>%module example<\/p>\n<p>%{<\/p>\n<p>#include &quot;example.c&quot;<\/p>\n<p>%}<\/p>\n<p>void hello();<\/p>\n<p>int add(int a, int b);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528SWIG\u751f\u6210Python\u7ed1\u5b9a\u5e76\u7f16\u8bd1<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528SWIG\u751f\u6210Python\u7ed1\u5b9a\u4ee3\u7801\uff0c\u5e76\u7f16\u8bd1\u751f\u6210\u5171\u4eab\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">swig -python -o example_wrap.c example.i<\/p>\n<p>gcc -shared -o _example.so example.c example_wrap.c -I\/usr\/include\/python3.8<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u4f7f\u7528\u751f\u6210\u7684\u6a21\u5757\u8c03\u7528C\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\u4f7f\u7528\u751f\u6210\u7684\u6a21\u5757\u8c03\u7528C\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import example<\/p>\n<h2><strong>\u8c03\u7528hello\u51fd\u6570<\/strong><\/h2>\n<p>example.hello()<\/p>\n<h2><strong>\u8c03\u7528add\u51fd\u6570<\/strong><\/h2>\n<p>result = example.add(2, 3)<\/p>\n<p>print(f&quot;Result of add: {result}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>SWIG\u9002\u7528\u4e8e\u9700\u8981\u5904\u7406\u5927\u91cfC\/C++\u4ee3\u7801\u7684\u573a\u666f\uff0c\u80fd\u591f\u81ea\u52a8\u751f\u6210\u5927\u91cf\u7ed1\u5b9a\u4ee3\u7801\uff0c\u51cf\u5c11\u624b\u5de5\u7f16\u5199\u7684\u5de5\u4f5c\u91cf\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u7f16\u5199Python\u6269\u5c55\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u7f16\u5199Python\u6269\u5c55\u6a21\u5757\u662f\u6700\u590d\u6742\u4f46\u4e5f\u662f\u6700\u9ad8\u6548\u7684\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u9700\u8981\u9ad8\u6027\u80fd\u548c\u590d\u6742\u529f\u80fd\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>1. \u7f16\u5199C\u4ee3\u7801\u548cPython\u6269\u5c55\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u9996\u5148\u7f16\u5199C\u4ee3\u7801\u548cPython\u6269\u5c55\u6a21\u5757\u3002\u4f8b\u5982\uff0c<code>example.c<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-c\">#include &lt;Python.h&gt;<\/p>\n<p>\/\/ \u5b9a\u4e49hello\u51fd\u6570\u7684\u5305\u88c5\u5668<\/p>\n<p>static PyObject* py_hello(PyObject* self, PyObject* args) {<\/p>\n<p>    printf(&quot;Hello from C!\\n&quot;);<\/p>\n<p>    Py_RETURN_NONE;<\/p>\n<p>}<\/p>\n<p>\/\/ \u5b9a\u4e49add\u51fd\u6570\u7684\u5305\u88c5\u5668<\/p>\n<p>static PyObject* py_add(PyObject* self, PyObject* args) {<\/p>\n<p>    int a, b;<\/p>\n<p>    if (!PyArg_ParseTuple(args, &quot;ii&quot;, &amp;a, &amp;b)) {<\/p>\n<p>        return NULL;<\/p>\n<p>    }<\/p>\n<p>    int result = a + b;<\/p>\n<p>    return Py_BuildValue(&quot;i&quot;, result);<\/p>\n<p>}<\/p>\n<p>\/\/ \u5b9a\u4e49\u65b9\u6cd5\u8868<\/p>\n<p>static PyMethodDef ExampleMethods[] = {<\/p>\n<p>    {&quot;hello&quot;, py_hello, METH_NOARGS, &quot;Print hello from C&quot;},<\/p>\n<p>    {&quot;add&quot;, py_add, METH_VARARGS, &quot;Add two integers&quot;},<\/p>\n<p>    {NULL, NULL, 0, NULL}<\/p>\n<p>};<\/p>\n<p>\/\/ \u5b9a\u4e49\u6a21\u5757<\/p>\n<p>static struct PyModuleDef examplemodule = {<\/p>\n<p>    PyModuleDef_HEAD_INIT,<\/p>\n<p>    &quot;example&quot;,<\/p>\n<p>    NULL,<\/p>\n<p>    -1,<\/p>\n<p>    ExampleMethods<\/p>\n<p>};<\/p>\n<p>\/\/ \u521d\u59cb\u5316\u6a21\u5757<\/p>\n<p>PyMODINIT_FUNC PyInit_example(void) {<\/p>\n<p>    return PyModule_Create(&amp;examplemodule);<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u7f16\u8bd1\u751f\u6210Python\u6269\u5c55\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u7f16\u5199<code>setup.py<\/code>\u811a\u672c\uff0c\u7528\u4e8e\u7f16\u8bd1\u751f\u6210Python\u6269\u5c55\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from distutils.core import setup, Extension<\/p>\n<p>module = Extension(&#39;example&#39;, sources=[&#39;example.c&#39;])<\/p>\n<p>setup(name=&#39;ExampleModule&#39;,<\/p>\n<p>      version=&#39;1.0&#39;,<\/p>\n<p>      description=&#39;This is a demo package&#39;,<\/p>\n<p>      ext_modules=[module])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u7f16\u8bd1\u751f\u6210\u6269\u5c55\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">python setup.py build<\/p>\n<p>python setup.py install<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u4f7f\u7528\u751f\u6210\u7684\u6269\u5c55\u6a21\u5757\u8c03\u7528C\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\u4f7f\u7528\u751f\u6210\u7684\u6269\u5c55\u6a21\u5757\u8c03\u7528C\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import example<\/p>\n<h2><strong>\u8c03\u7528hello\u51fd\u6570<\/strong><\/h2>\n<p>example.hello()<\/p>\n<h2><strong>\u8c03\u7528add\u51fd\u6570<\/strong><\/h2>\n<p>result = example.add(2, 3)<\/p>\n<p>print(f&quot;Result of add: {result}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7f16\u5199Python\u6269\u5c55\u6a21\u5757\u867d\u7136\u590d\u6742\uff0c\u4f46\u53ef\u4ee5\u63d0\u4f9b\u6700\u9ad8\u7684\u6027\u80fd\u548c\u6700\u5927\u7684\u7075\u6d3b\u6027\uff0c\u9002\u7528\u4e8e\u9700\u8981\u9ad8\u6548\u5904\u7406\u548c\u590d\u6742\u529f\u80fd\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86Python\u8c03\u7528C\u7a0b\u5e8f\u7684\u56db\u79cd\u4e3b\u8981\u65b9\u5f0f\uff1actypes\u3001cffi\u3001SWIG\u3001\u4ee5\u53ca\u7f16\u5199Python\u6269\u5c55\u6a21\u5757\u3002<strong>ctypes\u548ccffi\u9002\u7528\u4e8e\u5feb\u901f\u5f00\u53d1\u548c\u8c03\u7528\u7b80\u5355C\u51fd\u6570\u7684\u573a\u666f\uff0c\u5177\u6709\u8f83\u9ad8\u7684\u7075\u6d3b\u6027\u548c\u6613\u7528\u6027<\/strong>\uff1b<strong>SWIG\u9002\u7528\u4e8e\u9700\u8981\u5904\u7406\u5927\u91cfC\/C++\u4ee3\u7801\u7684\u573a\u666f\uff0c\u80fd\u591f\u81ea\u52a8\u751f\u6210\u7ed1\u5b9a\u4ee3\u7801<\/strong>\uff1b<strong>\u7f16\u5199Python\u6269\u5c55\u6a21\u5757\u9002\u7528\u4e8e\u9700\u8981\u9ad8\u6027\u80fd\u548c\u590d\u6742\u529f\u80fd\u7684\u573a\u666f\uff0c\u5c3d\u7ba1\u590d\u6742\u4f46\u63d0\u4f9b\u4e86\u6700\u5927\u7684\u7075\u6d3b\u6027\u548c\u6548\u7387<\/strong>\u3002<\/p>\n<\/p>\n<p><p>\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u662f\u4e3a\u4e86\u5728Python\u4e2d\u5229\u7528C\u4ee3\u7801\u7684\u9ad8\u6548\u6027\u548c\u529f\u80fd\u7279\u6027\uff0c\u6839\u636e\u5177\u4f53\u7684\u9700\u6c42\u9009\u62e9\u6700\u5408\u9002\u7684\u5de5\u5177\u548c\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u63d0\u5347\u5f00\u53d1\u6548\u7387\u548c\u7a0b\u5e8f\u6027\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8c03\u7528C\u7a0b\u5e8f\uff1f<\/strong><br \/>\u5728Python\u4e2d\u8c03\u7528C\u7a0b\u5e8f\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5305\u62ec\u4f7f\u7528ctypes\u3001cffi\u6216\u7f16\u5199Python\u6269\u5c55\u6a21\u5757\u3002ctypes\u662f\u4e00\u4e2a\u5185\u7f6e\u5e93\uff0c\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528C\u8bed\u8a00\u7f16\u5199\u7684\u5171\u4eab\u5e93\uff0c\u800ccffi\u5219\u63d0\u4f9b\u4e86\u66f4\u4e3a\u7075\u6d3b\u7684C\u63a5\u53e3\u3002\u7f16\u5199Python\u6269\u5c55\u6a21\u5757\u9700\u8981\u5bf9Python\u7684C API\u6709\u4e00\u5b9a\u4e86\u89e3\uff0c\u4f46\u53ef\u4ee5\u5b9e\u73b0\u66f4\u9ad8\u6548\u7684\u8c03\u7528\u3002<\/p>\n<p><strong>\u8c03\u7528C\u7a0b\u5e8f\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u7f16\u8bd1\u9009\u9879\uff1f<\/strong><br \/>\u5728\u7f16\u8bd1C\u7a0b\u5e8f\u65f6\uff0c\u786e\u4fdd\u4f7f\u7528\u4e86\u6b63\u786e\u7684\u7f16\u8bd1\u9009\u9879\u3002\u901a\u5e38\uff0c\u9700\u8981\u4f7f\u7528<code>-shared<\/code>\u9009\u9879\u6765\u751f\u6210\u5171\u4eab\u5e93\uff0c\u5e76\u786e\u4fdd\u4e0ePython\u7248\u672c\u517c\u5bb9\u3002\u5bf9\u4e8e\u4f7f\u7528ctypes\u6216cffi\u7684\u60c5\u51b5\uff0cC\u4ee3\u7801\u9700\u8981\u7b26\u5408C\u6807\u51c6\uff0c\u4ee5\u907f\u514d\u5728\u8c03\u7528\u65f6\u51fa\u73b0\u4e0d\u517c\u5bb9\u7684\u95ee\u9898\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406C\u4ee3\u7801\u548cPython\u4e4b\u95f4\u7684\u6570\u636e\u7c7b\u578b\u8f6c\u6362\uff1f<\/strong><br \/>\u5728\u8c03\u7528C\u7a0b\u5e8f\u65f6\uff0c\u6570\u636e\u7c7b\u578b\u7684\u8f6c\u6362\u662f\u4e00\u4e2a\u91cd\u8981\u73af\u8282\u3002ctypes\u63d0\u4f9b\u4e86\u591a\u79cd\u6570\u636e\u7c7b\u578b\u652f\u6301\uff0c\u53ef\u4ee5\u76f4\u63a5\u6620\u5c04C\u4e2d\u7684\u57fa\u672c\u7c7b\u578b\u5230Python\u4e2d\uff0c\u5982<code>c_int<\/code>\u3001<code>c_float<\/code>\u7b49\u3002\u5bf9\u4e8e\u590d\u6742\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u4f7f\u7528ctypes\u7684\u7ed3\u6784\u4f53\u6216\u6570\u7ec4\u7279\u6027\uff0c\u786e\u4fdd\u5728\u4f20\u9012\u6570\u636e\u65f6\u683c\u5f0f\u6b63\u786e\u3002\u5fc5\u8981\u65f6\uff0c\u53ef\u4ee5\u7f16\u5199\u8f6c\u6362\u51fd\u6570\uff0c\u4ee5\u4fbf\u4e8e\u66f4\u590d\u6742\u7684\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8c03\u7528C\u7a0b\u5e8f\u7684\u65b9\u5f0f\u6709\u5f88\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528ctypes\u3001cffi\u3001SWIG\u3001\u4ee5\u53ca\u7f16\u5199Python\u6269\u5c55\u6a21\u5757\u7b49 [&hellip;]","protected":false},"author":3,"featured_media":1171898,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1171889"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1171889"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1171889\/revisions"}],"predecessor-version":[{"id":1171900,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1171889\/revisions\/1171900"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1171898"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1171889"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1171889"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1171889"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}