27

Now that I've successfully installed Cython on Windows 7, I try to compile some Cython code using Cython, but gcc makes my life hard.

cdef void say_hello(name):
    print "Hello %s" % name

Using gcc to compile the code throws dozens of undefined reference to -erros, and I'm pretty sure the libpython.a is available (as the installation tutorial said, undefined reference to -errors are thrown if this file is missing).

$ cython ctest.pyx
$ gcc ctest.c -I"C:\Python27\include"

C:\Users\niklas\AppData\Local\Temp\cckThGrF.o:ctest.c:(.text+0x1038): undefined reference to `_imp__PyString_FromStringAndSize'
C:\Users\niklas\AppData\Local\Temp\cckThGrF.o:ctest.c:(.text+0x1075): undefined reference to `_imp___Py_TrueStruct'
C:\Users\niklas\AppData\Local\Temp\cckThGrF.o:ctest.c:(.text+0x1086): undefined reference to `_imp___Py_ZeroStruct'
C:\Users\niklas\AppData\Local\Temp\cckThGrF.o:ctest.c:(.text+0x1099): undefined reference to `_imp___Py_NoneStruct'
C:\Users\niklas\AppData\Local\Temp\cckThGrF.o:ctest.c:(.text+0x10b8): undefined reference to `_imp__PyObject_IsTrue'
c:/program files/mingw/bin/../lib/gcc/mingw32/4.5.2/../../../libmingw32.a(main.o):main.c:(.text+0xd2): undefined reference to `WinMain@16'
collect2: ld returned 1 exit status

The weird thing is, using pyximport* or a setup-script works pretty fine, but it's both not very handy when still working on a module.

How to compile those .c files generated with Cython using gcc ?

or any other compiler, important is that it will work !


*pyximport: Is it normal that only python-native functions and classes are contained in the imported module and not cdef-functions and classes ? like:

# filename: cython_test.pyx
cdef c_foo():
    print "c_foo !"
def foo():
    print "foo !"
    c_foo()

import pyximport as p; p.install()
import cython_test
cython_test.foo()
# foo !\nc_foo !
cython_test.c_foo()
# AttributeError, module object has no attribute c_foo


UPDATE

Calling $ gcc ctest.c "C:\Python27\libs\libpython27.a" kills the undefined reference to -erros, but this one:

c:/program files/mingw/bin/../lib/gcc/mingw32/4.5.2/../../../libmingw32.a(main.o):main.c:(.text+0xd2): undefined reference to `WinMain@16'
Niklas R
  • 16,299
  • 28
  • 108
  • 203
  • The "undefined reference to `WinMain@16'" means that the gcc is looking (without luck) for the "main" entry point. See for instance http://stackoverflow.com/questions/5259714/undefined-reference-to-winmain16 – Francesco Aug 08 '11 at 19:05

2 Answers2

21

Try:

gcc -c -IC:\Python27\include -o ctest.o ctest.c
gcc -shared -LC:\Python27\libs -o ctest.pyd ctest.o -lpython27

-shared creates a shared library. -lpython27 links with the import library C:\Python27\libs\libpython27.a.

Eryk Sun
  • 33,190
  • 5
  • 92
  • 111
  • Awesome, the .pyd file did compile ! I can include it to python. Can't cdefs be called from python ? So I do have to write a def-wrapper for every cdef ? I'll check this out ! – Niklas R Aug 08 '11 at 19:28
  • VERY IMPORTANT: if `-lpython27` is placed before the `ctest.c -o ctest.pyd` it just does not work... – Saullo G. P. Castro Jul 08 '13 at 06:59
  • @SaulloCastro: I edited the command to try to make the order more clear. – Eryk Sun Jul 08 '13 at 07:47
1

That is a linker (ld) error and not a compiler error. You should provide the path to the library (-l and -L) and not only to the headers (-I).

Francesco
  • 3,200
  • 1
  • 34
  • 46
  • unfortunately, adding `-L"C:\Python27\libs"` does not fix this problem. – Niklas R Aug 08 '11 at 17:17
  • using `-I"C:\Python27\libs\libpython.a"`, gcc tells me that -I is not a directory. **BTW**: please notice the updated question. – Niklas R Aug 08 '11 at 19:06
  • it's a lowercase L, not an uppercase i – Francesco Aug 08 '11 at 19:09
  • Ah, sorry. Doing so (`gcc ctest.c -l "C:\Python27\libs\libpython27.a"`) causes this error, really confusing. This file exists. `c:/program files/mingw/bin/../lib/gcc/mingw32/4.5.2/../../../../mingw32/bin/ld.exe: cannot find -lC:\Python27\libs\libpython27.a` – Niklas R Aug 08 '11 at 19:19
  • Thanks for your help anyway, eryksun's version works just fine. :) – Niklas R Aug 08 '11 at 19:31
  • yes the "version" is the same, -L is the path to the library dir and -l specifies the name of the library :-) – Francesco Aug 08 '11 at 19:35
  • oh.. I understand. lol. I thougt I should add the full path to `-l`. You'll earn a +1 as well. :) – Niklas R Aug 08 '11 at 19:38