{"id":10008244,"date":"2023-09-07T20:58:21","date_gmt":"2023-09-07T12:58:21","guid":{"rendered":"https:\/\/blog.itmax.cn\/?p=10008244"},"modified":"2023-09-07T20:58:21","modified_gmt":"2023-09-07T12:58:21","slug":"cython-%e7%bc%96%e8%af%91%e9%97%ae%e9%a2%98-%e5%92%8c-chatgpt-qa-%e6%97%a5%e5%bf%97","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10008244","title":{"rendered":"cython \u7f16\u8bd1\u95ee\u9898 \u548c chatGPT QA \u65e5\u5fd7"},"content":{"rendered":"<p>Q: <code>python setup.py build_ext --inplace --debug<\/code> \u4f1a\u5148\u751f\u6210 c \u6587\u4ef6\u5417\uff1f<\/p>\n<p>A: \u5728\u6267\u884c <code>python setup.py build_ext --inplace --debug<\/code> \u547d\u4ee4\u65f6\uff0c\u4f1a\u9996\u5148\u751f\u6210C\u6587\u4ef6\uff0c\u7136\u540e\u518d\u5c06\u5176\u7f16\u8bd1\u4e3a\u5171\u4eab\u5e93\uff08\u901a\u5e38\u662f\u6269\u5c55\u6a21\u5757\uff09\u3002\u4ee5\u4e0b\u662f\u8be5\u547d\u4ee4\u7684\u5de5\u4f5c\u6d41\u7a0b\uff1a<\/p>\n<ol>\n<li><strong>\u751f\u6210C\u6587\u4ef6<\/strong>\uff1a\u5f53\u60a8\u8fd0\u884c <code>python setup.py build_ext<\/code> \u65f6\uff0c\u5b83\u4f1a\u67e5\u627e\u9879\u76ee\u4e2d\u7684Cython\u6216C\u6269\u5c55\u6a21\u5757\uff0c\u7136\u540e\u4f7f\u7528Cython\u7f16\u8bd1\u5668\u5c06\u8fd9\u4e9b\u6269\u5c55\u6a21\u5757\u7684<code>.pyx<\/code>\u6e90\u6587\u4ef6\u8f6c\u6362\u4e3aC\u4ee3\u7801\uff08<code>.c<\/code>\u6587\u4ef6\uff09\u3002\u8fd9\u4e9b\u751f\u6210\u7684C\u6587\u4ef6\u5c06\u4f1a\u4f4d\u4e8e\u9879\u76ee\u7684\u6784\u5efa\u76ee\u5f55\u4e2d\u3002<\/li>\n<li><strong>\u7f16\u8bd1C\u4ee3\u7801<\/strong>\uff1a\u4e00\u65e6C\u4ee3\u7801\u751f\u6210\u5b8c\u6bd5\uff0c<code>build_ext<\/code> \u547d\u4ee4\u5c06\u4f7f\u7528C\u7f16\u8bd1\u5668\uff08\u901a\u5e38\u662fgcc\u6216\u7c7b\u4f3c\u5de5\u5177\uff09\u5c06C\u4ee3\u7801\u7f16\u8bd1\u4e3a\u5171\u4eab\u5e93\uff08\u6216\u6269\u5c55\u6a21\u5757\uff09\uff0c\u4ee5\u4fbf\u5b83\u53ef\u4ee5\u88abPython\u5bfc\u5165\u548c\u4f7f\u7528\u3002<\/li>\n<li><strong>\u751f\u6210\u5171\u4eab\u5e93<\/strong>\uff1a\u751f\u6210\u7684\u5171\u4eab\u5e93\u6587\u4ef6\uff08\u901a\u5e38\u662f<code>.so<\/code>\u3001<code>.dll<\/code>\u7b49\uff0c\u5177\u4f53\u53d6\u51b3\u4e8e\u64cd\u4f5c\u7cfb\u7edf\uff09\u4f1a\u653e\u7f6e\u5728\u4e0e\u6e90\u6587\u4ef6\u76f8\u540c\u7684\u76ee\u5f55\u4e2d\uff0c\u56e0\u4e3a\u60a8\u4f7f\u7528\u4e86 <code>--inplace<\/code> \u9009\u9879\u3002\u8fd9\u610f\u5473\u7740\u4e0d\u9700\u8981\u5c06\u5171\u4eab\u5e93\u590d\u5236\u5230\u5176\u4ed6\u5730\u65b9\uff0c\u60a8\u53ef\u4ee5\u76f4\u63a5\u4ece\u5f53\u524d\u76ee\u5f55\u4e2d\u5bfc\u5165\u548c\u4f7f\u7528\u5b83\u3002<\/li>\n<li><strong>\u8c03\u8bd5\u652f\u6301<\/strong>\uff1a\u5982\u679c\u60a8\u4f7f\u7528\u4e86 <code>--debug<\/code> \u9009\u9879\uff0c\u7f16\u8bd1\u8fc7\u7a0b\u4e2d\u4f1a\u5305\u62ec\u8c03\u8bd5\u4fe1\u606f\uff0c\u4ee5\u4fbf\u5728\u8c03\u8bd5Python\u4ee3\u7801\u65f6\u63d0\u4f9b\u66f4\u591a\u7684\u4fe1\u606f\u3002<\/li>\n<\/ol>\n<p>\u603b\u4e4b\uff0c<code>python setup.py build_ext --inplace --debug<\/code> \u547d\u4ee4\u4f1a\u5728\u6784\u5efa\u8fc7\u7a0b\u4e2d\u751f\u6210C\u6587\u4ef6\uff0c\u5e76\u5c06\u5b83\u4eec\u7f16\u8bd1\u4e3a\u5171\u4eab\u5e93\u6216\u6269\u5c55\u6a21\u5757\uff0c\u4ee5\u4fbf\u5728Python\u4e2d\u4f7f\u7528\u3002<\/p>\n<p>Q\uff1a\u5982\u4f55\u6307\u5b9a\u300c\u6307\u5b9a\u7684\u6784\u5efa\u76ee\u5f55\u300d<\/p>\n<p>A\uff1a\u8981\u6307\u5b9a\u6784\u5efa\u76ee\u5f55\uff0c\u60a8\u9700\u8981\u5728\u60a8\u7684\u9879\u76ee\u7684 <code>setup.py<\/code> \u6587\u4ef6\u6216\u8005 <code>pyproject.toml<\/code> \u6587\u4ef6\u4e2d\u8fdb\u884c\u914d\u7f6e\uff0c\u5177\u4f53\u53d6\u51b3\u4e8e\u60a8\u4f7f\u7528\u7684\u6784\u5efa\u5de5\u5177\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u5728\u4e24\u79cd\u60c5\u51b5\u4e0b\u6307\u5b9a\u6784\u5efa\u76ee\u5f55\u7684\u65b9\u6cd5\uff1a<\/p>\n<p><strong>\u5728 setup.py \u4e2d\u6307\u5b9a\u6784\u5efa\u76ee\u5f55\uff08\u4f7f\u7528 setuptools\uff09<\/strong>\uff1a<\/p>\n<p>\u5982\u679c\u60a8\u4f7f\u7528 setuptools \u6765\u6784\u5efa\u9879\u76ee\uff0c\u53ef\u4ee5\u5728\u9879\u76ee\u7684 <code>setup.py<\/code> \u6587\u4ef6\u4e2d\u6307\u5b9a\u6784\u5efa\u76ee\u5f55\u3002\u5728 <code>setup()<\/code> \u51fd\u6570\u4e2d\uff0c\u6dfb\u52a0 <code>ext_modules<\/code> \u53c2\u6570\u5e76\u4f7f\u7528 <code>cythonize()<\/code> \u51fd\u6570\u6765\u6307\u5b9a\u6784\u5efa\u76ee\u5f55\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre><code class=\"python\">from setuptools import setup\nfrom Cython.Build import cythonize\n\nsetup(\n    # ...\n    ext_modules = cythonize(&quot;your_module.pyx&quot;, build_dir=&quot;build&quot;),\n    # ...\n)<\/code><\/pre>\n<p>\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c<code>build_dir<\/code> \u53c2\u6570\u8bbe\u7f6e\u4e86\u6784\u5efa\u76ee\u5f55\u4e3a &#8220;build&#8221;\u3002\u60a8\u53ef\u4ee5\u5c06\u5176\u66ff\u6362\u4e3a\u60a8\u60f3\u8981\u7684\u4efb\u4f55\u76ee\u5f55\u8def\u5f84\u3002<\/p>\n<p><strong>\u5728 pyproject.toml \u4e2d\u6307\u5b9a\u6784\u5efa\u76ee\u5f55\uff08\u4f7f\u7528 Poetry \u6216 PEP 518\uff09<\/strong>\uff1a<\/p>\n<p>\u5982\u679c\u60a8\u4f7f\u7528 Poetry \u6216\u9075\u5faa PEP 518 \u7684\u9879\u76ee\u7ed3\u6784\uff0c\u901a\u5e38\u4f1a\u5728 <code>pyproject.toml<\/code> \u6587\u4ef6\u4e2d\u6307\u5b9a\u6784\u5efa\u76ee\u5f55\u3002\u7f16\u8f91 <code>pyproject.toml<\/code> \u6587\u4ef6\uff0c\u6dfb\u52a0\u4ee5\u4e0b\u914d\u7f6e\uff1a<\/p>\n<pre><code class=\"toml\">[build-system]\nrequires = [&quot;setuptools&quot;, &quot;wheel&quot;, &quot;cython&quot;]\nbuild-backend = &quot;setuptools.build_meta&quot;\n\n[tool.cython]\nbuild_dir = &quot;build&quot;<\/code><\/pre>\n<p>\u5728\u4e0a\u8ff0\u914d\u7f6e\u4e2d\uff0c<code>build_dir<\/code> \u53c2\u6570\u8bbe\u7f6e\u4e86\u6784\u5efa\u76ee\u5f55\u4e3a &#8220;build&#8221;\u3002\u60a8\u53ef\u4ee5\u6839\u636e\u9700\u8981\u5c06\u5176\u66ff\u6362\u4e3a\u60a8\u60f3\u8981\u7684\u76ee\u5f55\u8def\u5f84\u3002<\/p>\n<p>\u65e0\u8bba\u60a8\u9009\u62e9\u4f7f\u7528 setuptools \u8fd8\u662f Poetry\uff0c\u901a\u8fc7\u6307\u5b9a\u6784\u5efa\u76ee\u5f55\uff0c\u60a8\u53ef\u4ee5\u5c06\u751f\u6210\u7684C\u6587\u4ef6\u548c\u5171\u4eab\u5e93\u9694\u79bb\u5230\u6307\u5b9a\u7684\u4f4d\u7f6e\uff0c\u4ee5\u4fdd\u6301\u9879\u76ee\u7684\u7ec4\u7ec7\u7ed3\u6784\u5e72\u51c0\u3002<\/p>\n<hr>\n<p>\u6211\u5728\u7f16\u8bd1\u548c\u7814\u7a76 pyav\uff0c\u9047\u5230\u4e86\u4e00\u4e9b\u95ee\u9898<\/p>\n<p>\u6bd4\u5982\u7f16\u8bd1 pyav \u7684\u65f6\u5019\uff0c\u4e3a\u4ec0\u4e48\u4f1a\u751f\u6210 src \u6587\u4ef6\u5939\uff0c\u5e76\u4e14\u8fd9\u4e2a src \u6587\u4ef6\u5939\u4e0b\u9762\u90fd\u662f c \u6587\u4ef6<\/p>\n<p>\u4e0a\u9762\u7b2c\u4e00\u4e2a QA \u65e5\u5fd7\u56de\u7b54\u7b2c\u4e00\u4e2a\u95ee\u9898\uff0c\u56e0\u4e3a cython \u7f16\u8bd1\u4e3a so \u6587\u4ef6\u7684\uff0c\u4e2d\u95f4\u9700\u8981\u5148\u6574\u6210 c \u6587\u4ef6\u624d\u80fd\uff0c\u8fd9\u6837\u624d\u80fd\u4f7f\u7528 gcc \u7b49\u5de5\u5177\u6765\u505a\u8fd9\u4ef6\u4e8b\u60c5\uff0c\u6bd4\u8f83 gcc \u53ea\u8ba4\u8bc6 c \u6587\u4ef6\uff0c\u4e0d\u8ba4\u8bc6 pyx \u6587\u4ef6<\/p>\n<p>\u7b2c\u4e8c\u4e2a\u95ee\u9898\u300c\u4e3a\u4ec0\u4e48\u4f1a\u751f\u6210 src \u6587\u4ef6\u5939\u300d\uff0c\u751f\u6210 c \u6587\u4ef6\u9ed8\u8ba4\u662f\u5728\u5f53\u524d\u8def\u5f84\u7684\uff0c\u4f46\u662f\u7f16\u8bd1 pyav \u7684\u65f6\u5019\uff0c\u4f1a\u7edf\u4e00\u5230 src \u6587\u4ef6\u5939\u4e2d\u53bb\uff0c\u7814\u7a76\u4e86\u4e00\u4e0b\uff0c\u6b63\u5982 chatGPT \u6240\u8bf4\uff0c\u662f\u56e0\u4e3a pyav \u6dfb\u52a0\u4e86 <code>build_dir=&quot;src&quot;<\/code><\/p>\n<pre><code class=\"python\"># Construct the modules that we find in the &quot;av&quot; directory.\next_modules = []\nfor dirname, dirnames, filenames in os.walk(&quot;av&quot;):\n    for filename in filenames:\n        # We are looking for Cython sources.\n        if filename.startswith(&quot;.&quot;) or os.path.splitext(filename)[1] != &quot;.pyx&quot;:\n            continue\n\n        pyx_path = os.path.join(dirname, filename)\n        base = os.path.splitext(pyx_path)[0]\n\n        # Need to be a little careful because Windows will accept \/ or \/\n        # (where os.sep will be \/ on Windows).\n        mod_name = base.replace(&quot;\/&quot;, &quot;.&quot;).replace(os.sep, &quot;.&quot;)\n\n        # Cythonize the module.\n        ext_modules += cythonize(\n            Extension(\n                mod_name,\n                include_dirs=extension_extra[&quot;include_dirs&quot;],\n                libraries=extension_extra[&quot;libraries&quot;],\n                library_dirs=extension_extra[&quot;library_dirs&quot;],\n                sources=[pyx_path],\n            ),\n            compiler_directives=dict(\n                c_string_type=&quot;str&quot;,\n                c_string_encoding=&quot;ascii&quot;,\n                embedsignature=True,\n                language_level=2,\n            ),\n            build_dir=&quot;src&quot;,\n            include_path=[&quot;include&quot;],\n        )<\/code><\/pre>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000044191109\">https:\/\/segmentfault.com\/a\/1190000044191109<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Q: python setup.py build_ext &#8211;inplace &#8211;debug \u4f1a\u5148\u751f\u6210 c \u6587 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10008244","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10008244","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10008244"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10008244\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10008244"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10008244"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10008244"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}