{"id":975778,"date":"2024-12-27T06:16:08","date_gmt":"2024-12-26T22:16:08","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/975778.html"},"modified":"2024-12-27T06:16:11","modified_gmt":"2024-12-26T22:16:11","slug":"python%e5%a6%82%e4%bd%95%e8%b0%83%e7%94%a8c-%e4%bb%a3%e7%a0%81","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/975778.html","title":{"rendered":"python\u5982\u4f55\u8c03\u7528c \u4ee3\u7801"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24200845\/e6ba2b08-5a61-4854-883b-805245d4d33d.webp\" alt=\"python\u5982\u4f55\u8c03\u7528c \u4ee3\u7801\" \/><\/p>\n<p><p> <strong>Python\u8c03\u7528C\u4ee3\u7801\u6709\u591a\u79cd\u65b9\u5f0f\uff0c\u4e3b\u8981\u5305\u62ec\uff1a\u4f7f\u7528ctypes\u5e93\u3001\u4f7f\u7528cffi\u5e93\u3001\u7f16\u5199Python\u6269\u5c55\u6a21\u5757\u3002<\/strong> \u5176\u4e2d\uff0c<strong>ctypes\u5e93<\/strong>\u662f\u4e00\u79cd\u76f4\u63a5\u8c03\u7528\u52a8\u6001\u94fe\u63a5\u5e93\u7684\u65b9\u5f0f\uff0c<strong>cffi\u5e93<\/strong>\u5219\u66f4\u52a0\u7075\u6d3b\uff0c\u9002\u5408\u66f4\u590d\u6742\u7684C\u4ee3\u7801\u96c6\u6210\uff0c<strong>\u7f16\u5199Python\u6269\u5c55\u6a21\u5757<\/strong>\u9002\u5408\u9700\u8981\u9ad8\u6027\u80fd\u7684\u573a\u666f\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u79cd\u65b9\u5f0f\uff1a<strong>\u4f7f\u7528ctypes\u5e93<\/strong>\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528ctypes\u5e93\u8c03\u7528C\u4ee3\u7801<\/strong><\/p>\n<\/p>\n<p><p>ctypes\u662fPython\u7684\u4e00\u4e2a\u5916\u90e8\u51fd\u6570\u5e93\u6a21\u5757\uff0c\u5b83\u63d0\u4f9b\u4e86C\u517c\u5bb9\u7684\u6570\u636e\u7c7b\u578b\uff0c\u5e76\u5141\u8bb8\u8c03\u7528DLL\uff08\u52a8\u6001\u94fe\u63a5\u5e93\uff09\u4e2d\u7684C\u51fd\u6570\u6216\u5171\u4eab\u5e93\u3002\u5b83\u53ef\u4ee5\u7528\u6765\u5c01\u88c5\u5df2\u6709\u7684C\u4ee3\u7801\uff0c\u4ece\u800c\u5728Python\u4e2d\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001CTYPES\u5e93\u6982\u8ff0<\/h3>\n<\/p>\n<p><p>ctypes\u662fPython\u81ea\u5e26\u7684\u6807\u51c6\u5e93\uff0c\u9002\u7528\u4e8e\u8c03\u7528C\u8bed\u8a00\u7f16\u5199\u7684\u52a8\u6001\u94fe\u63a5\u5e93\uff08DLL\uff09\u6216\u5171\u4eab\u5e93\uff08.so\u6587\u4ef6\uff09\u3002\u4f7f\u7528ctypes\u65f6\uff0c\u65e0\u9700\u4fee\u6539\u539f\u6709\u7684C\u4ee3\u7801\uff0c\u53ea\u9700\u8981\u7b80\u5355\u5730\u5c01\u88c5\u5373\u53ef\u8c03\u7528\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u52a8\u6001\u52a0\u8f7d\u5e93<\/strong>\uff1actypes\u5141\u8bb8\u5728\u8fd0\u884c\u65f6\u52a8\u6001\u52a0\u8f7d\u5916\u90e8C\u5e93\u3002<\/li>\n<li><strong>\u652f\u6301\u591a\u5e73\u53f0<\/strong>\uff1a\u53ef\u4ee5\u5728Windows\u3001Linux\u548cMacOS\u4e0a\u4f7f\u7528\u3002<\/li>\n<li><strong>\u65e0\u9700\u7f16\u8bd1<\/strong>\uff1a\u4e0e\u5176\u4ed6\u65b9\u6cd5\u4e0d\u540c\uff0cctypes\u4e0d\u9700\u8981\u91cd\u65b0\u7f16\u8bd1C\u4ee3\u7801\u3002<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001\u51c6\u5907C\u4ee3\u7801\u4e0e\u7f16\u8bd1<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4f7f\u7528ctypes\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u51c6\u5907\u4e00\u4e2a\u7b80\u5355\u7684C\u4ee3\u7801\uff0c\u5e76\u5c06\u5176\u7f16\u8bd1\u4e3a\u5171\u4eab\u5e93\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8bC\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-c\">\/\/ simple.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>void hello() {<\/p>\n<p>    printf(&quot;Hello, World from C!\\n&quot;);<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u7f16\u8bd1C\u4ee3\u7801<\/h4>\n<\/p>\n<p><p>\u5728Linux\u6216MacOS\u4e0a\uff0c\u53ef\u4ee5\u4f7f\u7528gcc\u7f16\u8bd1\u5668\u5c06\u4ee3\u7801\u7f16\u8bd1\u4e3a\u5171\u4eab\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">gcc -shared -o simple.so -fPIC simple.c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728Windows\u4e0a\uff0c\u53ef\u4ee5\u4f7f\u7528MinGW\u7f16\u8bd1\u5668\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">gcc -shared -o simple.dll simple.c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u5728Python\u4e2d\u4f7f\u7528CTYPES\u8c03\u7528C\u51fd\u6570<\/h3>\n<\/p>\n<p><h4>1\u3001\u5bfc\u5165ctypes\u5e93<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165ctypes\u5e93\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import ctypes<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u52a0\u8f7d\u5171\u4eab\u5e93<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528ctypes\u7684<code>CDLL<\/code>\u7c7b\u6765\u52a0\u8f7d\u5171\u4eab\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5bf9\u4e8eLinux\u6216MacOS<\/p>\n<p>lib = ctypes.CDLL(&#39;.\/simple.so&#39;)<\/p>\n<h2><strong>\u5bf9\u4e8eWindows<\/strong><\/h2>\n<h2><strong>lib = ctypes.CDLL(&#39;.\/simple.dll&#39;)<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u8c03\u7528C\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u5728\u8c03\u7528C\u51fd\u6570\u4e4b\u524d\uff0c\u9700\u8981\u6307\u5b9a\u53c2\u6570\u548c\u8fd4\u56de\u503c\u7684\u7c7b\u578b\u3002ctypes\u63d0\u4f9b\u4e86\u591a\u79cdC\u6570\u636e\u7c7b\u578b\u7684\u5bf9\u5e94\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6307\u5b9aadd\u51fd\u6570\u7684\u53c2\u6570\u548c\u8fd4\u56de\u503c\u7c7b\u578b<\/p>\n<p>lib.add.argtypes = (ctypes.c_int, ctypes.c_int)<\/p>\n<p>lib.add.restype = ctypes.c_int<\/p>\n<h2><strong>\u8c03\u7528add\u51fd\u6570<\/strong><\/h2>\n<p>result = lib.add(5, 3)<\/p>\n<p>print(f&quot;Result of add: {result}&quot;)<\/p>\n<h2><strong>\u8c03\u7528hello\u51fd\u6570<\/strong><\/h2>\n<p>lib.hello()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406\u590d\u6742\u7684C\u6570\u636e\u7c7b\u578b<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\uff0cC\u51fd\u6570\u53ef\u80fd\u4f1a\u4f7f\u7528\u590d\u6742\u7684\u6570\u636e\u7c7b\u578b\uff0c\u4f8b\u5982\u7ed3\u6784\u4f53\u6216\u6307\u9488\u3002ctypes\u63d0\u4f9b\u4e86\u76f8\u5e94\u7684\u673a\u5236\u6765\u5904\u7406\u8fd9\u4e9b\u590d\u6742\u7684\u6570\u636e\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5904\u7406\u7ed3\u6784\u4f53<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2aC\u7ed3\u6784\u4f53\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-c\">\/\/ complex.c<\/p>\n<p>typedef struct {<\/p>\n<p>    int x;<\/p>\n<p>    int y;<\/p>\n<p>} Point;<\/p>\n<p>Point create_point(int x, int y) {<\/p>\n<p>    Point p;<\/p>\n<p>    p.x = x;<\/p>\n<p>    p.y = y;<\/p>\n<p>    return p;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528ctypes\u5b9a\u4e49\u4e00\u4e2a\u5bf9\u5e94\u7684\u7ed3\u6784\u4f53\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Point(ctypes.Structure):<\/p>\n<p>    _fields_ = [(&quot;x&quot;, ctypes.c_int), (&quot;y&quot;, ctypes.c_int)]<\/p>\n<h2><strong>\u52a0\u8f7d\u5e93<\/strong><\/h2>\n<p>lib = ctypes.CDLL(&#39;.\/complex.so&#39;)<\/p>\n<h2><strong>\u6307\u5b9acreate_point\u51fd\u6570\u7684\u8fd4\u56de\u503c\u7c7b\u578b<\/strong><\/h2>\n<p>lib.create_point.restype = Point<\/p>\n<h2><strong>\u8c03\u7528create_point\u51fd\u6570<\/strong><\/h2>\n<p>p = lib.create_point(10, 20)<\/p>\n<p>print(f&quot;Point: ({p.x}, {p.y})&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5904\u7406\u6307\u9488<\/h4>\n<\/p>\n<p><p>\u6307\u9488\u5728C\u8bed\u8a00\u4e2d\u975e\u5e38\u5e38\u89c1\uff0cctypes\u63d0\u4f9b\u4e86<code>POINTER<\/code>\u51fd\u6570\u6765\u5904\u7406\u6307\u9488\u7c7b\u578b\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u6574\u578b\u6307\u9488\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-c\">\/\/ pointer.c<\/p>\n<p>int* create_array(int size) {<\/p>\n<p>    int* array = (int*)malloc(size * sizeof(int));<\/p>\n<p>    for (int i = 0; i &lt; size; i++) {<\/p>\n<p>        array[i] = i;<\/p>\n<p>    }<\/p>\n<p>    return array;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u5904\u7406\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u52a0\u8f7d\u5e93<\/p>\n<p>lib = ctypes.CDLL(&#39;.\/pointer.so&#39;)<\/p>\n<h2><strong>\u6307\u5b9acreate_array\u51fd\u6570\u7684\u8fd4\u56de\u503c\u7c7b\u578b<\/strong><\/h2>\n<p>lib.create_array.restype = ctypes.POINTER(ctypes.c_int)<\/p>\n<h2><strong>\u8c03\u7528create_array\u51fd\u6570<\/strong><\/h2>\n<p>size = 5<\/p>\n<p>array_ptr = lib.create_array(size)<\/p>\n<h2><strong>\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20<\/strong><\/h2>\n<p>for i in range(size):<\/p>\n<p>    print(array_ptr[i])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<ol>\n<li><strong>\u6570\u636e\u7c7b\u578b\u5339\u914d<\/strong>\uff1a\u786e\u4fddctypes\u4e2d\u7684\u6570\u636e\u7c7b\u578b\u4e0eC\u4e2d\u7684\u6570\u636e\u7c7b\u578b\u5339\u914d\uff0c\u5426\u5219\u53ef\u80fd\u5bfc\u81f4\u4e0d\u53ef\u9884\u6599\u7684\u884c\u4e3a\u3002<\/li>\n<li><strong>\u5185\u5b58\u7ba1\u7406<\/strong>\uff1a\u4f7f\u7528\u6307\u9488\u65f6\u8981\u5c0f\u5fc3\u5185\u5b58\u7ba1\u7406\uff0c\u907f\u514d\u5185\u5b58\u6cc4\u6f0f\u3002<\/li>\n<li><strong>\u7ebf\u7a0b\u5b89\u5168<\/strong>\uff1a\u8c03\u7528\u975e\u7ebf\u7a0b\u5b89\u5168\u7684C\u5e93\u65f6\uff0c\u6ce8\u610fPython\u7684\u5168\u5c40\u89e3\u91ca\u5668\u9501\uff08GIL\uff09\u3002<\/li>\n<\/ol>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528ctypes\u5e93\u53ef\u4ee5\u65b9\u4fbf\u5730\u5728Python\u4e2d\u8c03\u7528C\u4ee3\u7801\uff0c\u4ece\u800c\u7ed3\u5408\u4e24\u8005\u7684\u4f18\u52bf\u3002ctypes\u9002\u5408\u9700\u8981\u8c03\u7528\u73b0\u6709C\u5e93\u7684\u573a\u666f\uff0c\u4e5f\u4e3a\u6027\u80fd\u8981\u6c42\u9ad8\u7684Python\u5e94\u7528\u63d0\u4f9b\u4e86\u4e00\u79cd\u4f18\u5316\u624b\u6bb5\u3002\u901a\u8fc7\u638c\u63e1ctypes\u7684\u57fa\u672c\u7528\u6cd5\u548c\u6ce8\u610f\u4e8b\u9879\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06C\u4ee3\u7801\u96c6\u6210\u5230Python\u9879\u76ee\u4e2d\uff0c\u5b9e\u73b0\u529f\u80fd\u548c\u6027\u80fd\u7684\u53cc\u8d62\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6267\u884cC\u4ee3\u7801\uff1f<\/strong><br \/>\u5728Python\u4e2d\u6267\u884cC\u4ee3\u7801\u901a\u5e38\u6709\u51e0\u79cd\u65b9\u5f0f\uff0c\u5305\u62ec\u4f7f\u7528Python\u7684C\u6269\u5c55\u6a21\u5757\u3001ctypes\u5e93\u548cCython\u7b49\u3002\u4f7f\u7528C\u6269\u5c55\u6a21\u5757\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2aPython\u6a21\u5757\uff0c\u76f4\u63a5\u5728Python\u4e2d\u8c03\u7528C\u51fd\u6570\uff1bctypes\u5219\u5141\u8bb8\u8c03\u7528\u52a8\u6001\u94fe\u63a5\u5e93\u4e2d\u7684C\u51fd\u6570\uff0c\u9002\u5408\u4e8e\u5feb\u901f\u96c6\u6210\uff1b\u800cCython\u5219\u662f\u4e00\u4e2a\u8ba9C\u548cPython\u4ee3\u7801\u53ef\u4ee5\u65e0\u7f1d\u7ed3\u5408\u7684\u5de5\u5177\uff0c\u9002\u5408\u4e8e\u9700\u8981\u5728Python\u4e2d\u7f16\u5199\u9ad8\u6027\u80fd\u4ee3\u7801\u7684\u573a\u666f\u3002<\/p>\n<p><strong>\u8c03\u7528C\u4ee3\u7801\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u7f16\u8bd1\u9009\u9879\uff1f<\/strong><br \/>\u5728\u7f16\u8bd1C\u4ee3\u7801\u65f6\uff0c\u786e\u4fdd\u4f7f\u7528\u5408\u9002\u7684\u7f16\u8bd1\u9009\u9879\u4ee5\u4fbf\u4e0ePython\u7684ABI\u517c\u5bb9\u3002\u901a\u5e38\u9700\u8981\u4f7f\u7528<code>-fPIC<\/code>\u9009\u9879\u6765\u751f\u6210\u4f4d\u7f6e\u65e0\u5173\u7684\u4ee3\u7801\uff0c\u5e76\u4f7f\u7528Python\u63d0\u4f9b\u7684<code>setup.py<\/code>\u811a\u672c\u4ee5\u4fbf\u4e8e\u7f16\u8bd1\u548c\u5b89\u88c5\u3002\u540c\u65f6\uff0c\u786e\u4fdd\u5728C\u4ee3\u7801\u4e2d\u5305\u542bPython.h\u5934\u6587\u4ef6\uff0c\u8fd9\u6837\u624d\u80fd\u4f7f\u7528Python\u7684API\u3002<\/p>\n<p><strong>Python\u4e0eC\u4e4b\u95f4\u7684\u6570\u636e\u4f20\u9012\u662f\u600e\u6837\u5b9e\u73b0\u7684\uff1f<\/strong><br \/>\u5728Python\u4e0eC\u4ee3\u7801\u7684\u4ea4\u4e92\u4e2d\uff0c\u6570\u636e\u4f20\u9012\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u65b9\u9762\u3002Python\u4e2d\u7684\u6570\u636e\u7c7b\u578b\u9700\u8981\u8f6c\u6362\u4e3aC\u6240\u80fd\u7406\u89e3\u7684\u7c7b\u578b\uff0c\u53cd\u4e4b\u4ea6\u7136\u3002\u4f8b\u5982\uff0cPython\u7684\u5b57\u7b26\u4e32\u53ef\u4ee5\u8f6c\u6362\u4e3aC\u7684\u5b57\u7b26\u6570\u7ec4\uff0c\u800cPython\u7684\u5217\u8868\u53ef\u4ee5\u901a\u8fc7\u6570\u7ec4\u4f20\u9012\u7ed9C\u51fd\u6570\u3002\u4f7f\u7528Python\u7684C API\u53ef\u4ee5\u5e2e\u52a9\u5904\u7406\u8fd9\u4e9b\u8f6c\u6362\uff0c\u786e\u4fdd\u6570\u636e\u5728\u4e24\u79cd\u8bed\u8a00\u4e4b\u95f4\u7684\u6b63\u786e\u4f20\u9012\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8c03\u7528C\u4ee3\u7801\u6709\u591a\u79cd\u65b9\u5f0f\uff0c\u4e3b\u8981\u5305\u62ec\uff1a\u4f7f\u7528ctypes\u5e93\u3001\u4f7f\u7528cffi\u5e93\u3001\u7f16\u5199Python\u6269\u5c55\u6a21\u5757\u3002 \u5176 [&hellip;]","protected":false},"author":3,"featured_media":975790,"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\/975778"}],"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=975778"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/975778\/revisions"}],"predecessor-version":[{"id":975795,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/975778\/revisions\/975795"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/975790"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=975778"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=975778"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=975778"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}