简体   繁体   中英

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.

The file is called linalg_cython.pyx and has these contents:

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. This is because I haven't fully understood the cython compilation process yet. I want to compile it on my own first before I start trusting setup.py. That being said I was able get a good start by looking at the output of "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.

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. 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.

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. Either way shouldn't it be possible to create the .pyd in one call to gcc?

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

<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.

In summary: When trying to comiple a simple cython module my gcc command is giving me 32/64 bit errors. How do I fix my gcc command such that it generates a valid 32 bit pyd file.

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. Make sure your python and module, you`re trying to import, are same bit architecture. Easiest way to fix this is by downloading and installing right python.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM