1

I am new to Cython and I am trying to convert a python file to a C file and then to an executable using Cython, but after generating the C file when I try to compile it using GCC in Windows I get a lot of Undefined reference errors:

Here's what I did;

My pyx file:

cdef public void fun():
    print('hello, world!')
if __name__ == "__main__":
    fun()

Then I used this code to generate the C file:

python -m cython hello.pyx --embed -3

this went succesfully and I got a hello.c file. Then I tried to compile this using:

x86_64-w64-mingw32-gcc -mconsole -DSIZEOF_VOID_P=8 hello.c -IE:\phyton\include -LE:\phyton\libs -lpython38 -o hello.exe -DMS_WIN64

but that didn't work and I got this:

C:\Users\STIP\AppData\Local\Temp\ccZz6iu8.o:hello.c:(.text+0xf7c): undefined reference to `__imp_PyExc_SystemError'
C:\Users\STIP\AppData\Local\Temp\ccZz6iu8.o:hello.c:(.text+0x1175): undefined reference to `__imp__Py_NoneStruct'
C:\Users\STIP\AppData\Local\Temp\ccZz6iu8.o:hello.c:(.text+0x129c): undefined reference to `__imp_PyUnicode_Type'
C:\Users\STIP\AppData\Local\Temp\ccZz6iu8.o:hello.c:(.text+0x12b7): undefined reference to `__imp_PyUnicode_Type'
C:\Users\STIP\AppData\Local\Temp\ccZz6iu8.o:hello.c:(.text+0x17da): undefined reference to `__imp__Py_NoneStruct'
C:\Users\STIP\AppData\Local\Temp\ccZz6iu8.o:hello.c:(.text+0x17f4): undefined reference to `__imp__Py_NoneStruct'
C:\Users\STIP\AppData\Local\Temp\ccZz6iu8.o:hello.c:(.text+0x19a3): undefined reference to `__imp__Py_FalseStruct'
C:\Users\STIP\AppData\Local\Temp\ccZz6iu8.o:hello.c:(.text+0x19ac): undefined reference to `__imp__Py_TrueStruct'
C:\Users\STIP\AppData\Local\Temp\ccZz6iu8.o:hello.c:(.text+0x19fe): undefined reference to `__imp__Py_FalseStruct'
C:\Users\STIP\AppData\Local\Temp\ccZz6iu8.o:hello.c:(.text+0x1a16): undefined reference to `__imp__Py_FalseStruct'
C:\Users\STIP\AppData\Local\Temp\ccZz6iu8.o:hello.c:(.text+0x1a23): undefined reference to `__imp__Py_TrueStruct'
C:\Users\STIP\AppData\Local\Temp\ccZz6iu8.o:hello.c:(.text+0x21a0): undefined reference to `__imp_PyModule_Type'
C:\Users\STIP\AppData\Local\Temp\ccZz6iu8.o:hello.c:(.text+0x21b8): undefined reference to `__imp_PyModule_Type'
C:\Users\STIP\AppData\Local\Temp\ccZz6iu8.o:hello.c:(.text+0x22b4): undefined reference to `__imp_PyBaseObject_Type'
C:\Users\STIP\AppData\Local\Temp\ccZz6iu8.o:hello.c:(.text+0x284b): undefined reference to `__imp__Py_TrueStruct'
C:\Users\STIP\AppData\Local\Temp\ccZz6iu8.o:hello.c:(.text+0x285f): undefined reference to `__imp__Py_FalseStruct'
C:\Users\STIP\AppData\Local\Temp\ccZz6iu8.o:hello.c:(.text+0x2875): undefined reference to `__imp__Py_NoneStruct'
C:\Users\STIP\AppData\Local\Temp\ccZz6iu8.o:hello.c:(.text+0x28d7): undefined reference to `__imp_PyExc_DeprecationWarning'
C:\Users\STIP\AppData\Local\Temp\ccZz6iu8.o:hello.c:(.text+0x292e): undefined reference to `__imp_PyExc_TypeError'
collect2.exe: error: ld returned 1 exit status

So where is my mistake, most of the time this has to do with linking libraries, but I have done that properly so can anybody help?

ead
  • 32,758
  • 6
  • 90
  • 153
AnonymousK
  • 11
  • 1
  • The point of a setup.py file is the that it should handle the whole compilation process and you shouldn't have to call cython or gcc separately. – DavidW Sep 21 '20 at 17:27

1 Answers1

0

While MSVC needs the corresponding *.lib-file for linking a dll, x86_64-w64-mingw32-gcc-linker is different and uses directly the *.dll (i.e. similar to using *.so-file for linking on Linux). See also this SO-post for more details.

Your command passes -LE:\phyton\libs to the linker and this is where the *.lib-files reside. The linker finds python38.lib (so there is no error, that lpython38 was not found) but doesn't find the expected symbols inside.

You need to provide path where the needed python38.dll can be found. Normally it is in the same folder as the python-executable, so probably in E:\python, that means

x86_64-w64-mingw32-gcc  <....> -LE:\phyton -lpython38 -o hello.exe -DMS_WIN64 -municode

should be used. Why -municode is needed see this SO-post.

ead
  • 32,758
  • 6
  • 90
  • 153