2

I'm doing my first test with Cython. Basically the hello.pyx example from http://docs.cython.org/src/quickstart/build.html

When I want to compile I get the following error:

C:[...]>python setup.py build_ext --inplace
Compiling hello.pyx because it changed.
[1/1] Cythonizing hello.pyx
running build_ext
building 'hello' extension
C:\MinGW\bin\gcc.exe -mdll -O -Wall -IC:\Python27\include -IC:\Python27\PC -c he
llo.c -o build\temp.win32-2.7\Release\hello.o
writing build\temp.win32-2.7\Release\hello.def
C:\MinGW\bin\gcc.exe -shared -s build\temp.win32-2.7\Release\hello.o build\temp.
win32-2.7\Release\hello.def -LC:\Python27\libs -LC:\Python27\PCbuild -lpython27
-lmsvcr90 -o C:\Users\Bernd\Documents\99_Projekte\ONGOING\201412_Laufauswertung\
Repro\Tests\hello.pyd
C:\Python27\libs/libpython27.a: error adding symbols: File format not recognized

collect2.exe: error: ld returned 1 exit status
error: command 'C:\\MinGW\\bin\\gcc.exe' failed with exit status 1

The file hello.c is generated.

Any idea?

Edit 1:

Following the hint from J.J. Hakala, i did the following: move libpython27.a away from C:\Python27\libs (for example bak subdirectory), and copy python27.dll from c:\windows\system32 to c:\python27\libs

Now the result is:

C:[...]>python setup.py build_ext --inplace
running build_ext
building 'hello' extension
C:\MinGW\bin\gcc.exe -mdll -O -Wall -IC:\Python27\include -IC:\Python27\PC -c he
llo.c -o build\temp.win32-2.7\Release\hello.o
writing build\temp.win32-2.7\Release\hello.def
C:\MinGW\bin\gcc.exe -shared -s build\temp.win32-2.7\Release\hello.o build\temp.
win32-2.7\Release\hello.def -LC:\Python27\libs -LC:\Python27\PCbuild -lpython27
-lmsvcr90 -o C:\Users\Bernd\Documents\99_Projekte\ONGOING\201412_Laufauswertung\
Repro\Tests\hello.pyd
build\temp.win32-2.7\Release\hello.o:hello.c:(.text+0x2d9): undefined reference
to `_imp___PyThreadState_Current'
build\temp.win32-2.7\Release\hello.o:hello.c:(.text+0x3b3): undefined reference
to `_imp__PyExc_RuntimeError'
build\temp.win32-2.7\Release\hello.o:hello.c:(.text+0x444): undefined reference
to `_imp___Py_NoneStruct'
build\temp.win32-2.7\Release\hello.o:hello.c:(.text+0x8e1): undefined reference
to `_imp__PyExc_ImportError'
collect2.exe: error: ld returned 1 exit status
error: command 'C:\\MinGW\\bin\\gcc.exe' failed with exit status 1

A folder build\temp.win32-2.7\Release with the files hello.def and hello.o is generated.

Edit 2: Maybe the solution is here: compiling Cython with MinGW - undefined reference PyExc

I'll try to install Visual Studio and then try again. Recommendation on how to make it run on MinGW are still wellcome.

4
  • 1
    You could test the following: move libpython27.a away from C:\Python27\libs (for example bak subdirectory), and copy python27.dll from c:\windows\system32 to c:\python27\libs Jan 21, 2016 at 7:31
  • Thank you for that hint J J Hakala. I have the feeling we made some progress, althroug still not working. See edit for details.
    – BerndGit
    Jan 21, 2016 at 18:42
  • Does adding -mthreads when compiling improve the situation? I noticed that I have that flag in my cython-related Makefile. Jan 21, 2016 at 19:15
  • python setup.py build_ext --inplace -mthreads did deliver Option -m not recognised. I'm not trying to install dlltool and gendef.
    – BerndGit
    Jan 22, 2016 at 21:15

1 Answer 1

3

If tools gendef and dlltool are installed, the following should be possible in a temporary directory (assuming msys or cygwin environment):

gendef c:/Windows/System32/python27.dll
dlltool -U -d python27.def -l libpython27.dll.a
cp libpython27.dll.a c:/Python27/libs

Substitute C:/Python27 if necessary.

gcc will now use the generated file instead of some existing .a or .lib file.

3
  • Great News: after doing this python setup.py build_ext --inplace runs through w/o any error. An Folder releaseis generated with *.gen *.o *.pyd files. I'm only confused since i don't find an *.exe file. So where do i find an Windows executable? (Sorry if i have some misunderstanding here)
    – BerndGit
    Jan 22, 2016 at 22:35
  • 1
    @BerndGit you have built a python extension module, not a standalone executable. When you do import hello, the classes and functions provided by the extension module will be available. Jan 22, 2016 at 22:42
  • Ok thanks. I found answers to my follow up questions at stackoverflow.com/questions/2581784/…
    – BerndGit
    Jan 23, 2016 at 7:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.