前言

最近做了个project,需要用到PyCrypto。

PyCrypto在Windows下需要使用VC进行编译,不过Python模块在Windows平台编译坑点比较多。

如:

winrand.c
D:\Windows Kits\10\include\10.0.17134.0\ucrt\inttypes.h(27): error C2061: 语法错误: 标识符“intmax_t”
D:\Windows Kits\10\include\10.0.17134.0\ucrt\inttypes.h(28): error C2061: 语法错误: 标识符“rem”
D:\Windows Kits\10\include\10.0.17134.0\ucrt\inttypes.h(28): error C2059: 语法错误:“;”
D:\Windows Kits\10\include\10.0.17134.0\ucrt\inttypes.h(29): error C2059: 语法错误:“}”
D:\Windows Kits\10\include\10.0.17134.0\ucrt\inttypes.h(31): error C2061: 语法错误: 标识符“imaxdiv_t”
D:\Windows Kits\10\include\10.0.17134.0\ucrt\inttypes.h(31): error C2059: 语法错误:“;”
D:\Windows Kits\10\include\10.0.17134.0\ucrt\inttypes.h(41): error C2143: 语法错误: 缺少“{”(在“__cdecl”的前面)
D:\Windows Kits\10\include\10.0.17134.0\ucrt\inttypes.h(42): error C2146: 语法错误: 缺少“)”(在标识符“_Number”的前面)
... blah blah blah ...

解决方案

点击查看原文。

处理方式:
修改文件 setup.py

    def detect_modules (self):
        # Read the config.h file (usually generated by autoconf)
        if self.compiler.compiler_type == 'msvc' and False:
            # Add special include directory for MSVC (because MSVC is special)
            self.compiler.include_dirs.insert(0, "src/inc-msvc/")
            ac = self.__read_autoconf("src/config.h")
        else:
            ac = self.__read_autoconf("src/config.h")

if self.compiler.compiler_type == 'msvc'后加上and False,使special失效。
然后拷贝\src\inc-msvc\config.h 到\src\config.h 编译即可。