{"id":175208,"date":"2024-05-08T18:47:03","date_gmt":"2024-05-08T10:47:03","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/175208.html"},"modified":"2024-05-08T18:47:08","modified_gmt":"2024-05-08T10:47:08","slug":"%e5%a6%82%e4%bd%95%e5%b0%86c-%e7%9a%84api%e5%b0%81%e8%a3%85%e6%88%90python%e5%8f%af%e8%b0%83%e7%94%a8%e5%bd%a2%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/175208.html","title":{"rendered":"\u5982\u4f55\u5c06C  \u7684API\u5c01\u88c5\u6210python\u53ef\u8c03\u7528\u5f62\u5f0f"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27052750\/51923cd7-8b9a-4c0c-b194-df934f43da32.webp\" alt=\"\u5982\u4f55\u5c06C  \u7684API\u5c01\u88c5\u6210python\u53ef\u8c03\u7528\u5f62\u5f0f\" \/><\/p>\n<p><p>C\u8bed\u8a00\u7684API\u56e0\u5176\u6267\u884c\u6548\u7387\u9ad8\u548c\u8ddd\u79bb\u786c\u4ef6\u5e95\u5c42\u8fd1\u800c\u5e7f\u53d7\u9752\u7750\uff0c\u800cPython\u4ee5\u7b80\u6d01\u6613\u5b66\u8457\u79f0\u3002\u5c06C\u7684API\u5c01\u88c5\u6210Python\u53ef\u8c03\u7528\u7684\u5f62\u5f0f\u53ef\u4ee5\u7ed3\u5408\u4e24\u8005\u7684\u4f18\u70b9\uff1a<strong>\u9ad8\u6548\u7684\u6267\u884c\u901f\u5ea6<\/strong>\u3001<strong>\u7b80\u6d01\u7684\u8c03\u7528\u63a5\u53e3<\/strong>\u3002\u5c01\u88c5C\u7684API\u5230Python\uff0c\u6700\u5e38\u89c1\u7684\u65b9\u5f0f\u662f\u901a\u8fc7\u521b\u5efaPython\u6269\u5c55\u6a21\u5757\uff0c\u901a\u5e38\u6d89\u53ca<strong>Cython<\/strong>\u3001<strong>ctypes<\/strong>\u548c<strong>C\u6269\u5c55\u63a5\u53e3\uff08Python.h\uff09<strong>\u4e09\u79cd\u6280\u672f\u3002<strong>Cython<\/strong>\u662f\u4e00\u4e2a\u7f16\u8bd1\u5668\uff0c\u5b83\u53ef\u4ee5\u5c06Python\u4ee3\u7801\u548cC\u4ee3\u7801\u6df7\u5408\u7f16\u5199\u7684Cython\u8bed\u8a00\u7f16\u8bd1\u6210C\uff0c\u8fdb\u800c\u7f16\u8bd1\u6210Python\u6a21\u5757\u3002<strong>ctypes<\/strong>\u662fPython\u7684\u4e00\u4e2a\u6807\u51c6\u5e93\u6a21\u5757\uff0c\u5b83\u5141\u8bb8\u8c03\u7528\u52a8\u6001\u94fe\u63a5\u5e93(DLLs)\u6216\u8005\u5171\u4eab\u5e93\u4e2d\u7684\u51fd\u6570\u3002\u800c\u4f7f\u7528<\/strong>C\u6269\u5c55\u63a5\u53e3<\/strong>\u5219\u6d89\u53ca\u76f4\u63a5\u7528C\u8bed\u8a00\u7f16\u5199Python\u6a21\u5757\uff0c\u8fd9\u901a\u5e38\u9700\u8981\u4e00\u4e2a\u8f83\u4e3a\u6df1\u5165\u7684\u5bf9C\u8bed\u8a00\u548cPython C API\u7684\u7406\u89e3\u3002\u4ee5\u4e0b\uff0c\u6211\u4eec\u5c06\u91cd\u70b9\u4ecb\u7ecd\u5982\u4f55\u901a\u8fc7C\u6269\u5c55\u63a5\u53e3\u7684\u65b9\u5f0f\u5c06C\u7684API\u5c01\u88c5\u6210Python\u53ef\u8c03\u7528\u7684\u5f62\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u51c6\u5907C\u8bed\u8a00API<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u5c01\u88c5\u4e4b\u524d\uff0c\u9700\u8981\u5148\u521b\u5efa\u6216\u51c6\u5907\u8981\u5c01\u88c5\u7684C\u8bed\u8a00API\u3002\u8fd9\u4e9bAPI\u53ef\u80fd\u662f\u4e00\u4e9b\u5df2\u7ecf\u5b58\u5728\u7684\u5e93\u51fd\u6570\uff0c\u6216\u8005\u662f\u4e13\u95e8\u4e3a\u4e86\u96c6\u6210\u5230Python\u4e2d\u800c\u7f16\u5199\u7684\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h4>\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684C\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u7f16\u5199\u4e00\u4e2aC\u51fd\u6570\uff0c\u4f9b\u540e\u7eed\u5c01\u88c5\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u51fd\u6570\uff0c\u7528\u4e8e\u8ba1\u7b97\u4e24\u4e2a\u6574\u6570\u7684\u548c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-c\">int add(int a, int b) {<\/p>\n<p>    return a + b;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u786e\u4fdd\u53ef\u91cd\u7528\u6027<\/h4>\n<\/p>\n<p><p>\u8981\u5c01\u88c5\u7684C\u51fd\u6570\u5e94\u5f53\u662f\u53ef\u91cd\u7528\u7684\uff0c\u610f\u5473\u7740\u5176\u5b9a\u4e49\u5e94\u5f53\u5728\u9002\u5f53\u7684\u5934\u6587\u4ef6\u4e2d\u58f0\u660e\uff0c\u5e76\u4e14\u51fd\u6570\u4f53\u5728\u5355\u72ec\u7684C\u6587\u4ef6\u4e2d\u5b9e\u73b0\u3002\u8fd9\u53ef\u4ee5\u4fdd\u8bc1\u5728\u5c01\u88c5\u8fc7\u7a0b\u4e2d\u7684\u6a21\u5757\u5316\u548c\u7ec4\u7ec7\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u521b\u5efaPython\u5305\u88c5\u5668<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u4f7f\u5f97C API\u53ef\u4ee5\u5728Python\u4e2d\u88ab\u8c03\u7528\uff0c\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u5bf9\u5e94\u7684Python\u5305\u88c5\u5668\uff08wrapper\uff09\u3002\u8fd9\u4e2a\u5305\u88c5\u5668\u5b9e\u8d28\u4e0a\u662f\u4e00\u4e2aPython\u6a21\u5757\uff0c\u5b83\u5bf9C\u8bed\u8a00\u7684API\u8fdb\u884c\u4e86\u5c01\u88c5\uff0c\u4f7f\u5176\u53ef\u4ee5\u50cf\u666e\u901a\u7684Python\u51fd\u6570\u4e00\u6837\u88ab\u8c03\u7528\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528Python C API<\/h4>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86C API\u6765\u5e2e\u52a9\u7528\u6237\u521b\u5efa\u81ea\u5df1\u7684\u6269\u5c55\u6a21\u5757\u3002\u8fd9\u9700\u8981\u5305\u62ecPython.h\u5934\u6587\u4ef6\uff0c\u5e76\u6309\u7167\u4e00\u5b9a\u7684\u683c\u5f0f\u7f16\u5199\u5c01\u88c5\u4ee3\u7801\u3002\u9996\u5148\uff0c\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u8868\u793aPython\u6a21\u5757\u7684<code>PyModuleDef<\/code>\u7ed3\u6784\u4f53\uff0c\u5e76\u5728\u8fd9\u4e2a\u7ed3\u6784\u4f53\u4e2d\u6ce8\u518c\u8981\u66b4\u9732\u7ed9Python\u7684\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h4>\u7f16\u5199\u5c01\u88c5\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>\u6bcf\u4e2aC\u51fd\u6570\u90fd\u9700\u8981\u6709\u4e00\u4e2a\u5bf9\u5e94\u7684Python\u5c01\u88c5\u65b9\u6cd5\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u63a5\u53d7<code>PyObject<\/code>\u4e3a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de<code>PyObject<\/code>\u4f5c\u4e3a\u7ed3\u679c\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u4e0a\u9762\u7684<code>add<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u521b\u5efa\u5982\u4e0b\u7684\u5c01\u88c5\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-c\">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>    int result = add(a, b);<\/p>\n<p>    return PyLong_FromLong(result);<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u521b\u5efa\u6a21\u5757\u521d\u59cb\u5316\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u6bcf\u4e2aPython\u6269\u5c55\u6a21\u5757\u90fd\u5fc5\u987b\u6709\u4e00\u4e2a\u521d\u59cb\u5316\u51fd\u6570\u3002\u8fd9\u4e2a\u51fd\u6570\u7684\u540d\u79f0\u7531\u6a21\u5757\u540d\u5e76\u5728\u524d\u9762\u52a0\u4e0a<code>PyInit_<\/code>\u6784\u6210\u3002\u8be5\u51fd\u6570\u4f7f\u7528\u4e0a\u9762\u5b9a\u4e49\u7684<code>PyModuleDef<\/code>\u7ed3\u6784\u4f53\uff0c\u5e76\u901a\u8fc7<code>PyModule_Create<\/code>\u51fd\u6570\u521b\u5efa\u6a21\u5757\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><h4>\u8bbe\u7f6e\u6a21\u5757\u65b9\u6cd5\u8868<\/h4>\n<\/p>\n<p><p>\u6a21\u5757\u7684\u65b9\u6cd5\u8868\u662f\u4e00\u4e2a<code>PyMethodDef<\/code>\u7c7b\u578b\u7684\u6570\u7ec4\uff0c\u5176\u4e2d\u5217\u51fa\u4e86\u6240\u6709\u5728Python\u4e2d\u80fd\u591f\u88ab\u8c03\u7528\u7684\u65b9\u6cd5\uff08\u5373C\u51fd\u6570\u7684\u5305\u88c5\u5668\uff09\u3002\u6bcf\u4e2a\u6761\u76ee\u9700\u8981\u5305\u62ecPython\u4e2d\u7684\u65b9\u6cd5\u540d\u3001C\u7684\u5c01\u88c5\u65b9\u6cd5\u3001\u65b9\u6cd5\u7684\u53c2\u6570\u7c7b\u578b\u548c\u65b9\u6cd5\u7684\u6587\u6863\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h4>\u5b9e\u73b0\u521d\u59cb\u5316\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u521d\u59cb\u5316\u51fd\u6570\u7684\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-c\">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><h3>\u56db\u3001\u7f16\u8bd1\u6269\u5c55\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u5c06C\u8bed\u8a00\u7684API\u5c01\u88c5\u6210Python\u6a21\u5757\u4e4b\u540e\uff0c\u9700\u8981\u5bf9\u5176\u8fdb\u884c\u7f16\u8bd1\uff0c\u4f7f\u5176\u6210\u4e3aPython\u53ef\u5bfc\u5165\u7684\u6a21\u5757\u3002\u901a\u5e38\u4f7f\u7528Python\u7684<code>distutils<\/code>\u6216\u8005<code>setuptools<\/code>\u5305\u53ef\u4ee5\u5e2e\u52a9\u5b8c\u6210\u8fd9\u4e00\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><h4>\u7f16\u5199setup.py<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a<code>setup.py<\/code>\u6587\u4ef6\uff0c\u4f7f\u7528<code>setuptools<\/code>\u914d\u7f6e\u7f16\u8bd1\u9009\u9879\u3002\u5176\u4e2d\uff0c\u9700\u8981\u6307\u5b9a\u6a21\u5757\u540d\u3001\u6e90\u4ee3\u7801\u6587\u4ef6\u4ee5\u53ca\u5176\u4ed6\u53ef\u80fd\u9700\u8981\u7684\u7f16\u8bd1\u9009\u9879\u3002<\/p>\n<\/p>\n<p><h4>\u8fd0\u884c\u6784\u5efa\u547d\u4ee4<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Python\u63d0\u4f9b\u7684<code>setup.py<\/code>\u811a\u672c\u6765\u8fd0\u884c\u6784\u5efa\u547d\u4ee4\u3002\u4e00\u6761\u6807\u51c6\u7684\u7f16\u8bd1\u6307\u4ee4\u662f\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><h3>\u4e94\u3001\u6d4b\u8bd5\u5c01\u88c5\u7684\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u5728\u7f16\u8bd1\u751f\u6210\u6269\u5c55\u6a21\u5757\u4e4b\u540e\uff0c\u9700\u8981\u8fdb\u884c\u6d4b\u8bd5\u4ee5\u786e\u4fdd\u5b83\u6309\u7167\u671f\u671b\u5de5\u4f5c\u3002\u53ef\u4ee5\u521b\u5efaPython\u811a\u672c\u6216\u76f4\u63a5\u5728Python\u63a7\u5236\u53f0\u4e2d\u5bfc\u5165\u7f16\u8bd1\u540e\u7684\u6a21\u5757\uff0c\u8c03\u7528\u76f8\u5e94\u7684\u51fd\u6570\u8fdb\u884c\u9a8c\u8bc1\u3002<\/p>\n<\/p>\n<p><h4>\u7f16\u5199\u6d4b\u8bd5\u7528\u4f8b<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u6d4b\u8bd5\u7528\u4f8b\uff0c\u8c03\u7528\u6269\u5c55\u6a21\u5757\u4e2d\u7684\u51fd\u6570\uff0c\u68c0\u67e5\u8f93\u51fa\u7ed3\u679c\u662f\u5426\u7b26\u5408\u9884\u671f\u3002\u8fd9\u4e0d\u4ec5\u80fd\u9a8c\u8bc1\u51fd\u6570\u7684\u529f\u80fd\uff0c\u8fd8\u80fd\u786e\u4fdd\u5c01\u88c5\u8fc7\u7a0b\u4e2d\u7684\u53c2\u6570\u4f20\u9012\u7b49\u7ec6\u8282\u662f\u6b63\u786e\u7684\u3002<\/p>\n<\/p>\n<p><h4>\u8fdb\u884c\u6d4b\u8bd5<\/h4>\n<\/p>\n<p><p>\u8fdb\u884c\u6d4b\u8bd5\uff0c\u5e76\u786e\u4fdd\u6240\u6709\u6d4b\u8bd5\u7528\u4f8b\u90fd\u80fd\u6b63\u5e38\u901a\u8fc7\u3002\u5982\u679c\u51fa\u73b0\u4efb\u4f55\u9519\u8bef\uff0c\u9700\u8981\u56de\u5230\u4e4b\u524d\u7684\u6b65\u9aa4\u4e2d\u68c0\u67e5\u5e76\u4fee\u6b63\u5c01\u88c5\u7684\u4ee3\u7801\u548c\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u6587\u6863\u548c\u7ef4\u62a4<\/h3>\n<\/p>\n<p><p>\u7f16\u5199\u6269\u5c55\u6a21\u5757\u7684\u6587\u6863\u662f\u4e00\u4e2a\u91cd\u8981\u6b65\u9aa4\uff0c\u56e0\u4e3a\u5b83\u6307\u5bfc\u7528\u6237\u5982\u4f55\u6b63\u786e\u5730\u4f7f\u7528\u4f60\u7684\u6a21\u5757\u3002\u540c\u65f6\uff0c\u7ef4\u62a4\u548c\u66f4\u65b0\u4f60\u7684\u6269\u5c55\u6a21\u5757\u4e5f\u662f\u5fc5\u8981\u7684\uff0c\u4ee5\u4fdd\u6301\u4e0ePython\u65b0\u7248\u672c\u7684\u517c\u5bb9\u6027\uff0c\u5e76\u4fee\u590d\u53ef\u80fd\u51fa\u73b0\u7684bug\u3002<\/p>\n<\/p>\n<p><h4>\u7f16\u5199\u6587\u6863<\/h4>\n<\/p>\n<p><p>\u7f16\u5199\u6e05\u6670\u3001\u5b8c\u6574\u7684API\u6587\u6863\uff0c\u5e76\u63d0\u4f9b\u8db3\u591f\u7684\u793a\u4f8b\uff0c\u4ee5\u5e2e\u52a9\u7528\u6237\u7406\u89e3\u5982\u4f55\u4f7f\u7528\u4f60\u7684\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><h4>\u6301\u7eed\u7ef4\u62a4<\/h4>\n<\/p>\n<p><p>\u968f\u7740\u65f6\u95f4\u7684\u63a8\u79fb\uff0c\u4e0d\u65ad\u66f4\u65b0\u548c\u7ef4\u62a4\u6a21\u5757\uff0c\u4fee\u590dbug\uff0c\u63d0\u5347\u6027\u80fd\uff0c\u5e76\u4fdd\u6301\u4e0ePython\u65b0\u7248\u672c\u7684\u517c\u5bb9\u6027\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u4f60\u53ef\u4ee5\u5c06C\u7684API\u5c01\u88c5\u6210Python\u53ef\u8c03\u7528\u7684\u5f62\u5f0f\u3002\u8fd9\u79cd\u5c01\u88c5\u65b9\u6cd5\u65e2\u53ef\u4ee5\u5145\u5206\u5229\u7528C\u8bed\u8a00\u5904\u7406\u6027\u80fd\u654f\u611f\u4efb\u52a1\u7684\u80fd\u529b\uff0c\u4e5f\u53ef\u4ee5\u4eab\u6709Python\u6613\u7528\u6027\u548c\u5e7f\u6cdb\u7684\u751f\u6001\u7cfb\u7edf\u3002\u5b9e\u65bd\u8fd9\u4e9b\u6b65\u9aa4\uff0c\u4f60\u53ef\u4ee5\u521b\u5efa\u5f3a\u5927\u800c\u9ad8\u6548\u7684Python\u5de5\u5177\uff0c\u589e\u5f3a\u4f60\u7684\u5e94\u7528\u7a0b\u5e8f\u6216\u5f00\u53d1\u65b0\u7684\u8f6f\u4ef6\u5e93\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5982\u4f55\u5728Python\u4e2d\u5c01\u88c5C\u7684API\uff1f<\/strong><\/p>\n<p>\u5c01\u88c5C\u7684API\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u9700\u6c42\uff0c\u56e0\u4e3aC\u4ee3\u7801\u901a\u5e38\u6bd4\u8f83\u9ad8\u6548\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u6b65\u9aa4\u6765\u5c06C\u7684API\u5c01\u88c5\u6210Python\u53ef\u8c03\u7528\u5f62\u5f0f\uff1a<\/p>\n<ul>\n<li>\n<p><strong>\u7f16\u5199C\u7684API\u4ee3\u7801<\/strong>\uff1a\u9996\u5148\uff0c\u4f60\u9700\u8981\u7f16\u5199C\u7684API\u4ee3\u7801\uff0c\u8be5\u4ee3\u7801\u5b9a\u4e49\u4e86\u4f60\u5e0c\u671b\u4ecePython\u4e2d\u8c03\u7528\u7684\u51fd\u6570\u548c\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528Cython\u6216ctypes\u6765\u5c01\u88c5API<\/strong>\uff1a\u5728Python\u4e2d\uff0c\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u7528\u6765\u5c01\u88c5C\u7684API\u3002\u4e00\u79cd\u5e38\u89c1\u7684\u9009\u62e9\u662f\u4f7f\u7528Cython\u6216ctypes\u3002Cython\u662f\u4e00\u79cdPython\u548cC\u6df7\u5408\u7f16\u7a0b\u8bed\u8a00\uff0c\u5b83\u53ef\u4ee5\u5c06C\u4ee3\u7801\u76f4\u63a5\u8f6c\u6362\u6210Python\u6269\u5c55\u6a21\u5757\u3002\u53e6\u4e00\u65b9\u9762\uff0cctypes\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u6a21\u5757\uff0c\u5b83\u5141\u8bb8\u4f60\u8c03\u7528\u5305\u542b\u5728\u5171\u4eab\u5e93\u6216\u52a8\u6001\u94fe\u63a5\u5e93\u4e2d\u7684C\u51fd\u6570\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u7f16\u5199Python\u5305\u88c5\u5668\u51fd\u6570<\/strong>\uff1a\u4e00\u65e6\u4f60\u5c06C\u7684API\u5c01\u88c5\u5728Python\u6269\u5c55\u6a21\u5757\u4e2d\uff0c\u4f60\u53ef\u4ee5\u7f16\u5199Python\u5305\u88c5\u5668\u51fd\u6570\uff0c\u8fd9\u4e9b\u51fd\u6570\u5c06\u8c03\u7528C\u51fd\u6570\u5e76\u5904\u7406\u8fd4\u56de\u7ed3\u679c\u3002\u8fd9\u4e9b\u5305\u88c5\u5668\u51fd\u6570\u53ef\u4ee5\u5c06C\u7684API\u5c01\u88c5\u6210\u66f4Pythonic\u7684\u5f62\u5f0f\uff0c\u4f7f\u5176\u66f4\u5bb9\u6613\u4f7f\u7528\u548c\u7406\u89e3\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u6784\u5efa\u548c\u5b89\u88c5\u5c01\u88c5\u7684\u6a21\u5757<\/strong>\uff1a\u6700\u540e\uff0c\u4f60\u9700\u8981\u6784\u5efa\u548c\u5b89\u88c5\u5c01\u88c5\u7684\u6a21\u5757\uff0c\u4ee5\u4f7f\u5176\u53ef\u4ee5\u88ab\u5176\u4ed6Python\u4ee3\u7801\u5f15\u7528\u548c\u8c03\u7528\u3002\u8fd9\u901a\u5e38\u6d89\u53ca\u4f7f\u7528\u9002\u5f53\u7684\u6784\u5efa\u7cfb\u7edf\uff08\u5982setup.py\uff09\u548c\u5de5\u5177\uff08\u5982pip\uff09\u6765\u5b89\u88c5\u6269\u5c55\u6a21\u5757\u3002<\/p>\n<\/li>\n<\/ul>\n<p><strong>2. \u5728Python\u4e2d\u5982\u4f55\u8c03\u7528\u5df2\u5c01\u88c5\u7684C API\uff1f<\/strong><\/p>\n<p>\u4e00\u65e6\u4f60\u5c06C\u7684API\u5c01\u88c5\u6210\u4e86Python\u53ef\u8c03\u7528\u5f62\u5f0f\uff0c\u4f60\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u5728Python\u4e2d\u8c03\u7528\u5b83\uff1a<\/p>\n<ul>\n<li>\n<p><strong>\u5bfc\u5165\u5c01\u88c5\u7684\u6a21\u5757<\/strong>\uff1a\u9996\u5148\uff0c\u4f60\u9700\u8981\u5bfc\u5165\u5c01\u88c5\u7684\u6a21\u5757\uff0c\u4ee5\u4fbf\u53ef\u4ee5\u8bbf\u95ee\u5176\u4e2d\u5b9a\u4e49\u7684\u51fd\u6570\u548c\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u8c03\u7528C\u51fd\u6570<\/strong>\uff1a\u4f7f\u7528\u5c01\u88c5\u6a21\u5757\u4e2d\u7684\u51fd\u6570\u8c03\u7528C\u7684API\u3002\u4f60\u53ef\u4ee5\u4f20\u9012\u53c2\u6570\u7ed9\u8fd9\u4e9b\u51fd\u6570\uff0c\u5e76\u5904\u7406\u5b83\u4eec\u7684\u8fd4\u56de\u503c\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u5904\u7406\u8fd4\u56de\u7ed3\u679c<\/strong>\uff1aC\u7684API\u51fd\u6570\u53ef\u80fd\u8fd4\u56deC\u7c7b\u578b\u7684\u6570\u636e\uff0c\u5982\u6574\u6570\u3001\u6d6e\u70b9\u6570\u3001\u5b57\u7b26\u4e32\u7b49\u3002\u5728Python\u4e2d\uff0c\u4f60\u53ef\u4ee5\u6839\u636e\u9700\u8981\u5c06\u8fd9\u4e9b\u6570\u636e\u8f6c\u6362\u4e3aPython\u7684\u76f8\u5e94\u7c7b\u578b\u3002<\/p>\n<\/li>\n<\/ul>\n<p><strong>3. \u6709\u6ca1\u6709\u5176\u4ed6\u53ef\u4f9b\u9009\u62e9\u7684\u65b9\u6cd5\u6765\u5c01\u88c5C\u7684API\uff1f<\/strong><\/p>\n<p>\u9664\u4e86Cython\u548cctypes\u4e4b\u5916\uff0c\u8fd8\u6709\u4e00\u4e9b\u5176\u4ed6\u53ef\u4f9b\u9009\u62e9\u7684\u65b9\u6cd5\u6765\u5c01\u88c5C\u7684API\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u9009\u62e9\uff1a<\/p>\n<ul>\n<li>\n<p><strong>SWIG<\/strong>\uff1aSWIG\u662f\u4e00\u4e2a\u7528\u4e8e\u8fde\u63a5C\u548c\u5176\u4ed6\u9ad8\u7ea7\u7f16\u7a0b\u8bed\u8a00\uff08\u5982Python\uff09\u7684\u5de5\u5177\u3002\u5b83\u53ef\u4ee5\u81ea\u52a8\u751f\u6210\u7528\u4e8e\u8c03\u7528C\u4ee3\u7801\u7684\u5305\u88c5\u5668\u4ee3\u7801\u3002<\/p>\n<\/li>\n<li>\n<p><strong>CFFI<\/strong>\uff1aCFFI\u662f\u4e00\u4e2a\u7528\u4e8e\u8c03\u7528C\u4ee3\u7801\u7684\u8f7b\u91cf\u7ea7\u63a5\u53e3\u3002\u5b83\u5141\u8bb8\u4f60\u5c06C\u7684API\u5c01\u88c5\u6210Python\u53ef\u8c03\u7528\u7684\u5f62\u5f0f\uff0c\u800c\u65e0\u9700\u7f16\u5199\u5927\u91cf\u7684\u5305\u88c5\u5668\u4ee3\u7801\u3002<\/p>\n<\/li>\n<li>\n<p><strong>Boost.Python<\/strong>\uff1aBoost.Python\u662f\u4e00\u4e2a\u4e0eC++\u66f4\u5bc6\u5207\u76f8\u5173\u7684\u5e93\uff0c\u4f46\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5c01\u88c5C\u4ee3\u7801\u3002\u5b83\u63d0\u4f9b\u4e86\u8bb8\u591a\u5de5\u5177\u548c\u673a\u5236\u6765\u5c06C\u4ee3\u7801\u5c01\u88c5\u4e3aPython\u4ee3\u7801\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u7279\u70b9\uff0c\u53ef\u4ee5\u6839\u636e\u4f60\u7684\u9700\u6c42\u548c\u504f\u597d\u8fdb\u884c\u9009\u62e9\u3002\u5728\u9009\u62e9\u65f6\uff0c\u53ef\u4ee5\u8003\u8651\u5b83\u4eec\u7684\u6613\u7528\u6027\u3001\u6027\u80fd\u3001\u517c\u5bb9\u6027\u548c\u7ef4\u62a4\u6210\u672c\u7b49\u56e0\u7d20\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"C\u8bed\u8a00\u7684API\u56e0\u5176\u6267\u884c\u6548\u7387\u9ad8\u548c\u8ddd\u79bb\u786c\u4ef6\u5e95\u5c42\u8fd1\u800c\u5e7f\u53d7\u9752\u7750\uff0c\u800cPython\u4ee5\u7b80\u6d01\u6613\u5b66\u8457\u79f0\u3002\u5c06C\u7684API\u5c01\u88c5\u6210Pyt [&hellip;]","protected":false},"author":3,"featured_media":175215,"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\/175208"}],"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=175208"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/175208\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/175215"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=175208"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=175208"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=175208"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}