2012-07-31 125 views
1

我在一台较旧的计算机上,我试图用Cython加速我编写的一些python代码,但是即使是最简单的脚本,我也无法使用Cython。Cython编译器错误

这里是Python代码,我试图通过用Cython运行,并从我的知识应该工作:

def hw(): 
    print "Hello World" 

if __name__ == "__main__": 
    hw() 

这里是从我的终端输出:

C:\PyProjects\_cython>python hw.py 
Hello World 

C:\PyProjects\_cython>cython hw.py 

C:\PyProjects\_cython>gcc hw.c 
In file included from hw.c:4: 
Python.h:8:22: error: pyconfig.h: No such file or directory 
hw.c:457: error: expected '=', ',', ';', 'asm' or '__attribute__' before '__Pyx_ 
PyInt_AsUnsignedLongLong' 
hw.c:461: error: expected '=', ',', ';', 'asm' or '__attribute__' before '__Pyx_ 
PyInt_AsLongLong' 
hw.c:465: error: expected '=', ',', ';', 'asm' or '__attribute__' before '__Pyx_ 
PyInt_AsSignedLongLong' 
hw.c: In function '__Pyx_PyUnicode_Equals': 
hw.c:826: error: 'Py_UNICODE' undeclared (first use in this function) 
hw.c:826: error: (Each undeclared identifier is reported only once 
hw.c:826: error: for each function it appears in.) 
hw.c:826: error: expected ';' before 'ch1' 
hw.c:827: error: expected ';' before 'ch2' 
hw.c:828: error: 'ch1' undeclared (first use in this function) 
hw.c:828: error: 'ch2' undeclared (first use in this function) 
hw.c: At top level: 
hw.c:1204: error: expected '=', ',', ';', 'asm' or '__attribute__' before '__Pyx 
_PyInt_AsUnsignedLongLong' 
hw.c:1274: error: expected '=', ',', ';', 'asm' or '__attribute__' before '__Pyx 
_PyInt_AsLongLong' 
hw.c:1344: error: expected '=', ',', ';', 'asm' or '__attribute__' before '__Pyx 
_PyInt_AsSignedLongLong' 
hw.c: In function '__Pyx_InitStrings': 
hw.c:1564: warning: assignment makes pointer from integer without a cast 
hw.c: In function '__Pyx_PyInt_AsSize_t': 
hw.c:1669: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'val' 
hw.c:1669: error: 'val' undeclared (first use in this function) 
hw.c:1670: error: expected ')' before 'LONG_LONG' 
hw.c:1672: error: expected ')' before 'LONG_LONG' 

我在windows 7 64bit使用32bit python 2.7

+2

您的'gcc'命令缺少include目录和其他选项。 – 2012-07-31 03:53:44

+0

使Basile的评论更具体。确保Python.h可以被gcc包括在内以作为开始。 – Endophage 2012-07-31 03:54:40

+0

好吧,我用“gcc hw.c -IC:\ Python27 \ include”,结果如下:http://codepad.org/7uqvY7ui 它仍然失败并且抛出一个错误 – 2012-07-31 03:59:03

回答

3

看看documentation。根据我的经验,最简单的方法是使用distutils,因为它知道传递给编译器所需的所有信息(例如,头文件的位置,重要的库,用于创建合适的共享对象的编译器选项等)。 ,编写一个setup.py文件并不是非常困难。如果你不想安装库,只需执行python setup.py build并将共享对象复制到你想要的任何目录中(尽管你可能需要在build目录中挖掘一下以找到它)。