Skip to content

附录:在 Windows 上安装 MinGW

原文: http://docs.cython.org/en/latest/src/tutorial/appendix.html

  1. http://www.mingw.org/wiki/HOWTO_Install_the_MinGW_GCC_Compiler_Suite 下载 MinGW 安装程序。 (截至撰写本文时,下载链接有点难以找到;它位于左侧菜单中的“关于”下)。您需要名为“Automated MinGW Installer”(当前版本为 5.1.4)的文件。

  2. 运行它并安装 MinGW。 Cython 只需要基本的包,但是无论如何,你可能也应该安装 C ++编译器。

  3. 您需要设置 Windows 的“PATH”环境变量,以便包括例如“c:\ mingw \ bin”(如果您将 MinGW 安装到“c:\ mingw”)。以下 Web 页面描述了 Windows XP 中的过程(Vista 过程类似): https://support.microsoft.com/kb/310519

  4. 最后,告诉 Python 使用 MinGW 作为默认编译器(否则它将尝试 Visual C)。如果将 Python 安装到“c:\ Python27”,则创建一个名为“c:\ Python27 \ Lib \ distutils \ distutils.cfg”的文件,其中包含:

    ```config [build] compiler = mingw32

    ```

[WinInst] 页面包含了关于这一过程的更详细的流程;欢迎任何有助于使 Windows 安装过程更顺畅的贡献; 一个不幸的事实是,没有一个常规Cython开发人员可以方便地访问 Windows。

Python 3.8+

从Python 3.8版本以后,搜索动态链接库的地址被重设了。 更新日志

只有系统路径,包含DLL或者PYD文件的目录会在加载时期被用于搜索依赖。作为替代,一个新的函数 os.add_add_directory() 被加入用于提供额外的搜索路径。但是如此一个运行时的更新没法适用于 所有的场景。

和MSVC不同,MinGW有自己的特有的库,比如 libstdc++-6.dll -- 没有被放置在系统路径中(比如 c:\Windows\system32)。 一个C ++的例子是可以用MSVC的工具 dumpbin 来查找依赖:

> dumpbin /dependents my_gnu_extension.cp38-win_amd64.pyd
...
Dump of file my_gnu_extension.cp38-win_amd64.pyd

File Type: DLL

  Image has the following dependencies:

      python38.dll
      KERNEL32.dll
      msvcrt.dll
      libgcc_s_seh-1.dll
      libstdc++-6.dll
      ...

这些标准库可以用静态链接来内嵌,只需要给链接器增加下面的选项即可。

-static-libgcc -static-libstdc++ -Wl,-Bstatic,--whole-archive -lwinpthread -Wl,--no-whole-archive

setup.py 中,可以通过拓展 build_ext 类来加入一个跨平台的配置。

from setuptools import setup
from setuptools.command.build_ext import build_ext

link_args = ['-static-libgcc',
             '-static-libstdc++',
             '-Wl,-Bstatic,--whole-archive',
             '-lwinpthread',
             '-Wl,--no-whole-archive']

...  # Add extensions

class Build(build_ext):
    def build_extensions(self):
        if self.compiler.compiler_type == 'mingw32':
            for e in self.extensions:
                e.extra_link_args = link_args
        super(Build, self).build_extensions()

setup(
    ...
    cmdclass={'build_ext': Build},
    ...
)

我们一直在努力

apachecn/AiLearning

【布客】中文翻译组