{"id":949927,"date":"2024-12-27T00:19:31","date_gmt":"2024-12-26T16:19:31","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/949927.html"},"modified":"2024-12-27T00:19:34","modified_gmt":"2024-12-26T16:19:34","slug":"%e5%a6%82%e4%bd%95%e5%b0%81%e8%a3%85python-dll","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/949927.html","title":{"rendered":"\u5982\u4f55\u5c01\u88c5python dll"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25084729\/c6a58f47-60a8-442a-99bb-2e9dd13832b4.webp\" alt=\"\u5982\u4f55\u5c01\u88c5python dll\" \/><\/p>\n<p><p> <strong>\u5c01\u88c5Python DLL\u7684\u65b9\u6cd5\u4e3b\u8981\u5305\u62ec\uff1a\u4f7f\u7528Cython\u3001\u4f7f\u7528ctypes\u3001\u4f7f\u7528CFFI\u3002<\/strong>\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u5177\u4f53\u9009\u62e9\u53d6\u51b3\u4e8e\u9879\u76ee\u9700\u6c42\u548c\u4e2a\u4eba\u504f\u597d\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Cython\u6765\u5c01\u88c5Python\u4ee3\u7801\u4e3aDLL\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528Cython\u5c01\u88c5Python\u4e3aDLL<\/h3>\n<\/p>\n<p><p>Cython\u662f\u4e00\u79cd\u5e7f\u6cdb\u4f7f\u7528\u7684\u5de5\u5177\uff0c\u7528\u4e8e\u5c06Python\u4ee3\u7801\u7f16\u8bd1\u4e3aC\u4ee3\u7801\uff0c\u4ece\u800c\u751f\u6210DLL\u6587\u4ef6\u3002Cython\u4e0d\u4ec5\u53ef\u4ee5\u63d0\u9ad8Python\u4ee3\u7801\u7684\u6027\u80fd\uff0c\u8fd8\u80fd\u591f\u4f7f\u5176\u66f4\u5bb9\u6613\u4e0eC\/C++\u8fdb\u884c\u4ea4\u4e92\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5Cython<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u786e\u4fdd\u5df2\u5b89\u88c5Cython\u3002\u53ef\u4ee5\u4f7f\u7528pip\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install cython<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u7f16\u5199Python\u4ee3\u7801<\/h4>\n<\/p>\n<p><p>\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684Python\u6a21\u5757\uff0c\u4f8b\u5982<code>example.py<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def greet(name):<\/p>\n<p>    return f&quot;Hello, {name}!&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u521b\u5efaCython\u914d\u7f6e\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u521b\u5efa\u4e00\u4e2a<code>setup.py<\/code>\u6587\u4ef6\uff0c\u7528\u4e8e\u914d\u7f6eCython\u7684\u7f16\u8bd1\u8fc7\u7a0b\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;example.py&quot;),<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4. \u7f16\u8bd1Python\u4ee3\u7801\u4e3aC\u4ee3\u7801<\/h4>\n<\/p>\n<p><p>\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff0c\u5c06Python\u4ee3\u7801\u7f16\u8bd1\u4e3aC\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<p><p>\u8fd9\u5c06\u751f\u6210\u4e00\u4e2a\u5171\u4eab\u5bf9\u8c61\u6587\u4ef6\uff08.so\uff09\u6216DLL\u6587\u4ef6\uff08.pyd\uff09\uff0c\u5177\u4f53\u53d6\u51b3\u4e8e\u64cd\u4f5c\u7cfb\u7edf\u3002<\/p>\n<\/p>\n<p><h4>5. \u4f7f\u7528\u751f\u6210\u7684DLL\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u751f\u6210\u7684DLL\u6587\u4ef6\u53ef\u4ee5\u5728\u5176\u4ed6Python\u4ee3\u7801\u4e2d\u5bfc\u5165\u548c\u4f7f\u7528\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import example<\/p>\n<p>print(example.greet(&quot;World&quot;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528ctypes\u5c01\u88c5Python\u4e3aDLL<\/h3>\n<\/p>\n<p><p><code>ctypes<\/code>\u662fPython\u7684\u4e00\u4e2a\u5185\u5efa\u5e93\uff0c\u5141\u8bb8\u8c03\u7528\u5171\u4eab\u5e93\u4e2d\u7684C\u51fd\u6570\u3002\u901a\u8fc7<code>ctypes<\/code>\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2aC\u52a8\u6001\u94fe\u63a5\u5e93\uff0c\u5e76\u5728Python\u4e2d\u8c03\u7528\u3002<\/p>\n<\/p>\n<p><h4>1. \u7f16\u5199C\u4ee3\u7801<\/h4>\n<\/p>\n<p><p>\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684C\u6587\u4ef6\uff0c\u4f8b\u5982<code>example.c<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-c\">#include &lt;stdio.h&gt;<\/p>\n<p>void greet(const char* name) {<\/p>\n<p>    printf(&quot;Hello, %s!\\n&quot;, name);<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u7f16\u8bd1C\u4ee3\u7801\u4e3aDLL<\/h4>\n<\/p>\n<p><p>\u5728Windows\u4e0a\uff0c\u53ef\u4ee5\u4f7f\u7528<code>gcc<\/code>\u7f16\u8bd1C\u4ee3\u7801\u4e3aDLL\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">gcc -shared -o example.dll example.c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u5728Python\u4e2d\u4f7f\u7528DLL<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>ctypes<\/code>\u52a0\u8f7d\u5e76\u8c03\u7528DLL\u4e2d\u7684\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import ctypes<\/p>\n<h2><strong>Load the DLL<\/strong><\/h2>\n<p>example = ctypes.CDLL(&#39;.\/example.dll&#39;)<\/p>\n<h2><strong>Call the function<\/strong><\/h2>\n<p>example.greet(b&quot;World&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528CFFI\u5c01\u88c5Python\u4e3aDLL<\/h3>\n<\/p>\n<p><p>CFFI\u662f\u4e00\u4e2a\u66f4\u73b0\u4ee3\u7684\u5e93\uff0c\u7528\u4e8e\u5728Python\u4e2d\u8c03\u7528C\u4ee3\u7801\uff0c\u4e0e<code>ctypes<\/code>\u7c7b\u4f3c\uff0c\u4f46\u63d0\u4f9b\u4e86\u66f4\u597d\u7684\u63a5\u53e3\u548c\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5CFFI<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528pip\u5b89\u88c5CFFI\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install cffi<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u7f16\u5199C\u4ee3\u7801\u548cCFFI\u63a5\u53e3<\/h4>\n<\/p>\n<p><p>\u7f16\u5199\u4e00\u4e2aC\u6587\u4ef6\uff0c\u4f8b\u5982<code>example.c<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-c\">#include &lt;stdio.h&gt;<\/p>\n<p>void greet(const char* name) {<\/p>\n<p>    printf(&quot;Hello, %s!\\n&quot;, name);<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7f16\u5199\u4e00\u4e2aPython\u6587\u4ef6\uff0c\u4f8b\u5982<code>build_example.py<\/code>\uff0c\u4f7f\u7528CFFI\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from cffi import FFI<\/p>\n<p>ffi = FFI()<\/p>\n<p>ffi.cdef(&quot;&quot;&quot;<\/p>\n<p>    void greet(const char* name);<\/p>\n<p>&quot;&quot;&quot;)<\/p>\n<p>ffi.set_source(&quot;_example&quot;,<\/p>\n<p>&quot;&quot;&quot;<\/p>\n<p>#include &quot;example.c&quot;<\/p>\n<p>&quot;&quot;&quot;,<\/p>\n<p>sources=[&#39;example.c&#39;])<\/p>\n<p>ffi.compile()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u7f16\u8bd1C\u4ee3\u7801\u4e3aPython\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u4ee5\u7f16\u8bd1C\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python build_example.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u751f\u6210\u4e00\u4e2a\u53ef\u4ee5\u5728Python\u4e2d\u5bfc\u5165\u7684\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><h4>4. \u5728Python\u4e2d\u4f7f\u7528CFFI\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u5bfc\u5165\u5e76\u4f7f\u7528\u751f\u6210\u7684\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import _example<\/p>\n<p>_example.lib.greet(b&quot;World&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u5c06Python\u4ee3\u7801\u5c01\u88c5\u4e3aDLL\u6587\u4ef6\u3002<strong>\u4f7f\u7528Cython\u53ef\u4ee5\u76f4\u63a5\u5c06Python\u4ee3\u7801\u8f6c\u6362\u4e3aC\u4ee3\u7801\u5e76\u751f\u6210DLL\u6587\u4ef6<\/strong>\uff0c\u8fd9\u5bf9\u4e8e\u63d0\u9ad8\u6027\u80fd\u548c\u4e0e\u5176\u4ed6C\/C++\u4ee3\u7801\u96c6\u6210\u975e\u5e38\u6709\u7528\u3002<strong>\u4f7f\u7528ctypes\u548cCFFI\u5219\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528\u5df2\u6709\u7684C\u5e93<\/strong>\uff0c\u5728Python\u4e2d\u5b9e\u73b0C\u51fd\u6570\u7684\u8c03\u7528\u3002\u6839\u636e\u9879\u76ee\u7684\u5177\u4f53\u9700\u6c42\u548c\u73af\u5883\uff0c\u53ef\u4ee5\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u5c01\u88c5Python DLL\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u4ec0\u4e48\u662fPython DLL\uff0c\u4e3a\u4ec0\u4e48\u8981\u5c01\u88c5\u5b83\uff1f<\/strong><br \/>Python DLL\uff08\u52a8\u6001\u94fe\u63a5\u5e93\uff09\u662f\u4e00\u79cd\u5c06Python\u4ee3\u7801\u7f16\u8bd1\u4e3a\u53ef\u4f9b\u5176\u4ed6\u7a0b\u5e8f\u8c03\u7528\u7684\u4e8c\u8fdb\u5236\u683c\u5f0f\u7684\u65b9\u5f0f\u3002\u5c01\u88c5Python DLL\u7684\u4e3b\u8981\u76ee\u7684\u662f\u4e3a\u4e86\u63d0\u9ad8\u4ee3\u7801\u7684\u91cd\u7528\u6027\u3001\u6027\u80fd\u4ee5\u53ca\u4e0e\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\u7684\u517c\u5bb9\u6027\u3002\u901a\u8fc7\u5c06Python\u4ee3\u7801\u5c01\u88c5\u4e3aDLL\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u5728\u4e0d\u540c\u7684\u5e94\u7528\u7a0b\u5e8f\u4e2d\u5171\u4eab\u8fd9\u90e8\u5206\u529f\u80fd\uff0c\u5c24\u5176\u662f\u5728\u9700\u8981\u63d0\u9ad8\u6548\u7387\u7684\u60c5\u51b5\u4e0b\u3002<\/p>\n<p><strong>\u5c01\u88c5Python DLL\u7684\u5e38\u7528\u5de5\u5177\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u5f00\u53d1\u8005\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a\u5de5\u5177\u6765\u5c01\u88c5Python\u4ee3\u7801\u4e3aDLL\uff0c\u5305\u62ec\u4f46\u4e0d\u9650\u4e8eCython\u3001pybind11\u548cctypes\u3002\u8fd9\u4e9b\u5de5\u5177\u5404\u6709\u7279\u70b9\uff0cCython\u9002\u5408\u4e8e\u9700\u8981\u4f18\u5316\u6027\u80fd\u7684\u4ee3\u7801\uff0cpybind11\u5219\u65b9\u4fbf\u4e0eC++\u8fdb\u884c\u4ea4\u4e92\uff0c\u800cctypes\u53ef\u4ee5\u5728Python\u4e2d\u76f4\u63a5\u8c03\u7528DLL\u3002\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u53d6\u51b3\u4e8e\u9879\u76ee\u9700\u6c42\u548c\u76ee\u6807\u8bed\u8a00\u7684\u517c\u5bb9\u6027\u3002<\/p>\n<p><strong>\u5c01\u88c5Python DLL\u7684\u8fc7\u7a0b\u590d\u6742\u5417\uff1f<\/strong><br \/>\u5c01\u88c5Python DLL\u7684\u8fc7\u7a0b\u53ef\u80fd\u4f1a\u6d89\u53ca\u591a\u4e2a\u6b65\u9aa4\uff0c\u5305\u62ec\u7f16\u5199Python\u4ee3\u7801\u3001\u914d\u7f6e\u7f16\u8bd1\u73af\u5883\u3001\u751f\u6210DLL\u6587\u4ef6\u7b49\u3002\u867d\u7136\u6700\u521d\u770b\u8d77\u6765\u6709\u4e9b\u590d\u6742\uff0c\u4f46\u4e00\u65e6\u638c\u63e1\u4e86\u76f8\u5173\u5de5\u5177\u7684\u4f7f\u7528\u548c\u7f16\u8bd1\u7684\u6d41\u7a0b\uff0c\u6574\u4e2a\u8fc7\u7a0b\u4f1a\u53d8\u5f97\u76f8\u5bf9\u7b80\u5355\u3002\u8bb8\u591a\u5f00\u53d1\u8005\u5728\u5c1d\u8bd5\u51e0\u6b21\u540e\uff0c\u5c31\u80fd\u719f\u7ec3\u8fdb\u884c\u5c01\u88c5\uff0c\u5e76\u6709\u6548\u7ba1\u7406\u4ee3\u7801\u7684\u7248\u672c\u548c\u4f9d\u8d56\u5173\u7cfb\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5c01\u88c5Python DLL\u7684\u65b9\u6cd5\u4e3b\u8981\u5305\u62ec\uff1a\u4f7f\u7528Cython\u3001\u4f7f\u7528ctypes\u3001\u4f7f\u7528CFFI\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\uff0c [&hellip;]","protected":false},"author":3,"featured_media":949931,"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\/949927"}],"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=949927"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/949927\/revisions"}],"predecessor-version":[{"id":949932,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/949927\/revisions\/949932"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/949931"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=949927"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=949927"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=949927"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}