Question

I have been writing some python extension modules with cython. The extensions I've written build and work well. Then, I wanted to use typed memoryviews, when accessing my numpy arrays, as they seem to have several advantages http://docs.cython.org/src/userguide/memoryviews.html

However, as soon as I use a memoryview in my cython code I will get an error when building the extension. For example, if I add this test line:

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

to an existing, working cython extension. I will get the following errors:

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

I've tried to add -march=i486 to the gcc line, as suggested in this post: undefined reference to sync_fetch_and_add_4 but that didn't solve the problem. For that matter, I also tried -march=i586 and -march=pentium without success.

Any idea what's going on here?

My platform is Windows 7, mingw version is 4.70, Cython version is 0.17.1

Thanks

Was it helpful?

Solution

I found a solution.

Actually, the gcc flag -march=i486 does solve the problem! However, when I tested it in the console I just applied it to the gcc line for the link step (that's where I got the errors) and as it didn't solve the problem I thought it just didn't work. In fact, I need to use -march=i486 both in the compile and link steps, then there is no errors anymore.

As to how to include these flags when I build the extension, I have tried to add

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

to the setup.py but it didn't seem to work.

So I have modified the c:\python27\Lib\distutils\cygwinccompiler.py to include these flags in the compile and link steps. Not sure if this is very elegant way of setting these flags. Any alternatives are welcome!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top