现在我已经成功地在Windows7上安装了Cython,我尝试用Cython编译一些Cython代码,但是gcc让我的工作变得很艰难。
cdef void say_hello(name):
print "Hello %s" % name
使用gcc编译代码会抛出数十个对 -erros的未定义引用,我非常确定libpython.a
是可用的(正如安装教程所说,如果缺少此文件,则会抛出对-errors的未定义引用)。
$ 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
奇怪的是,使用pyximport
*或setup
-script可以很好地工作,但在处理模块时这两者都不是很方便。
如何用gcc编译那些用Cython生成的 .c
文件?
或者任何其他编译器,重要的是它能工作!
*pyximport
:在导入的模块中只包含python原生函数和类,而不包含cdef函数和类,这正常吗?像这样:
# 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
更新
调用$ 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'
发布于 2011-08-09 03:22:22
尝试:
gcc -c -IC:\Python27\include -o ctest.o ctest.c
gcc -shared -LC:\Python27\libs -o ctest.pyd ctest.o -lpython27
-shared
创建一个共享库。-lpython27
链接到导入库C:\Python27libs\libpython27.a。
发布于 2011-08-09 00:23:20
这是一个链接器(ld)错误,而不是一个编译器错误。您应该提供库的路径(-l和-L),而不仅仅是头文件(-I)。
https://stackoverflow.com/questions/6985109
复制相似问题