{"id":938761,"date":"2024-12-26T20:18:35","date_gmt":"2024-12-26T12:18:35","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/938761.html"},"modified":"2024-12-26T20:18:37","modified_gmt":"2024-12-26T12:18:37","slug":"python%e5%a6%82%e4%bd%95%e5%b5%8c%e5%85%a5c","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/938761.html","title":{"rendered":"python\u5982\u4f55\u5d4c\u5165c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25074103\/802fd1f8-c7dd-48c2-909b-10f0e8816b55.webp\" alt=\"python\u5982\u4f55\u5d4c\u5165c\" \/><\/p>\n<p><p> \u5d4c\u5165C\u8bed\u8a00\u4ee3\u7801\u5230Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7<strong>\u4f7f\u7528Python\u7684C\u6269\u5c55\u6a21\u5757\u3001\u5229\u7528Cython\u3001\u4f7f\u7528ctypes\u5e93\u3001\u901a\u8fc7cffi\u5e93<\/strong>\u7b49\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\u3002\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u521b\u5efaPython\u7684C\u6269\u5c55\u6a21\u5757\uff0c\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u83b7\u5f97\u66f4\u597d\u7684\u6027\u80fd\u63d0\u5347\u3002Cython\u662f\u4e00\u79cd\u4f18\u5316\u5de5\u5177\uff0c\u80fd\u591f\u5c06Python\u4ee3\u7801\u8f6c\u6362\u4e3aC\u4ee3\u7801\u3002ctypes\u548ccffi\u5219\u63d0\u4f9b\u4e86\u5728Python\u4e2d\u8c03\u7528C\u51fd\u6570\u7684\u4fbf\u5229\u9014\u5f84\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u8ba8\u8bba\u6bcf\u4e00\u79cd\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u793a\u4f8b\u4ee3\u7801\u4ee5\u5e2e\u52a9\u7406\u89e3\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528C\u6269\u5c55\u6a21\u5757<\/p>\n<\/p>\n<p><p>Python\u7684C\u6269\u5c55\u6a21\u5757\u662f\u901a\u8fc7\u7f16\u5199C\u4ee3\u7801\uff0c\u7136\u540e\u5c06\u5176\u7f16\u8bd1\u4e3aPython\u53ef\u8c03\u7528\u7684\u6a21\u5757\u3002\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u53ef\u4ee5\u83b7\u5f97\u6700\u9ad8\u7684\u6027\u80fd\u63d0\u5347\uff0c\u56e0\u4e3a\u76f4\u63a5\u5728C\u8bed\u8a00\u5c42\u9762\u8fdb\u884c\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u7f16\u5199C\u4ee3\u7801<\/strong><\/li>\n<\/ol>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684C\u4ee3\u7801\uff0c\u5047\u8bbe\u6211\u4eec\u8981\u5b9e\u73b0\u4e00\u4e2a\u8ba1\u7b97\u52a0\u6cd5\u7684\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-c\">\/\/ add.c<\/p>\n<p>#include &lt;Python.h&gt;<\/p>\n<p>static PyObject* 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>    return PyLong_FromLong(a + b);<\/p>\n<p>}<\/p>\n<p>static PyMethodDef AddMethods[] = {<\/p>\n<p>    {&quot;add&quot;, add, METH_VARARGS, &quot;Add two numbers&quot;},<\/p>\n<p>    {NULL, NULL, 0, NULL}<\/p>\n<p>};<\/p>\n<p>static struct PyModuleDef addmodule = {<\/p>\n<p>    PyModuleDef_HEAD_INIT,<\/p>\n<p>    &quot;add&quot;,<\/p>\n<p>    NULL,<\/p>\n<p>    -1,<\/p>\n<p>    AddMethods<\/p>\n<p>};<\/p>\n<p>PyMODINIT_FUNC PyInit_add(void) {<\/p>\n<p>    return PyModule_Create(&amp;addmodule);<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u7f16\u5199setup.py\u6587\u4ef6<\/strong><\/li>\n<\/ol>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u7f16\u5199\u4e00\u4e2asetup.py\u6587\u4ef6\uff0c\u7528\u4e8e\u6784\u5efa\u548c\u7f16\u8bd1\u6211\u4eec\u7684C\u6269\u5c55\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from setuptools import setup, Extension<\/p>\n<p>module = Extension(&#39;add&#39;, sources=[&#39;add.c&#39;])<\/p>\n<p>setup(name=&#39;AddModule&#39;,<\/p>\n<p>      version=&#39;1.0&#39;,<\/p>\n<p>      description=&#39;A simple C extension module&#39;,<\/p>\n<p>      ext_modules=[module])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u7f16\u8bd1\u6a21\u5757<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u547d\u4ee4\u884c\u4e2d\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u7f16\u8bd1\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python setup.py build<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"4\">\n<li><strong>\u5728Python\u4e2d\u4f7f\u7528\u6a21\u5757<\/strong><\/li>\n<\/ol>\n<p><p>\u7f16\u8bd1\u5b8c\u6210\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Python\u4e2d\u5bfc\u5165\u5e76\u4f7f\u7528\u8fd9\u4e2a\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import add<\/p>\n<p>result = add.add(3, 4)<\/p>\n<p>print(&quot;Result:&quot;, result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u5229\u7528Cython<\/p>\n<\/p>\n<p><p>Cython\u662f\u4e00\u79cd\u7528\u4e8e\u5c06Python\u4ee3\u7801\u8f6c\u6362\u4e3aC\u4ee3\u7801\u7684\u5de5\u5177\u3002\u4f7f\u7528Cython\uff0c\u53ef\u4ee5\u5c06Python\u4ee3\u7801\u4e2d\u7684\u5173\u952e\u90e8\u5206\u8f6c\u6362\u4e3aC\uff0c\u4ee5\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u7f16\u5199Cython\u4ee3\u7801<\/strong><\/li>\n<\/ol>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u7b80\u5355\u7684Python\u51fd\u6570\uff0c\u73b0\u5728\u6211\u4eec\u7528Cython\u6765\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-cython\"># add.pyx<\/p>\n<p>def add(int a, int b):<\/p>\n<p>    return a + b<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u7f16\u5199setup.py\u6587\u4ef6<\/strong><\/li>\n<\/ol>\n<p><p>\u6211\u4eec\u9700\u8981\u7f16\u5199\u4e00\u4e2asetup.py\u6587\u4ef6\uff0c\u7528\u4e8e\u6784\u5efa\u548c\u7f16\u8bd1Cython\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from setuptools import setup<\/p>\n<p>from Cython.Build import cythonize<\/p>\n<p>setup(<\/p>\n<p>    ext_modules=cythonize(&quot;add.pyx&quot;)<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u7f16\u8bd1Cython\u4ee3\u7801<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u547d\u4ee4\u884c\u4e2d\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u7f16\u8bd1Cython\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python setup.py build_ext --inplace<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"4\">\n<li><strong>\u5728Python\u4e2d\u4f7f\u7528Cython\u6a21\u5757<\/strong><\/li>\n<\/ol>\n<p><p>\u7f16\u8bd1\u5b8c\u6210\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Python\u4e2d\u5bfc\u5165\u5e76\u4f7f\u7528\u8fd9\u4e2a\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import add<\/p>\n<p>result = add.add(3, 4)<\/p>\n<p>print(&quot;Result:&quot;, result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528ctypes\u5e93<\/p>\n<\/p>\n<p><p>ctypes\u5e93\u662fPython\u7684\u6807\u51c6\u5e93\uff0c\u53ef\u4ee5\u7528\u6765\u8c03\u7528C\u8bed\u8a00\u7f16\u5199\u7684\u5171\u4eab\u5e93\uff08\u52a8\u6001\u94fe\u63a5\u5e93\uff09\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u7f16\u5199C\u4ee3\u7801\u5e76\u7f16\u8bd1\u4e3a\u5171\u4eab\u5e93<\/strong><\/li>\n<\/ol>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u7f16\u5199\u4e00\u4e2aC\u51fd\u6570\uff0c\u5e76\u5c06\u5176\u7f16\u8bd1\u4e3a\u5171\u4eab\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-c\">\/\/ add.c<\/p>\n<p>#include &lt;stdio.h&gt;<\/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>\u7f16\u8bd1\u4e3a\u5171\u4eab\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">gcc -shared -o libadd.so -fPIC add.c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u5728Python\u4e2d\u4f7f\u7528ctypes\u8c03\u7528C\u51fd\u6570<\/strong><\/li>\n<\/ol>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528ctypes\u5e93\u6765\u52a0\u8f7d\u548c\u8c03\u7528\u5171\u4eab\u5e93\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>lib = ctypes.CDLL(&#39;.\/libadd.so&#39;)<\/p>\n<h2><strong>\u8c03\u7528add\u51fd\u6570<\/strong><\/h2>\n<p>result = lib.add(3, 4)<\/p>\n<p>print(&quot;Result:&quot;, result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u901a\u8fc7cffi\u5e93<\/p>\n<\/p>\n<p><p>cffi\u5e93\u662f\u4e00\u4e2a\u7528\u4e8e\u5728Python\u4e2d\u8c03\u7528C\u4ee3\u7801\u7684\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u66f4\u9ad8\u5c42\u6b21\u7684\u63a5\u53e3\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u7f16\u5199C\u4ee3\u7801<\/strong><\/li>\n<\/ol>\n<p><p>\u4e0ectypes\u7c7b\u4f3c\uff0c\u6211\u4eec\u9700\u8981\u7f16\u5199\u4e00\u4e2aC\u51fd\u6570\uff0c\u5e76\u7f16\u8bd1\u4e3a\u5171\u4eab\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-c\">\/\/ add.c<\/p>\n<p>#include &lt;stdio.h&gt;<\/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>\u7f16\u8bd1\u4e3a\u5171\u4eab\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">gcc -shared -o libadd.so -fPIC add.c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u5728Python\u4e2d\u4f7f\u7528cffi\u8c03\u7528C\u51fd\u6570<\/strong><\/li>\n<\/ol>\n<p><p>\u4f7f\u7528cffi\u5e93\uff0c\u6211\u4eec\u53ef\u4ee5\u8c03\u7528\u5171\u4eab\u5e93\u4e2d\u7684\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>\u58f0\u660e\u51fd\u6570\u539f\u578b<\/strong><\/h2>\n<p>ffi.cdef(&quot;int add(int, int);&quot;)<\/p>\n<h2><strong>\u52a0\u8f7d\u5171\u4eab\u5e93<\/strong><\/h2>\n<p>lib = ffi.dlopen(&#39;.\/libadd.so&#39;)<\/p>\n<h2><strong>\u8c03\u7528add\u51fd\u6570<\/strong><\/h2>\n<p>result = lib.add(3, 4)<\/p>\n<p>print(&quot;Result:&quot;, result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u5728Python\u4e2d\u5d4c\u5165C\u8bed\u8a00\u4ee3\u7801\uff0c\u4ee5\u63d0\u9ad8\u6027\u80fd\u6216\u5b9e\u73b0\u7279\u5b9a\u529f\u80fd\u3002\u4e0d\u540c\u7684\u65b9\u6cd5\u5404\u6709\u4f18\u52a3\uff0c\u9009\u62e9\u9002\u5408\u81ea\u5df1\u9700\u6c42\u7684\u65b9\u5f0f\u8fdb\u884c\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8c03\u7528C\u8bed\u8a00\u7f16\u5199\u7684\u51fd\u6570\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u8c03\u7528C\u8bed\u8a00\u7684\u51fd\u6570\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Python\u7684C\u6269\u5c55\u63a5\u53e3\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u521b\u5efa\u4e00\u4e2aC\u6587\u4ef6\uff0c\u5176\u4e2d\u5305\u542b\u60a8\u5e0c\u671b\u8c03\u7528\u7684\u51fd\u6570\u3002\u7136\u540e\uff0c\u4f7f\u7528Python\u7684C API\u7f16\u8bd1\u8be5\u6587\u4ef6\u4e3a\u5171\u4eab\u5e93\u3002\u6700\u540e\uff0c\u901a\u8fc7ctypes\u6216cffi\u6a21\u5757\u5728Python\u4e2d\u52a0\u8f7d\u5e76\u8c03\u7528\u8fd9\u4e9bC\u51fd\u6570\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u6709\u6548\u5730\u63d0\u9ad8\u6027\u80fd\uff0c\u5c24\u5176\u662f\u5bf9\u4e8e\u8ba1\u7b97\u5bc6\u96c6\u578b\u4efb\u52a1\u3002<\/p>\n<p><strong>\u4f7f\u7528Cython\u662f\u5426\u53ef\u4ee5\u7b80\u5316Python\u4e0eC\u7684\u4ea4\u4e92\uff1f<\/strong><br \/>Cython\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u5e2e\u52a9\u60a8\u5728Python\u4ee3\u7801\u4e2d\u76f4\u63a5\u5d4c\u5165C\u4ee3\u7801\u3002\u901a\u8fc7\u5728Cython\u4e2d\u4f7f\u7528\u7c7b\u578b\u58f0\u660e\uff0c\u60a8\u53ef\u4ee5\u83b7\u5f97\u63a5\u8fd1C\u7684\u6027\u80fd\uff0c\u540c\u65f6\u4fdd\u6301Python\u7684\u6613\u7528\u6027\u3002Cython\u4f1a\u81ea\u52a8\u751f\u6210C\u4ee3\u7801\u5e76\u8fdb\u884c\u7f16\u8bd1\uff0c\u4ece\u800c\u7b80\u5316\u4e86Python\u548cC\u4e4b\u95f4\u7684\u4ea4\u4e92\u3002\u4f7f\u7528Cython\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u8c03\u7528C\u5e93\uff0c\u5e76\u5c06Python\u4ee3\u7801\u8f6c\u6362\u4e3aC\u6269\u5c55\u3002<\/p>\n<p><strong>\u5982\u4f55\u8c03\u8bd5\u5728Python\u4e2d\u5d4c\u5165\u7684C\u4ee3\u7801\uff1f<\/strong><br \/>\u8c03\u8bd5\u5d4c\u5165\u5230Python\u4e2d\u7684C\u4ee3\u7801\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8fdb\u884c\u3002\u4f7f\u7528gdb\u7b49\u8c03\u8bd5\u5de5\u5177\uff0c\u53ef\u4ee5\u76f4\u63a5\u8c03\u8bd5\u751f\u6210\u7684\u5171\u4eab\u5e93\u3002\u6b64\u5916\uff0c\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528Python\u7684\u8c03\u8bd5\u5de5\u5177\uff0c\u5982pdb\uff0c\u7ed3\u5408C\u7684\u8c03\u8bd5\u5de5\u5177\u8fdb\u884c\u9010\u6b65\u8c03\u8bd5\u3002\u5728\u7f16\u8bd1C\u4ee3\u7801\u65f6\uff0c\u786e\u4fdd\u542f\u7528\u8c03\u8bd5\u9009\u9879\uff0c\u8fd9\u6837\u53ef\u4ee5\u83b7\u5f97\u66f4\u8be6\u7ec6\u7684\u8c03\u8bd5\u4fe1\u606f\uff0c\u5e2e\u52a9\u60a8\u5feb\u901f\u5b9a\u4f4d\u95ee\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5d4c\u5165C\u8bed\u8a00\u4ee3\u7801\u5230Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528Python\u7684C\u6269\u5c55\u6a21\u5757\u3001\u5229\u7528Cython\u3001\u4f7f\u7528ctypes\u5e93\u3001\u901a [&hellip;]","protected":false},"author":3,"featured_media":938764,"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\/938761"}],"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=938761"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/938761\/revisions"}],"predecessor-version":[{"id":938765,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/938761\/revisions\/938765"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/938764"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=938761"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=938761"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=938761"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}