Windows下使用mingw32编译Python扩展py-bcrypt及使用patch文件备忘

!本文可能 超过1年没有更新,今后内容也许不会被维护或者支持,部分内容可能具有时效性,涉及技术细节或者软件使用方面,本人不保证相应的兼容和可操作性。

项目在Windows下构建,某个密码哈希加密模块使用了py-bcrypt,可惜的是官方只提供了C语言源代码,没有现成的Windows二进制包,我还翻阅了《Unofficial Windows Binaries for Python Extension Packages》也没找到现成的包,当然这个对于Linux系统不成问题,按部就班的安装就OK啦,可是Windows下遇到了些麻烦,比如error: Setup script exited with error: Unable to find vcvarsall.bat错误,于是我采用mingw32进行编译,当然如果你的电脑上没有安装mingw32,可以参考下面的步骤,[来源]

1. 点击这里下载最新版本的MinGW,并安装。

2. 设置PATH环境变量,比如你装在C:\MinGW路径下,你需要在PATH环境变量中添加C:\MinGW\bin路径。

完成上面两步后切换到项目路径,然后运行命令python setup.py install build --compiler=mingw32,本来以为一切顺利,结果偏偏报cc1.exe: error: unrecognized command line option '-mno-cygwin'错误:

running install
running bdist_egg
running egg_info
creating py_bcrypt.egg-info
writing py_bcrypt.egg-info\PKG-INFO
writing top-level names to py_bcrypt.egg-info\top_level.txt
writing dependency_links to py_bcrypt.egg-info\dependency_links.txt
writing manifest file 'py_bcrypt.egg-info\SOURCES.txt'
reading manifest file 'py_bcrypt.egg-info\SOURCES.txt'
writing manifest file 'py_bcrypt.egg-info\SOURCES.txt'
installing library code to build\bdist.win-amd64\egg
running install_lib
running build_py
creating build
creating build\lib.win-amd64-2.7
creating build\lib.win-amd64-2.7\bcrypt
copying bcrypt\__init__.py -> build\lib.win-amd64-2.7\bcrypt
running build_ext
building 'bcrypt._bcrypt' extension
creating build\temp.win-amd64-2.7
creating build\temp.win-amd64-2.7\Release
creating build\temp.win-amd64-2.7\Release\bcrypt
D:\MinGW\bin\gcc.exe -mno-cygwin -mdll -O -Wall -ID:\Python27\include -ID:\Pytho
n27\PC -c bcrypt/bcrypt_python.c -o build\temp.win-amd64-2.7\Release\bcrypt\bcry
pt_python.o
cc1.exe: error: unrecognized command line option '-mno-cygwin'
error: command 'gcc' failed with exit status 1

搜索了网络,找到这么篇求助《mingw32编译安装Python第三方库失败,:unrecognized command line option ‘-mno-cygwin’》,得知-mno-cygwin参数已经不被新版本的gcc支持了,所以需要修改源代码移除这个参数:

如果你的Python27装在C盘的,那么可以打开C:\Python27\Lib\distutils\cygwinccompiler.py文件,然后找到大概322行左右下面一段:

322
323
324
325
326
327
328
self.set_executables(compiler='gcc -mno-cygwin -O -Wall',
     compiler_so='gcc -mno-cygwin -mdll -O -Wall',
     compiler_cxx='g++ -mno-cygwin -O -Wall',
     linker_exe='gcc -mno-cygwin',
     linker_so='%s -mno-cygwin %s %s'
        % (self.linker_dll, shared_option,
           entry_point))

删除所有的-mno-cygwin字符串,然后保存。

好了,接下来可以继续了,重复刚才的安装命令,但是我又想错了,编译py-bcrypt出现下面的错误提示:

