cython: 使用mingw编译器

本文主要介绍在python和cython时,如何配置使用mingw编译器。

一、准备工作

  1. 安装python和cython。
  2. 安装mingw。注意python和mingw的位数必须一致。然后将mingw添加到Path环境变量。这里我使用的是64位python和mingw。如下图所示:
    6141703-7750989a18fcc485.png
    image.png

二、添加distutils.cfg文件

在python安装目录C:\Program Files\Python\Lib\distutils中新建distutils.cfg文件,内容如下:

[build]
compiler=mingw32

[build_ext]
compiler=mingw32

注:如果目录已经存在该文件,修改成上述内容即可。

三、修改cygwinccompiler.py文件

进入python安装目录C:\Program Files\Python\Lib\distutils中,修改cygwinccompiler.py文件,添加以下内容到get_msvcr()函数(用于解决ValueError: Unknown MS Compiler version 1900错误)。

elif msc_ver == '1900':
    # Visual Studio 2015 / Visual C++ 14.0
    # "msvcr140.dll no longer exists"
    return ['vcruntime140']

修改后的内容为:

def get_msvcr():
    """Include the appropriate MSVC runtime library if Python was built
    with MSVC 7.0 or later.
    """
    msc_pos = sys.version.find('MSC v.')
    if msc_pos != -1:
        msc_ver = sys.version[msc_pos + 6:msc_pos + 10]
        if msc_ver == '1300':
            # MSVC 7.0
            return ['msvcr70']
        elif msc_ver == '1310':
            # MSVC 7.1
            return ['msvcr71']
        elif msc_ver == '1400':
            # VS2005 / MSVC 8.0
            return ['msvcr80']
        elif msc_ver == '1500':
            # VS2008 / MSVC 9.0
            return ['msvcr90']
        elif msc_ver == '1600':
            # VS2010 / MSVC 10.0
            return ['msvcr100']
        elif msc_ver == '1900':
            # Visual Studio 2015 / Visual C++ 14.0
            # "msvcr140.dll no longer exists" 
            return ['vcruntime140']
        else:
            raise ValueError("Unknown MS Compiler version %s " % msc_ver)

四、转换python36.dllvcruntime140.dll文件

转换python36.dll文件
用于解决C:\Program Files\Python\libs/libpython36.a: error adding symbols: File format not recognized错误。

  1. 复制C:\Program Files\Python\python36.dll到桌面,执行以下命令:
gendef python36.dll
dlltool -D python36.dll -d python36.def -l libpython36.a
  1. 备份C:\Program Files\Python\libs/libpython36.a文件,将上步生成的libpython36.a复制到C:\Program Files\Python\libs目录下。

转换vcruntime140.dll文件
用于解决C:\Program Files\Python / vcruntime140.dll: file not recognized: File format not recognized错误。

  1. 复制C:\Program Files\Python\vcruntime140.dll到桌面,执行以下命令:
gendef vcruntime140.dll
dlltool -D vcruntime140.dll -d vcruntime140.def -l libvcruntime140.a

2.将生成的libvcruntime140.a复制到C:\ProgramFiles\Python\libs目录即可。

本次测试环境:WIN10(64) + Python 3.6.4(64) + MinGW7.2.0 (x86_64-posix-seh-rev0)。

版权声明:本文为「txfly」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://www.jianshu.com/p/50105307dea5

  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值