简体   繁体   English

在Windows上使用gcc(mingw32)从命令行编译Cython扩展

[英]Compile Cython extensions from the command line with gcc (mingw32) on windows

I'm trying to test a small cython module on win32, and I'm having trouble building it. 我正在尝试在win32上测试一个小型cython模块,但无法构建它。

The file is called linalg_cython.pyx and has these contents: 该文件称为linalg_cython.pyx,具有以下内容:

from __future__ import absolute_import, division, print_function
import numpy as np
cimport numpy as np
import cython

#@cython.boundscheck(False)
#np.ndarray[np.float32]
#@cython.wraparound(False)
def L2_sqrd_float32(np.ndarray hist1, np.ndarray hist2):
    """ returns the squared L2 distance
    seealso L2
    Test:
    hist1 = np.random.rand(4, 2)
    hist2 = np.random.rand(4, 2)
    out = np.empty(hist1.shape, dtype=hist1.dtype)
    """
    return (np.abs(hist1 - hist2) ** 2).sum(-1)  # this is faster


L2_sqrd = L2_sqrd_float32

I was able to get this compiling by using a setup.py, but I don't want to have to rely on setup.py to build the extensions. 我可以使用setup.py进行编译,但是我不想依靠setup.py来构建扩展。 This is because I haven't fully understood the cython compilation process yet. 这是因为我还没有完全了解cython编译过程。 I want to compile it on my own first before I start trusting setup.py. 在开始信任setup.py之前,我想先自行编译它。 That being said I was able get a good start by looking at the output of "setup.py build_ext": 话虽如此,我可以通过查看“ setup.py build_ext”的输出来获得一个良好的开端:

C:\MinGW\bin\gcc.exe -mdll -O -Wall ^
        -IC:\Python27\Lib\site-packages\numpy\core\include ^
        -IC:\Python27\include -IC:\Python27\PC ^
        -c vtool\linalg_cython.c ^
        -o build\temp.win32-2.7\Release\vtool\linalg_cython.o


writing build\temp.win32-2.7\Release\vtool\linalg_cython.def


C:\MinGW\bin\gcc.exe -shared \
        -s \
        build\temp.win32-2.7\Release\vtool\linalg_cython.o \
        build\temp.win32-2.7\Release\vtool\linalg_cython.def \
        -LC:\Python27\libs \
        -LC:\Python27\PCbuild \
        -lpython27 \
        -lmsvcr90 \
        -o build\lib.win32-2.7\vtool\linalg_cython.pyd

The pyd file that this created seemed to work, but my goal is understanding, not just making it work. 这个创建的pyd文件似乎可以正常工作,但是我的目标是理解,而不仅仅是使其正常工作。

Copying this format (and trying some things myself) I'm currently using these commands to build everything manually. 复制这种格式(并自己尝试一些操作),我目前正在使用这些命令来手动构建所有内容。

C:\Python27\Scripts\cython.exe vtool\linalg_cython.pyx

C:\MinGW\bin\gcc.exe -mdll -O -DNPY_NO_DEPRECATED_API -Wall -Wno-unknown-pragmas
  -Wno-format -Wno-unused-function -m32 -shared
  -IC:\Python27\Lib\site-packages\numpy\core\include -IC:\Python27\include
  -IC:\Python27\PC -IC:\Python27\Lib\site-packages\numpy\core\include
  -LC:\Python27\libs -LC:\Python27\PCbuild -lpython27 -lmsvcr90 -o
  vtool\linalg_cython.pyd -c vtool\linalg_cython.c

The main difference between my command and the setup.py command is that I'm trying to call gcc in one line instead of splitting it up into two lines. 我的命令和setup.py命令之间的主要区别在于,我试图在一行中调用gcc,而不是将其分成两行。 I would have called it in two commands, but the def file seems to be autogenerated by setup.py and I'm not sure what its all about. 我会用两个命令来调用它,但是def文件似乎是由setup.py自动生成的,我不确定它的全部含义。

Its contents seem simple: 它的内容看起来很简单:

LIBRARY linalg_cython.pyd
EXPORTS
initlinalg_cython

but I'd like to know more about what it is before I split my command into two steps and autogenerate this def file myself. 但在将命令分为两步并自己自动生成此def文件之前,我想了解更多信息。 Either way shouldn't it be possible to create the .pyd in one call to gcc? 两种方法都不应该在对gcc的一次调用中创建.pyd吗?

With the command that I'm using I'm able to get a .pyd file in the right place, but when I try to import it I get 使用我正在使用的命令,我可以在正确的位置获取一个.pyd文件,但是当我尝试导入该文件时,我得到了

<type 'exceptions.ImportError'>: DLL load failed: %1 is not a valid Win32 application.

which is supposed to be a x86/x64 mismatch, hence why I tried adding the flag -m32. 这应该是x86 / x64不匹配的,因此为什么我尝试添加标志-m32。

In summary: When trying to comiple a simple cython module my gcc command is giving me 32/64 bit errors. 总结:当尝试简化一个简单的cython模块时,我的gcc命令给了我32/64位错误。 How do I fix my gcc command such that it generates a valid 32 bit pyd file. 如何修复我的gcc命令,使其生成有效的32位pyd文件。

You didn't mention if your python is 32bit or 64bit..this kind of behavior usually happens when you try to import 32bit module in 64bit python, or vice versa. 您没有提到python是32位还是64位。当您尝试在64位python中导入32位模块时,通常会发生这种行为,反之亦然。 Make sure your python and module, you`re trying to import, are same bit architecture. 确保您要导入的python和模块是相同的体系结构。 Easiest way to fix this is by downloading and installing right python. 解决此问题的最简单方法是下载并安装正确的python。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Windows上通过MINGW32在Python中进行系统调用 - System Call in Python via MINGW32 on Windows 无法在 GIT bash mingw32 for Windows 中激活 virtualenv - Can not activate a virtualenv in GIT bash mingw32 for Windows python - 获取Pip在Windows上使用MinGW32? - python - Getting Pip to use MinGW32 on Windows? Python 2.7 Windows 7安装mysqllib mingw32 - python 2.7 windows 7 installing mysqllib mingw32 无法使用MingW在Windows上编译Cython - Can't Compile Cython on Windows with MingW 使用cython和mingw进行编译会产生gcc:错误:无法识别的命令行选项'-mno-cygwin' - Compiling with cython and mingw produces gcc: error: unrecognized command line option '-mno-cygwin' 无法在 cygwin 或 dos 命令提示符下使用 Python 3.2 和 MinGW32 安装 Cython - Cannot install Cython with Python 3.2 and MinGW32 under cygwin or under dos comandprompt MySQL for Python编译器MinGW32 gcc.exe错误:找不到CreateProcess文件或目录 - Mysql for Python compiler MinGW32 gcc.exe error: CreateProcess file or directory not found Pycharm:编译 Cython 扩展错误 - 无法执行“gcc”:没有这样的文件或目录 - Pycharm: Compile Cython Extensions Error - unable to execute 'gcc': No such file or directory 尝试使用distutils为mingw32交叉编译SWIG Python扩展时出错 - Error when trying to cross-compile SWIG Python extension for mingw32 using distutils
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM