cython 在 winpython 上运行良好但在 anaconda 上运行不正常 - 链接问题

cython works fine on winpython but not on anaconda - linking issue

我有一个 Cython 代码可以在 WinPython 下正常运行,但是当我从 WinPython 切换到 Anaconda3 时出现问题。我正在使用 Python 3.4

测试代码为:

# cython: boundscheck=False
# cython: wraparound=False
# cython: cdivision=True

cimport cython
cimport numpy as np
import numpy as np
from numpy cimport ndarray as ar
from libc.math cimport *


cpdef ar[double, ndim=1, mode='c'] test(ar[double, ndim=1, mode='c'] x):
    cdef:
        int n = x.shape[0]
        Py_ssize_t i
        ar[double, ndim=1, mode='c'] y = np.zeros(n)*np.nan
    with nogil:
        for i in range(0, n):
            y[i] = x[i]+1
    return y

编译代码为:

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
import numpy as np

ext_modules = [Extension('test', ['tech/test.pyx'], include_dirs=[np.get_include()],
                         define_macros=[('NPY_NO_DEPRECATED_API', None)],
                         extra_compile_args=['-O3', '-march=native', '-ffast-math'],
                         libraries=['m']
                         )]

setup(
    name="Test Function",
    cmdclass={'build_ext': build_ext},
    ext_modules=ext_modules
)

我正在使用 MinGw 进行编译,所以在 ...\Anaconda3\Lib\distutils 文件夹下我有一个包含

的文件
[build]
compiler = mingw32

另外在PATH环境变量中,我还有:

默认: ...\Anaconda3 ...\Anaconda3\Scripts

还补充说: ...\Anaconda3\libs(这包含 python34)

还添加了:从 WinPython 复制的包含 gcc 等的 mingw32 文件: ...\Anaconda3\Tools\tools\mingw32\bin(这包含 gcc) ...\Anaconda3\Tools\tools\mingw32\x86_64-w64-mingw32\bin

当我尝试时:

python setup.py build_ext --inplace

在生成 test.ctest.otest.def 之前,代码运行良好。此后这就是我得到的:

C:\Anaconda3\Tools\tools\mingw32\bin\gcc.exe -shared -s build\temp.win-amd64-3.4\Release\tech\test.o build\temp.win-amd64-3.4\Release\tech\test.def -LC:\Anaconda3\libs -LC:\Anaconda3\PCbuild\amd64 -lm -lpython34 -lmsvcr100 -

o P:\Documents\Temp\python-master\python-master\common\test.pyd

build\temp.win-amd64-3.4\Release\tech\test.o:test.c:(.text+0x8e): undefined reference to `__imp_PyExc_TypeError'

build\temp.win-amd64-3.4\Release\tech\test.o:test.c:(.text+0x10e): undefined reference to `__imp_PyExc_ValueError'

build\temp.win-amd64-3.4\Release\tech\test.o:test.c:(.text+0x259): undefined reference to `__imp__PyThreadState_Current'
....
....
C:/Anaconda3/Tools/tools/mingw32/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/../../../../x86_64-w64-mingw3                ild\temp.win-amd64-3.4\Release\tech\test.o: bad reloc address 0x0 in section `.data'
collect2.exe: error: ld returned 1 exit status                                                                        
error: command 'C:\Anaconda3\Tools\tools\mingw32\bin\gcc.exe' failed with exit status 1   
  1. 首先,我在任何地方都找不到 Anacond3\PCBuild\amd64 directory/file。

  2. 我试图在整个互联网上查找,但找不到任何对 __imp_PyExc_TypeError__imp__PyThreadState_Current__imp_PyExc_ValueError.[=26= 的引用]

最终转换为 *.pyd 文件可能出了什么问题?

我怀疑 Anaconda 还不支持 mingw-64。

从历史上看,mingw-64 确实是一个无望的想法,直到 Cark Kleffner 提出了他的静态版本。

当 Carl 的更新版本作为 mingwpy 轮可用时,情况可能(应该?)改变。

你可以运行

conda remove libpython

让 Anaconda 使用 Visual Studio 而不是 mingw,这可能会更好。