本文主要介紹在python和cython時(shí),如何配置使用mingw編譯器。
一耸采、準(zhǔn)備工作
- 安裝python和cython。
- 安裝mingw工育。注意python和mingw的位數(shù)必須一致虾宇。然后將mingw添加到Path環(huán)境變量。這里我使用的是64位python和mingw如绸。如下圖所示:
image.png
二嘱朽、添加distutils.cfg文件
在python安裝目錄C:\Program Files\Python\Lib\distutils
中新建distutils.cfg
文件,內(nèi)容如下:
[build]
compiler=mingw32
[build_ext]
compiler=mingw32
注:如果目錄已經(jīng)存在該文件怔接,修改成上述內(nèi)容即可搪泳。
三、修改cygwinccompiler.py文件
進(jìn)入python安裝目錄C:\Program Files\Python\Lib\distutils
中蜕提,修改cygwinccompiler.py
文件森书,添加以下內(nèi)容到get_msvcr()
函數(shù)(用于解決ValueError: Unknown MS Compiler version 1900錯(cuò)誤
)。
elif msc_ver == '1900':
# Visual Studio 2015 / Visual C++ 14.0
# "msvcr140.dll no longer exists"
return ['vcruntime140']
修改后的內(nèi)容為:
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)
四、轉(zhuǎn)換python36.dll和vcruntime140.dll文件
轉(zhuǎn)換python36.dll文件
用于解決C:\Program Files\Python\libs/libpython36.a: error adding symbols: File format not recognized
錯(cuò)誤凛膏。
- 復(fù)制
C:\Program Files\Python\python36.dll
到桌面杨名,執(zhí)行以下命令:
gendef python36.dll
dlltool -D python36.dll -d python36.def -l libpython36.a
- 備份
C:\Program Files\Python\libs/libpython36.a
文件,將上步生成的libpython36.a
復(fù)制到C:\Program Files\Python\libs
目錄下猖毫。
轉(zhuǎn)換vcruntime140.dll文件
用于解決C:\Program Files\Python / vcruntime140.dll: file not recognized: File format not recognized
錯(cuò)誤台谍。
- 復(fù)制
C:\Program Files\Python\vcruntime140.dll
到桌面,執(zhí)行以下命令:
gendef vcruntime140.dll
dlltool -D vcruntime140.dll -d vcruntime140.def -l libvcruntime140.a
2.將生成的libvcruntime140.a
復(fù)制到C:\ProgramFiles\Python\libs
目錄即可吁断。
本次測(cè)試環(huán)境:WIN10(64) + Python 3.6.4(64) + MinGW7.2.0 (x86_64-posix-seh-rev0)趁蕊。
版權(quán)聲明:本文為「txfly」的原創(chuàng)文章,遵循 CC 4.0 BY-SA 版權(quán)協(xié)議仔役,轉(zhuǎn)載請(qǐng)附上原文出處鏈接及本聲明掷伙。
原文鏈接:http://www.reibang.com/p/50105307dea5