编译Cython代码时遇到的问题: fatal error LNK1112: module machine type 'x64' conflicts with target machine type 'X86'

时间:2022-10-20 20:58:21

使用python setup.py build_ext --inplace命令编译cython代码, 出现以下错误:

Compiling cython_example.pyx because it changed.
[1/1] Cythonizing cython_example.pyx
D:\python_virtualenv\bigdata\lib\site-packages\Cython\Compiler\Main.py:367: FutureWarning: Cython directive 'language_level' not set, using 2 for now (Py2). This will change in
a later release! File: D:\学习\bigdatalearn\cython_test\cython_example.pyx
tree = Parsing.p_module(s, pxd, full_module_name)
running build_ext
building 'cython_test.cython_example' extension
creating build
creating build\temp.win-amd64-3.6
creating build\temp.win-amd64-3.6\Release
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD "-Id:\program files\python361\include" "-Id:\program files\python361\includ
e" "-IC:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt" "-IC:\Program Files (x86)\Windows Kits
\8.1\include\shared" "-IC:\Program Files (x86)\Windows Kits\8.1\include\um" "-IC:\Program Files (x86)\Windows Kits\8.1\include\winrt" /Tccython_example.c /Fobuild\temp.win-amd64
-3.6\Release\cython_example.obj
cython_example.c
creating D:\学习\bigdatalearn\cython_test\cython_test
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\link.exe /nologo /INCREMENTAL:NO /LTCG /DLL /MANIFEST:EMBED,ID=2 /MANIFESTUAC:NO "/LIBPATH:d:\program files\python361\
Libs" /LIBPATH:D:\python_virtualenv\bigdata\libs /LIBPATH:D:\python_virtualenv\bigdata\PCbuild\amd64 "/LIBPATH:C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\LIB\amd64"
"/LIBPATH:C:\Program Files (x86)\Windows Kits\10\lib\10.0.10240.0\ucrt\x64" "/LIBPATH:C:\Program Files (x86)\Windows Kits\8.1\lib\winv6.3\um\x64" /EXPORT:PyInit_cython_example b
uild\temp.win-amd64-3.6\Release\cython_example.obj /OUT:D:\学习\bigdatalearn\cython_test\cython_test\cython_example.cp36-win_amd64.pyd /IMPLIB:build\temp.win-amd64-3.6\Release\c
ython_example.cp36-win_amd64.lib
python36.lib(python36.dll) : fatal error LNK1112: module machine type 'x64' conflicts with target machine type 'X86'
error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\bin\\link.exe' failed with exit status 1112

解决:

  报错的意思是系统环境是64位windows系统,使用的C编译工具却是32位的。
  windows系统中'x86'表示32位。
  上面这个错的主要问题是使用的link.exe是32位的,只要改成64位的link.exe就没有问题了。
  

  环境: windows64位, python 64位, python3.6.1
  第一步:
    找到windows的系统环境变量path:
    将"C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\amd64;" 加在path最前面,这样就可以优先使用64位的link.exe。
  第二步:
    新建一个Dos窗口, 执行 "echo %PATH%" ,使环境变量立即生效。(在前面建好的DOS窗口下执行没有用)

  第三步:
    如果是在pycharm的Terminal中执行的 python setup.py build_ext --inplace命令,还需要先重启pycharm,再在其中执行编译命令才会有效。

    DOS的话新建一个DOS窗口即可。

解决了上面这个问题,又出现了一个新问题:

running build_ext
building 'cython_test.cython_example' extension
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\amd64\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD "-Id:\program files\python361\include" "-Id:\program files\python361\include" "-IC:\Program Files (x86)\Microsoft Visual
Studio 14.0\VC\INCLUDE" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\8.1\include\shared" "-IC:\Program Files (x86)\Windows Kits\8.1\include\um" "-IC:\Program Files (x
86)\Windows Kits\8.1\include\winrt" /Tccython_example.c /Fobuild\temp.win-amd64-3.6\Release\cython_example.obj
cython_example.c
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\amd64\link.exe /nologo /INCREMENTAL:NO /LTCG /DLL /MANIFEST:EMBED,ID=2 /MANIFESTUAC:NO "/LIBPATH:d:\program files\python361\Libs" /LIBPATH:D:\python_virtualenv\bigdata\li
bs /LIBPATH:D:\python_virtualenv\bigdata\PCbuild\amd64 "/LIBPATH:C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\LIB\amd64" "/LIBPATH:C:\Program Files (x86)\Windows Kits\10\lib\10.0.10240.0\ucrt\x64" "/LIBPATH:C:\Program F
iles (x86)\Windows Kits\8.1\lib\winv6.3\um\x64" /EXPORT:PyInit_cython_example build\temp.win-amd64-3.6\Release\cython_example.obj /OUT:D:\学习\bigdatalearn\cython_test\cython_test\cython_example.cp36-win_amd64.pyd /IMPLIB:build\t
emp.win-amd64-3.6\Release\cython_example.cp36-win_amd64.lib
cython_example.obj : warning LNK4197: export 'PyInit_cython_example' specified multiple times; using first specification
Creating library build\temp.win-amd64-3.6\Release\cython_example.cp36-win_amd64.lib and object build\temp.win-amd64-3.6\Release\cython_example.cp36-win_amd64.exp
Generating code
Finished generating code
LINK : fatal error LNK1158: cannot run 'rc.exe'
error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\bin\\amd64\\link.exe' failed with exit status 1158 

解决:
  错误的意思是缺少rc.exe文件,这个文件在C:\Program Files (x86)\Windows Kits\8.1\bin\x64下,只需要将该目录下的 rc.exe 和 rcdll.dll拷贝一份粘贴到link.exe所在目录C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\amd64下即可。

  如果是32位系统,则是将C:\Program Files (x86)\Windows Kits\8.1\bin\x86下的rc.exe和rcdll.dll粘贴到C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin下。