{"id":1089609,"date":"2025-01-08T13:54:58","date_gmt":"2025-01-08T05:54:58","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1089609.html"},"modified":"2025-01-08T13:55:00","modified_gmt":"2025-01-08T05:55:00","slug":"python-%e5%a6%82%e4%bd%95%e8%b0%83%e7%94%a8%e4%b8%80%e4%b8%aac%e6%a8%a1%e5%9d%97-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1089609.html","title":{"rendered":"python \u5982\u4f55\u8c03\u7528\u4e00\u4e2ac\u6a21\u5757"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24202443\/5e643297-802f-4fae-9e92-3b21c1d780f1.webp\" alt=\"python \u5982\u4f55\u8c03\u7528\u4e00\u4e2ac\u6a21\u5757\" \/><\/p>\n<p><p> <strong>Python \u8c03\u7528 C \u6a21\u5757\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528 ctypes\u3001cffi \u548c\u7f16\u5199 Python \u6269\u5c55\u6a21\u5757\u3002<\/strong> Python \u53ef\u4ee5\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\u4e0e C \u8bed\u8a00\u7684\u4ee3\u7801\u8fdb\u884c\u4ea4\u4e92\uff0c\u4ece\u800c\u63d0\u9ad8\u6267\u884c\u6548\u7387\u3001\u91cd\u7528\u5df2\u6709\u7684 C \u4ee3\u7801\u6216\u4f7f\u7528\u7279\u5b9a\u7684 C \u5e93\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u63cf\u8ff0\u6bcf\u79cd\u65b9\u6cd5\u53ca\u5176\u4f7f\u7528\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001CTYPES<\/h3>\n<\/p>\n<p><p><strong>ctypes \u662f Python \u7684\u4e00\u4e2a\u5916\u90e8\u51fd\u6570\u5e93\uff0c\u5b83\u5141\u8bb8\u8c03\u7528\u52a8\u6001\u94fe\u63a5\u5e93\u4e2d\u7684\u51fd\u6570\u3002<\/strong><\/p>\n<\/p>\n<p><h4>1\u3001\u52a0\u8f7d C \u5e93<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684 C \u4ee3\u7801\uff0c\u5e76\u5c06\u5176\u7f16\u8bd1\u4e3a\u52a8\u6001\u94fe\u63a5\u5e93\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u7b80\u5355\u7684 C \u51fd\u6570 <code>add<\/code>\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\uff08\u5728 Linux \u4e0a\uff09\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">gcc -shared -o libadd.so -fPIC add.c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728 Windows \u4e0a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">gcc -shared -o add.dll add.c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528 ctypes \u8c03\u7528 C \u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u5728 Python \u4e2d\u4f7f\u7528 <code>ctypes<\/code> \u52a0\u8f7d\u5e76\u8c03\u7528\u8fd9\u4e2a\u5e93\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>if __name__ == &quot;__m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n__&quot;:<\/p>\n<p>    lib = ctypes.CDLL(&#39;.\/libadd.so&#39;)  # \u5728 Windows \u4e0a\u4f7f\u7528 &#39;add.dll&#39;<\/p>\n<p>    # \u5b9a\u4e49\u51fd\u6570\u539f\u578b<\/p>\n<p>    lib.add.argtypes = (ctypes.c_int, ctypes.c_int)<\/p>\n<p>    lib.add.restype = ctypes.c_int<\/p>\n<p>    # \u8c03\u7528\u51fd\u6570<\/p>\n<p>    result = lib.add(5, 3)<\/p>\n<p>    print(f&quot;Result of add(5, 3): {result}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001CFFI<\/h3>\n<\/p>\n<p><p><strong>CFFI (C Foreign Function Interface) \u662f\u53e6\u4e00\u4e2a\u7528\u4e8e\u8c03\u7528 C \u4ee3\u7801\u7684\u5e93\uff0c\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7ea7\u7684\u63a5\u53e3\u3002<\/strong><\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5 CFFI<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u901a\u8fc7 pip \u5b89\u88c5 <code>cffi<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install cffi<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u7f16\u5199\u5e76\u52a0\u8f7d C \u4ee3\u7801<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528 CFFI \u52a0\u8f7d\u5e76\u8c03\u7528 C \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\u4e49 C \u51fd\u6570<\/strong><\/h2>\n<p>ffi.cdef(&quot;&quot;&quot;<\/p>\n<p>    int add(int, int);<\/p>\n<p>&quot;&quot;&quot;)<\/p>\n<h2><strong>\u52a0\u8f7d\u5171\u4eab\u5e93<\/strong><\/h2>\n<p>C = ffi.dlopen(&#39;.\/libadd.so&#39;)  # \u5728 Windows \u4e0a\u4f7f\u7528 &#39;add.dll&#39;<\/p>\n<h2><strong>\u8c03\u7528 C \u51fd\u6570<\/strong><\/h2>\n<p>result = C.add(5, 3)<\/p>\n<p>print(f&quot;Result of add(5, 3): {result}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u7f16\u5199 Python \u6269\u5c55\u6a21\u5757<\/h3>\n<\/p>\n<p><p><strong>\u7f16\u5199 Python \u6269\u5c55\u6a21\u5757\u662f\u6700\u7075\u6d3b\u3001\u6027\u80fd\u6700\u597d\u7684\u65b9\u6cd5\uff0c\u4f46\u4e5f\u6700\u590d\u6742\u3002<\/strong><\/p>\n<\/p>\n<p><h4>1\u3001\u7f16\u5199 C \u6269\u5c55\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u7f16\u5199 C \u4ee3\u7801\u5e76\u5b9a\u4e49 Python \u63a5\u53e3\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-c\">\/\/ mymodule.c<\/p>\n<p>#include &lt;Python.h&gt;<\/p>\n<p>\/\/ \u5b9a\u4e49 C \u51fd\u6570<\/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>    return PyLong_FromLong(a + b);<\/p>\n<p>}<\/p>\n<p>\/\/ \u5b9a\u4e49\u6a21\u5757\u65b9\u6cd5<\/p>\n<p>static PyMethodDef MyModuleMethods[] = {<\/p>\n<p>    {&quot;add&quot;, py_add, METH_VARARGS, &quot;Add two numbers&quot;},<\/p>\n<p>    {NULL, NULL, 0, NULL}<\/p>\n<p>};<\/p>\n<p>\/\/ \u5b9a\u4e49\u6a21\u5757<\/p>\n<p>static struct PyModuleDef mymodule = {<\/p>\n<p>    PyModuleDef_HEAD_INIT,<\/p>\n<p>    &quot;mymodule&quot;,<\/p>\n<p>    NULL,<\/p>\n<p>    -1,<\/p>\n<p>    MyModuleMethods<\/p>\n<p>};<\/p>\n<p>\/\/ \u521d\u59cb\u5316\u6a21\u5757<\/p>\n<p>PyMODINIT_FUNC PyInit_mymodule(void) {<\/p>\n<p>    return PyModule_Create(&amp;mymodule);<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u7f16\u8bd1\u5e76\u5b89\u88c5\u6269\u5c55\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a <code>setup.py<\/code> \u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from setuptools import setup, Extension<\/p>\n<p>module = Extension(&#39;mymodule&#39;, sources=[&#39;mymodule.c&#39;])<\/p>\n<p>setup(<\/p>\n<p>    name=&#39;mymodule&#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>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u7f16\u8bd1\u5e76\u5b89\u88c5\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\u3001\u5728 Python \u4e2d\u4f7f\u7528\u6269\u5c55\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u5728 Python \u4e2d\u5bfc\u5165\u5e76\u4f7f\u7528\u6269\u5c55\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import mymodule<\/p>\n<p>result = mymodule.add(5, 3)<\/p>\n<p>print(f&quot;Result of add(5, 3): {result}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0a\u65b9\u6cd5\u4e2d\uff0c<strong>ctypes<\/strong> \u9002\u5408\u5feb\u901f\u4e14\u7b80\u5355\u7684\u8c03\u7528\u3001<strong>cffi<\/strong> \u63d0\u4f9b\u4e86\u66f4\u9ad8\u7ea7\u7684\u529f\u80fd\u548c\u66f4\u597d\u7684\u6027\u80fd\uff0c\u800c<strong>\u7f16\u5199 Python \u6269\u5c55\u6a21\u5757<\/strong> \u5219\u63d0\u4f9b\u4e86\u6700\u7075\u6d3b\u548c\u9ad8\u6027\u80fd\u7684\u89e3\u51b3\u65b9\u6848\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u52a3\uff0c\u9009\u62e9\u54ea\u79cd\u65b9\u5f0f\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u4f7f\u7528\u573a\u666f\u548c\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8c03\u7528C\u6a21\u5757\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u8c03\u7528C\u6a21\u5757\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Python\u7684C API\uff0c\u6216\u8005\u901a\u8fc7<code>ctypes<\/code>\u6216<code>cffi<\/code>\u7b49\u5e93\u6765\u52a0\u8f7dC\u7f16\u5199\u7684\u5171\u4eab\u5e93\u3002\u60a8\u9700\u8981\u5148\u7f16\u8bd1C\u4ee3\u7801\u4e3a\u5171\u4eab\u5e93\u683c\u5f0f\uff08\u5982<code>.so<\/code>\u6216<code>.dll<\/code>\uff09\uff0c\u7136\u540e\u5728Python\u4ee3\u7801\u4e2d\u5f15\u7528\u5b83\u3002<\/p>\n<p><strong>\u4f7f\u7528C\u7f16\u5199\u7684\u6a21\u5757\u5728Python\u4e2d\u6709\u54ea\u4e9b\u6027\u80fd\u4f18\u52bf\uff1f<\/strong><br \/>C\u8bed\u8a00\u7684\u6267\u884c\u901f\u5ea6\u901a\u5e38\u6bd4Python\u5feb\u5f97\u591a\uff0c\u56e0\u6b64\u5728\u9700\u8981\u9ad8\u6027\u80fd\u8ba1\u7b97\u6216\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\uff0c\u4f7f\u7528C\u6a21\u5757\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u6548\u7387\u3002\u6b64\u5916\uff0cC\u8bed\u8a00\u5bf9\u4e8e\u5e95\u5c42\u786c\u4ef6\u7684\u8bbf\u95ee\u548c\u63a7\u5236\u66f4\u4e3a\u7075\u6d3b\uff0c\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u529f\u80fd\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u8c03\u7528C\u6a21\u5757\u65f6\uff0c\u5982\u4f55\u5904\u7406\u6570\u636e\u7c7b\u578b\u7684\u8f6c\u6362\uff1f<\/strong><br \/>\u5728\u8c03\u7528C\u51fd\u6570\u65f6\uff0c\u60a8\u9700\u8981\u6ce8\u610f\u6570\u636e\u7c7b\u578b\u7684\u5339\u914d\u3002Python\u4e2d\u7684\u6570\u636e\u7c7b\u578b\u548cC\u8bed\u8a00\u4e2d\u7684\u6570\u636e\u7c7b\u578b\u5e76\u4e0d\u5b8c\u5168\u76f8\u540c\u3002\u53ef\u4ee5\u4f7f\u7528<code>ctypes<\/code>\u5e93\u63d0\u4f9b\u7684\u7c7b\u578b\u8fdb\u884c\u8f6c\u6362\uff0c\u4f8b\u5982\u4f7f\u7528<code>ctypes.c_int<\/code>\u6765\u5904\u7406C\u7684\u6574\u578b\u3002\u786e\u4fdd\u5728\u4f20\u9012\u53c2\u6570\u65f6\u6b63\u786e\u5339\u914d\u7c7b\u578b\uff0c\u4ee5\u907f\u514d\u8fd0\u884c\u65f6\u9519\u8bef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python \u8c03\u7528 C \u6a21\u5757\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528 ctypes\u3001cffi \u548c\u7f16\u5199 Python \u6269\u5c55\u6a21\u5757\u3002 [&hellip;]","protected":false},"author":3,"featured_media":1089615,"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\/1089609"}],"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=1089609"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1089609\/revisions"}],"predecessor-version":[{"id":1089618,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1089609\/revisions\/1089618"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1089615"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1089609"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1089609"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1089609"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}