cython: 使用mingw编译器

admin 2019-5-4 6162

本文来源:https://www.jianshu。com/p/50105307dea5

--------------------------------

一、准备工作

  1. 安装python和cython。
  2. 安装mingw。注意python和mingw的位数必须一致。然后将mingw添加到Path环境变量。这里我使用的是64位python和mingw。如下图所示:
    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.dlldlltool -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.dlldlltool -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)。


==================================

在实际测试中发现报错如下:

xxxxxx.c:201:12: error: enumerator value for '__pyx_check_sizeof_voidp' is not an integer constant

     enum { __pyx_check_sizeof_voidp = 1 / (int)(SIZEOF_VOID_P == sizeof(void*)) };

改成

enum { __pyx_check_sizeof_voidp = 1 / 1};

编译通过




最新回复 (0)
返回