Nik*_*s R 20 c python gcc compilation cython
现在我已经在Windows 7上成功安装了Cython,我尝试使用Cython编译一些Cython代码,但是gcc让我的生活变得艰难.
cdef void say_hello(name):
print "Hello %s" % name
Run Code Online (Sandbox Code Playgroud)
使用gcc编译代码会引发许多未定义的 -erros 引用,我很确定它libpython.a是可用的(正如安装教程所说,如果缺少此文件,则会引发未定义的引用错误).
$ cython ctest.pyx
$ gcc ctest.c -I"C:\Python27\include"
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
奇怪的是,使用pyximport*或setup-script工作得非常好,但是当它仍在处理模块时它们都不是很方便.
.c使用gcc 编译使用Cython生成的文件?或任何其他编译器,重要的是它会工作!
*pyximport:在导入的模块中只包含python-native函数和类而不包含cdef函数和类,这是正常的吗?喜欢:
# filename: cython_test.pyx
cdef c_foo():
print "c_foo !"
def foo():
print "foo !"
c_foo()
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
调用会$ gcc ctest.c "C:\Python27\libs\libpython27.a"杀死对 -erros 的未定义引用,但是这个:
c:/program files/mingw/bin/../lib/gcc/mingw32/4.5.2/../../../libmingw32.a(main.o):main.c:(.text+0xd2): undefined reference to `WinMain@16'
Run Code Online (Sandbox Code Playgroud)
Ery*_*Sun 19
尝试:
gcc -c -IC:\Python27\include -o ctest.o ctest.c
gcc -shared -LC:\Python27\libs -o ctest.pyd ctest.o -lpython27
Run Code Online (Sandbox Code Playgroud)
-shared创建一个共享库.-lpython27链接到导入库C:\ Python27\libs\libpython27.a.