首页 文章

cython:与mingw构建错误的memoryview

提问于
浏览
2

我一直在用cython编写一些python扩展模块 . 我写的扩展构建并运行良好 . 然后,我想在访问我的numpy数组时使用类型化的内存视图,因为它们似乎有几个优点http://docs.cython.org/src/userguide/memoryviews.html

但是,只要我在我的cython代码中使用memoryview,我就会在构建扩展时出错 . 例如,如果我添加此测试行:

cdef double [:,:: 1] X = np.zeros((100,100))

到现有的,有效的cython扩展 . 我会收到以下错误:

C:\MinGW\bin\gcc.exe -shared -s build\temp.win32-2.7\Release\image_box.o build\temp.win32-2.7\Release\image_box.def -Lc:\python27\libs -Lc:\python27\PCbuild -lp
ython27 -lmsvcr90 -o x:\ARframework\python\image_ops\image_box.pyd
build\temp.win32-2.7\Release\image_box.o:image_box.c:(.text+0xe23): undefined reference to `___sync_fetch_and_add_4'
build\temp.win32-2.7\Release\image_box.o:image_box.c:(.text+0x3318): undefined reference to `___sync_fetch_and_add_4'
build\temp.win32-2.7\Release\image_box.o:image_box.c:(.text+0x4c81): undefined reference to `___sync_fetch_and_sub_4'
build\temp.win32-2.7\Release\image_box.o:image_box.c:(.text+0x4d37): undefined reference to `___sync_fetch_and_sub_4'
build\temp.win32-2.7\Release\image_box.o:image_box.c:(.text+0x10767): undefined reference to `___sync_fetch_and_sub_4'
build\temp.win32-2.7\Release\image_box.o:image_box.c:(.text+0x10793): undefined reference to `___sync_fetch_and_sub_4'
collect2.exe: error: ld returned 1 exit status
error: command 'gcc' failed with exit status 1

我试图将-march = i486添加到gcc行,如本文中所述:undefined reference to sync_fetch_and_add_4但这并没有解决问题 . 就此而言,我也试过-march = i586和-march = pentium但没有成功 .

知道这里发生了什么吗?

我的平台是Windows 7,mingw版本是4.70,Cython版本是0.17.1

谢谢

1 回答

  • 3

    我找到了解决方案 .

    实际上,gcc标志-march = i486确实解决了这个问题!但是,当我在控制台中测试它时,我只是将它应用到链接步骤的gcc行(这是我得到错误的地方)并且因为它没有解决问题我认为它只是不起作用 . 实际上,我需要在编译和链接步骤中使用-march = i486,然后就不再有错误了 .

    至于在构建扩展时如何包含这些标志,我试图添加

    import os
    os.environ['LDFLAGS'] = '-march=i486'
    os.environ['CFLAGS'] = '-march=i486'
    

    到setup.py但它似乎没有工作 .

    所以我修改了c:\ python27 \ Lib \ distutils \ cygwinccompiler.py以在编译和链接步骤中包含这些标志 . 不确定这是否是设置这些标志的非常优雅的方式 . 欢迎任何替代方案!

相关问题