running install
running bdist_egg
running egg_info
writing py_bcrypt.egg-info\PKG-INFO
writing top-level names to py_bcrypt.egg-info\top_level.txt
writing dependency_links to py_bcrypt.egg-info\dependency_links.txt
reading manifest file 'py_bcrypt.egg-info\SOURCES.txt'
writing manifest file 'py_bcrypt.egg-info\SOURCES.txt'
installing library code to build\bdist.win-amd64\egg
running install_lib
running build_py
running build_ext
building 'bcrypt._bcrypt' extension
D:\MinGW\bin\gcc.exe -mdll -O -Wall -ID:\Python27\include -ID:\Python27\PC -c bc
rypt/bcrypt_python.c -o build\temp.win-amd64-2.7\Release\bcrypt\bcrypt_python.o
bcrypt/bcrypt_python.c:29:26: error: unknown type name 'u_int8_t'
bcrypt/bcrypt_python.c:29:38: error: unknown type name 'u_int16_t'
bcrypt/bcrypt_python.c:29:49: error: unknown type name 'u_int8_t'
bcrypt/bcrypt_python.c: In function 'bcrypt_encode_salt':
bcrypt/bcrypt_python.c:56:2: warning: implicit declaration of function 'encode_s
alt' [-Wimplicit-function-declaration]
error: command 'gcc' failed with exit status 1

重点是形如unknown type name 'u_int8_t'的错误,Windows SDK环境下没有定义这个类型,最后在stackoverflow的这篇求助《py-bcrypt installing on win 7 64bit python》找到了答案,为了能够在Windows下编译,我们还需要打一个Patch,我们可以到这里下载这个Patch。

问题又来了:Windows下如何打这个Patch呢?幸好我们可以找到Python下的一个实用脚本python-patch,下载后可以重命名为patch.py并保存到C:\Python27\Scripts(假设Python安装在C盘)下,然后运行形如命令C:\Python27\Scripts\patch.py file1.patch来打patch,注意这里的file1.patch是你刚才下载的patch文件。

打好patch后,让我们再次执行刚才的编译安装命令,结果依旧失败:

running install
running bdist_egg
running egg_info
writing py_bcrypt.egg-info\PKG-INFO
writing top-level names to py_bcrypt.egg-info\top_level.txt
writing dependency_links to py_bcrypt.egg-info\dependency_links.txt
reading manifest file 'py_bcrypt.egg-info\SOURCES.txt'
writing manifest file 'py_bcrypt.egg-info\SOURCES.txt'
installing library code to build\bdist.win-amd64\egg
running install_lib
running build_py
running build_ext
building 'bcrypt._bcrypt' extension
D:\MinGW\bin\gcc.exe -mdll -O -Wall -ID:\Python27\include -ID:\Python27\PC -c bc
rypt/bcrypt_python.c -o build\temp.win-amd64-2.7\Release\bcrypt\bcrypt_python.o
bcrypt/bcrypt_python.c: In function 'bcrypt_encode_salt':
bcrypt/bcrypt_python.c:56:2: warning: pointer targets in passing argument 2 of '
encode_salt' differ in signedness [-Wpointer-sign]
bcrypt/bcrypt_python.c:29:6: note: expected 'u_int8_t *' but argument is of type
 'char *'
