{"id":952674,"date":"2024-12-27T01:35:00","date_gmt":"2024-12-26T17:35:00","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/952674.html"},"modified":"2024-12-27T01:35:01","modified_gmt":"2024-12-26T17:35:01","slug":"python%e5%a6%82%e4%bd%95%e8%b0%83%e7%94%a8-so","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/952674.html","title":{"rendered":"python\u5982\u4f55\u8c03\u7528.so"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25090440\/401a81de-d2da-40a8-a8c0-fc721c4b4715.webp\" alt=\"python\u5982\u4f55\u8c03\u7528.so\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u8c03\u7528.so\u6587\u4ef6\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528ctypes\u3001cffi\u548ccython\u7b49\u5de5\u5177\uff0c\u8fd9\u4e9b\u5de5\u5177\u5141\u8bb8Python\u7a0b\u5e8f\u4e0eC\u8bed\u8a00\u7f16\u5199\u7684\u5171\u4eab\u5e93\uff08.so\u6587\u4ef6\uff09\u8fdb\u884c\u4ea4\u4e92\u3002\u5176\u4e2d\uff0c\u4f7f\u7528ctypes\u5e93\u662f\u6700\u5e38\u89c1\u548c\u76f4\u63a5\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u4e0d\u9700\u8981\u989d\u5916\u5b89\u88c5\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528ctypes\u8c03\u7528.so\u6587\u4ef6\u7684\u6b65\u9aa4\u5305\u62ec\uff1a\u9996\u5148\u52a0\u8f7d.so\u6587\u4ef6\uff0c\u7136\u540e\u5b9a\u4e49\u51fd\u6570\u539f\u578b\uff0c\u6700\u540e\u8c03\u7528\u76ee\u6807\u51fd\u6570\u3002\u4ee5\u4e0b\u662f\u5173\u4e8e\u5982\u4f55\u4f7f\u7528ctypes\u8c03\u7528.so\u6587\u4ef6\u7684\u8be6\u7ec6\u63cf\u8ff0\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001CTYPES\u6a21\u5757\u7684\u4f7f\u7528<\/h3>\n<\/p>\n<p><h4>1. \u52a0\u8f7d\u5171\u4eab\u5e93<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u8c03\u7528.so\u6587\u4ef6\uff0c\u9996\u5148\u9700\u8981\u4f7f\u7528<code>ctypes.CDLL<\/code>\u6765\u52a0\u8f7d\u5171\u4eab\u5e93\u3002CDLL\u662fctypes\u6a21\u5757\u4e2d\u7684\u4e00\u4e2a\u7c7b\uff0c\u4e13\u95e8\u7528\u4e8e\u52a0\u8f7dC\u8bed\u8a00\u7f16\u5199\u7684\u5171\u4eab\u5e93\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from ctypes import CDLL<\/p>\n<h2><strong>\u5047\u8bbe\u5171\u4eab\u5e93\u540d\u4e3alibexample.so<\/strong><\/h2>\n<p>lib = CDLL(&#39;.\/libexample.so&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>CDLL<\/code>\u51fd\u6570\u52a0\u8f7d\u4e86\u5f53\u524d\u76ee\u5f55\u4e0b\u540d\u4e3a<code>libexample.so<\/code>\u7684\u5171\u4eab\u5e93\u3002\u786e\u4fdd\u8def\u5f84\u6b63\u786e\uff0c\u5e76\u4e14.so\u6587\u4ef6\u5728Python\u811a\u672c\u7684\u53ef\u8bbf\u95ee\u8def\u5f84\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>2. \u5b9a\u4e49\u51fd\u6570\u539f\u578b<\/h4>\n<\/p>\n<p><p>\u52a0\u8f7d.so\u6587\u4ef6\u540e\uff0c\u9700\u8981\u5b9a\u4e49\u6240\u9700\u8c03\u7528\u51fd\u6570\u7684\u539f\u578b\uff0c\u5305\u62ec\u51fd\u6570\u540d\u3001\u53c2\u6570\u7c7b\u578b\u548c\u8fd4\u56de\u503c\u7c7b\u578b\u3002\u8fd9\u4e00\u6b65\u9aa4\u786e\u4fddPython\u80fd\u591f\u6b63\u786e\u5730\u8c03\u7528\u548c\u4f20\u9012\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5047\u8bbe.so\u6587\u4ef6\u4e2d\u6709\u4e00\u4e2a\u51fd\u6570int add(int a, int b)<\/p>\n<p>lib.add.argtypes = (ctypes.c_int, ctypes.c_int)<\/p>\n<p>lib.add.restype = ctypes.c_int<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86<code>add<\/code>\u51fd\u6570\uff0c\u6307\u5b9a\u5176\u63a5\u53d7\u4e24\u4e2a<code>int<\/code>\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a<code>int<\/code>\u503c\u3002<\/p>\n<\/p>\n<p><h4>3. \u8c03\u7528\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u5b9a\u4e49\u51fd\u6570\u539f\u578b\u540e\uff0c\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528\u8be5\u51fd\u6570\uff0c\u5c31\u50cf\u8c03\u7528Python\u51fd\u6570\u4e00\u6837\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">result = lib.add(3, 5)<\/p>\n<p>print(result)  # \u8f93\u51fa\u7ed3\u679c\u4e3a8<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u8c03\u7528\u4e86<code>add<\/code>\u51fd\u6570\uff0c\u5e76\u4f20\u9012\u4e86\u4e24\u4e2a\u6574\u6570\u53c2\u65703\u548c5\u3002\u8fd4\u56de\u503c\u5b58\u50a8\u5728\u53d8\u91cf<code>result<\/code>\u4e2d\u5e76\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001CFFI\u6a21\u5757\u7684\u4f7f\u7528<\/h3>\n<\/p>\n<p><p>CFFI\uff08C Foreign Function Interface\uff09\u662fPython\u7684\u53e6\u4e00\u4e2a\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e00\u79cd\u8c03\u7528C\u4ee3\u7801\u7684\u65b9\u5f0f\u3002CFFI\u6bd4ctypes\u66f4\u7075\u6d3b\uff0c\u4f46\u9700\u8981\u989d\u5916\u5b89\u88c5\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5CFFI<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f7f\u7528pip\u5b89\u88c5CFFI\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">pip install cffi<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u52a0\u8f7d\u5e76\u8c03\u7528.so\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528CFFI\u8c03\u7528.so\u6587\u4ef6\u7684\u6b65\u9aa4\u4e0ectypes\u7565\u6709\u4e0d\u540c\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\u539f\u578b<\/strong><\/h2>\n<p>ffi.cdef(&quot;&quot;&quot;<\/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>lib = ffi.dlopen(&#39;.\/libexample.so&#39;)<\/p>\n<h2><strong>\u8c03\u7528\u51fd\u6570<\/strong><\/h2>\n<p>result = lib.add(3, 5)<\/p>\n<p>print(result)  # \u8f93\u51fa\u7ed3\u679c\u4e3a8<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>CFFI\u901a\u8fc7<code>cdef<\/code>\u65b9\u6cd5\u5b9a\u4e49\u4e86C\u51fd\u6570\u7684\u63a5\u53e3\uff0c\u7136\u540e\u901a\u8fc7<code>dlopen<\/code>\u52a0\u8f7d\u5171\u4eab\u5e93\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001CYTHON\u6a21\u5757\u7684\u4f7f\u7528<\/h3>\n<\/p>\n<p><p>Cython\u662f\u4e00\u79cd\u5c06Python\u4ee3\u7801\u8f6c\u6362\u4e3aC\u4ee3\u7801\u7684\u5de5\u5177\uff0c\u9002\u5408\u9700\u8981\u9ad8\u6027\u80fd\u7684\u573a\u5408\u3002\u4f7f\u7528Cython\u8c03\u7528.so\u6587\u4ef6\u9700\u8981\u7f16\u5199Cython\u4ee3\u7801\u5e76\u8fdb\u884c\u7f16\u8bd1\u3002<\/p>\n<\/p>\n<p><h4>1. \u7f16\u5199Cython\u4ee3\u7801<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2aCython\u6587\u4ef6<code>example.pyx<\/code>\uff0c\u5e76\u5728\u5176\u4e2d\u5b9a\u4e49\u5bf9.so\u6587\u4ef6\u7684\u8c03\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-cython\"># example.pyx<\/p>\n<p>cdef extern from &quot;libexample.h&quot;:<\/p>\n<p>    int add(int a, int b)<\/p>\n<p>def py_add(int a, int b):<\/p>\n<p>    return add(a, b)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u7f16\u8bd1Cython\u4ee3\u7801<\/h4>\n<\/p>\n<p><p>\u9700\u8981\u4e00\u4e2a<code>setup.py<\/code>\u6587\u4ef6\u6765\u7f16\u8bd1Cython\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># setup.py<\/p>\n<p>from setuptools import setup<\/p>\n<p>from Cython.Build import cythonize<\/p>\n<p>setup(<\/p>\n<p>    ext_modules=cythonize(&quot;example.pyx&quot;),<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u7f16\u8bd1\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">python setup.py build_ext --inplace<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u8c03\u7528Cython\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u7f16\u8bd1\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u5728Python\u4e2d\u5bfc\u5165Cython\u6a21\u5757\u5e76\u8c03\u7528\u5c01\u88c5\u7684\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from example import py_add<\/p>\n<p>result = py_add(3, 5)<\/p>\n<p>print(result)  # \u8f93\u51fa\u7ed3\u679c\u4e3a8<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u9762\u7684\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528ctypes\u3001cffi\u548ccython\u5728Python\u4e2d\u8c03\u7528.so\u6587\u4ef6\u3002<strong>ctypes\u6700\u7b80\u5355\u6613\u7528\uff0c\u9002\u5408\u5927\u591a\u6570\u5e94\u7528\u573a\u666f\uff1bCFFI\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u7075\u6d3b\u6027\u548c\u66f4\u597d\u7684\u6027\u80fd\uff1bCython\u9002\u5408\u9700\u8981\u9ad8\u6027\u80fd\u5e76\u613f\u610f\u7f16\u5199\u548c\u7f16\u8bd1Cython\u4ee3\u7801\u7684\u7528\u6237\u3002<\/strong>\u9009\u62e9\u54ea\u79cd\u65b9\u5f0f\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u9700\u6c42\u548c\u6027\u80fd\u8981\u6c42\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u786e\u4fdd\u5171\u4eab\u5e93\u7684\u517c\u5bb9\u6027\u548c\u8def\u5f84\u7684\u6b63\u786e\u6027\u662f\u8c03\u7528\u6210\u529f\u7684\u5173\u952e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u52a0\u8f7d\u548c\u4f7f\u7528.so\u6587\u4ef6\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u52a0\u8f7d.so\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528ctypes\u6216cffi\u5e93\u3002ctypes\u662fPython\u81ea\u5e26\u7684\u5e93\uff0c\u53ef\u4ee5\u8f7b\u677e\u8c03\u7528C\u8bed\u8a00\u7f16\u5199\u7684\u5171\u4eab\u5e93\u3002\u4f7f\u7528ctypes\u65f6\uff0c\u9700\u8981\u6307\u5b9a.so\u6587\u4ef6\u7684\u8def\u5f84\uff0c\u5e76\u5b9a\u4e49\u6240\u9700\u7684\u51fd\u6570\u539f\u578b\uff0c\u4ee5\u4fbfPython\u80fd\u591f\u6b63\u786e\u8c03\u7528\u8fd9\u4e9b\u51fd\u6570\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u8c03\u7528.so\u6587\u4ef6\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u4e8b\u9879\uff1f<\/strong><br \/>\u5728\u8c03\u7528.so\u6587\u4ef6\u4e4b\u524d\uff0c\u9700\u8981\u786e\u4fdd.so\u6587\u4ef6\u548cPython\u811a\u672c\u662f\u5728\u76f8\u540c\u7684\u73af\u5883\u4e2d\u8fd0\u884c\uff0c\u4e14\u6240\u9700\u7684\u4f9d\u8d56\u5e93\u5df2\u5b89\u88c5\u3002\u6b64\u5916\uff0c\u786e\u4fdd\u51fd\u6570\u7684\u53c2\u6570\u7c7b\u578b\u548c\u8fd4\u56de\u7c7b\u578b\u5728Python\u4e2d\u6b63\u786e\u6620\u5c04\uff0c\u907f\u514d\u56e0\u6570\u636e\u7c7b\u578b\u4e0d\u5339\u914d\u800c\u5bfc\u81f4\u7684\u9519\u8bef\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9b\u5de5\u5177\u6216\u5e93\u6765\u7b80\u5316Python\u5bf9.so\u6587\u4ef6\u7684\u8c03\u7528\uff1f<\/strong><br \/>\u9664\u4e86ctypes\uff0ccffi\u4e5f\u662f\u4e00\u4e2a\u5e38\u7528\u7684\u5e93\uff0c\u80fd\u591f\u66f4\u65b9\u4fbf\u5730\u4e0eC\u4ee3\u7801\u8fdb\u884c\u4ea4\u4e92\u3002\u5bf9\u4e8e\u66f4\u590d\u6742\u7684C\u6269\u5c55\uff0c\u4f7f\u7528Python\u7684C API\u7f16\u5199\u6269\u5c55\u6a21\u5757\u4e5f\u662f\u4e00\u4e2a\u597d\u9009\u62e9\u3002\u5de5\u5177\u5982SWIG\u548cpybind11\u53ef\u4ee5\u81ea\u52a8\u751f\u6210Python\u7ed1\u5b9a\uff0c\u7b80\u5316\u8fc7\u7a0b\u5e76\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8c03\u7528.so\u6587\u4ef6\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528ctypes\u3001cffi\u548ccython\u7b49\u5de5\u5177\uff0c\u8fd9\u4e9b\u5de5\u5177\u5141\u8bb8Pytho [&hellip;]","protected":false},"author":3,"featured_media":952680,"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\/952674"}],"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=952674"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/952674\/revisions"}],"predecessor-version":[{"id":952683,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/952674\/revisions\/952683"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/952680"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=952674"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=952674"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=952674"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}