D:\MinGW\bin\gcc.exe -mdll -O -Wall -ID:\Python27\include -ID:\Python27\PC -c bc
rypt/blowfish.c -o build\temp.win-amd64-2.7\Release\bcrypt\blowfish.o
D:\MinGW\bin\gcc.exe -mdll -O -Wall -ID:\Python27\include -ID:\Python27\PC -c bc
rypt/bcrypt.c -o build\temp.win-amd64-2.7\Release\bcrypt\bcrypt.o
writing build\temp.win-amd64-2.7\Release\bcrypt\_bcrypt.def
D:\MinGW\bin\gcc.exe -shared -s build\temp.win-amd64-2.7\Release\bcrypt\bcrypt_p
ython.o build\temp.win-amd64-2.7\Release\bcrypt\blowfish.o build\temp.win-amd64-
2.7\Release\bcrypt\bcrypt.o build\temp.win-amd64-2.7\Release\bcrypt\_bcrypt.def
-LD:\Python27\libs -LD:\Python27\PCbuild\amd64 -lpython27 -lmsvcr90 -o build\lib
.win-amd64-2.7\bcrypt\_bcrypt.pyd
build\temp.win-amd64-2.7\Release\bcrypt\bcrypt_python.o:bcrypt_python.c:(.text+0
x5b): undefined reference to `_imp__PyArg_ParseTupleAndKeywords'
build\temp.win-amd64-2.7\Release\bcrypt\bcrypt_python.o:bcrypt_python.c:(.text+0
x73): undefined reference to `_imp__PyExc_ValueError'
build\temp.win-amd64-2.7\Release\bcrypt\bcrypt_python.o:bcrypt_python.c:(.text+0
x7e): undefined reference to `_imp__PyErr_SetString'
build\temp.win-amd64-2.7\Release\bcrypt\bcrypt_python.o:bcrypt_python.c:(.text+0
x9e): undefined reference to `_imp__PyExc_ValueError'
build\temp.win-amd64-2.7\Release\bcrypt\bcrypt_python.o:bcrypt_python.c:(.text+0
xa9): undefined reference to `_imp__PyErr_SetString'
build\temp.win-amd64-2.7\Release\bcrypt\bcrypt_python.o:bcrypt_python.c:(.text+0
xdc): undefined reference to `_imp__PyString_FromString'
build\temp.win-amd64-2.7\Release\bcrypt\bcrypt_python.o:bcrypt_python.c:(.text+0
x134): undefined reference to `_imp__PyArg_ParseTupleAndKeywords'
build\temp.win-amd64-2.7\Release\bcrypt\bcrypt_python.o:bcrypt_python.c:(.text+0
x15e): undefined reference to `_imp__PyEval_SaveThread'
build\temp.win-amd64-2.7\Release\bcrypt\bcrypt_python.o:bcrypt_python.c:(.text+0
x177): undefined reference to `_imp__PyEval_RestoreThread'
build\temp.win-amd64-2.7\Release\bcrypt\bcrypt_python.o:bcrypt_python.c:(.text+0
x1b0): undefined reference to `_imp__PyExc_ValueError'
build\temp.win-amd64-2.7\Release\bcrypt\bcrypt_python.o:bcrypt_python.c:(.text+0
x1bb): undefined reference to `_imp__PyErr_SetString'
build\temp.win-amd64-2.7\Release\bcrypt\bcrypt_python.o:bcrypt_python.c:(.text+0
x1cb): undefined reference to `_imp__PyString_FromString'
build\temp.win-amd64-2.7\Release\bcrypt\bcrypt_python.o:bcrypt_python.c:(.text+0
x20a): undefined reference to `_imp__Py_InitModule4'
build\temp.win-amd64-2.7\Release\bcrypt\bcrypt_python.o:bcrypt_python.c:(.text+0
x223): undefined reference to `_imp__PyModule_AddStringConstant'
collect2: ld returned 1 exit status
error: command 'gcc' failed with exit status 1

这次出现大量的undefined reference to错误,搜索网络,然后设置C:\Python27\libs到环境变量中,错误依旧,然后我就怀疑是否和我的64位Windows 7和64位Python 27有关系了,结果还真是这个原因,最后还是使用微软的MSVC编译成功的,关于详细步骤可以参考下一篇文章。

若无特别说明,本网站文章均为原创,原则上这些文章不允许转载,但是如果阁下是出于研究学习目的可以转载到阁下的个人博客或者主页,转载遵循创作共同性“署名-非商业性使用-相同方式共享”原则,请转载时注明作者出处谢绝商业性、非署名、采集站、垃圾站或者纯粹为了流量的转载。谢谢合作!
请稍后...

发表评论

电子邮件地址不会被公开。 必填项已用*标注

*