编译pyx文件

时间:2016-02-29 18:47:08

标签: c python-2.7 gcc cython

我刚开始学习cython,这是我第一次尝试编译.pyx文件。 我试图在我的cmd中运行:

  

Cython fib.pyx

给了我fib.c文件。

然后我跑:

  

gcc -c fib.c -I / Python27 / include

它给了我fib.o文件,但是当我尝试

  

gcc -shared fib.o -o fib.so

它给了我这个例外:

fib.o:fib.c:(.text+0x72): undefined reference to `_imp__PyObject_GetAttr'
fib.o:fib.c:(.text+0xdb): undefined reference to `_imp__PyErr_Occurred'
fib.o:fib.c:(.text+0x26c): undefined reference to `_imp__PyString_FromString'
fib.o:fib.c:(.text+0x2ae): undefined reference to `_imp__PyNumber_Add'
fib.o:fib.c:(.text+0x31c): undefined reference to `_imp__PyMethod_Type'
fib.o:fib.c:(.text+0x418): undefined reference to `_imp__PyTuple_New'
fib.o:fib.c:(.text+0x53b): undefined reference to `_imp___Py_NoneStruct'
fib.o:fib.c:(.text+0x543): undefined reference to `_imp___Py_NoneStruct'
fib.o:fib.c:(.text+0x54d): undefined reference to `_imp___Py_NoneStruct'
fib.o:fib.c:(.text+0x79d): undefined reference to `_imp__PyNumber_Add'
fib.o:fib.c:(.text+0x874): undefined reference to `_imp__PyList_Type'
fib.o:fib.c:(.text+0x88b): undefined reference to `_imp__PyTuple_Type'
fib.o:fib.c:(.text+0x8c4): undefined reference to `_imp__PyObject_GetIter'
fib.o:fib.c:(.text+0x972): undefined reference to `_imp__PyList_Type'
fib.o:fib.c:(.text+0xa0b): undefined reference to `_imp__PyErr_Occurred'
fib.o:fib.c:(.text+0xa1b): undefined reference to `_imp__PyExc_StopIteration'
fib.o:fib.c:(.text+0xa2f): undefined reference to `_imp__PyExc_StopIteration'
fib.o:fib.c:(.text+0xa40): undefined reference to `_imp__PyErr_GivenExceptionMatches'
fib.o:fib.c:(.text+0xa53): undefined reference to `_imp__PyErr_Clear'
fib.o:fib.c:(.text+0xac3): undefined reference to `_imp__PyTuple_New'
fib.o:fib.c:(.text+0xcec): undefined reference to `_imp__PyString_FromString'
fib.o:fib.c:(.text+0xd2e): undefined reference to `_imp__PyNumber_Add'
fib.o:fib.c:(.text+0xd96): undefined reference to `_imp__PyTuple_New'
fib.o:fib.c:(.text+0xe9e): undefined reference to `_imp__PyString_FromString'
fib.o:fib.c:(.text+0xee0): undefined reference to `_imp__PyNumber_Add'
fib.o:fib.c:(.text+0xf48): undefined reference to `_imp__PyTuple_New'
fib.o:fib.c:(.text+0x107c): undefined reference to `_imp__PyMethod_Type'
fib.o:fib.c:(.text+0x11d9): undefined reference to `_imp__PyList_Type'
fib.o:fib.c:(.text+0x11f0): undefined reference to `_imp__PyTuple_Type'
fib.o:fib.c:(.text+0x1229): undefined reference to `_imp__PyObject_GetIter'
fib.o:fib.c:(.text+0x12d7): undefined reference to `_imp__PyList_Type'
fib.o:fib.c:(.text+0x1370): undefined reference to `_imp__PyErr_Occurred'
fib.o:fib.c:(.text+0x1380): undefined reference to `_imp__PyExc_StopIteration'
fib.o:fib.c:(.text+0x1394): undefined reference to `_imp__PyExc_StopIteration'
fib.o:fib.c:(.text+0x13a5): undefined reference to `_imp__PyErr_GivenExceptionMatches'
fib.o:fib.c:(.text+0x13b8): undefined reference to `_imp__PyErr_Clear'
fib.o:fib.c:(.text+0x1472): undefined reference to `_imp__PyMethod_Type'
fib.o:fib.c:(.text+0x153f): undefined reference to `_imp__PyTuple_New'
fib.o:fib.c:(.text+0x183e): undefined reference to `_imp__PyMethod_Type'
fib.o:fib.c:(.text+0x19c0): undefined reference to `_imp___Py_NoneStruct'
fib.o:fib.c:(.text+0x19c8): undefined reference to `_imp___Py_NoneStruct'
fib.o:fib.c:(.text+0x19d2): undefined reference to `_imp___Py_NoneStruct'
fib.o:fib.c:(.text+0x1c16): undefined reference to `_imp__PyObject_GC_Track'
fib.o:fib.c:(.text+0x1c67): undefined reference to `_imp__PyObject_GC_UnTrack'
fib.o:fib.c:(.text+0x1d5b): undefined reference to `_imp___Py_NoneStruct'
fib.o:fib.c:(.text+0x1d63): undefined reference to `_imp___Py_NoneStruct'
fib.o:fib.c:(.text+0x1d6d): undefined reference to `_imp___Py_NoneStruct'
fib.o:fib.c:(.text+0x1e91): undefined reference to `_imp__PyCode_New'
fib.o:fib.c:(.text+0x1ee1): undefined reference to `_imp__PyTuple_Pack'
fib.o:fib.c:(.text+0x1f31): undefined reference to `_imp__PyTuple_Pack'
fib.o:fib.c:(.text+0x1fd2): undefined reference to `_imp__PyTuple_Pack'
fib.o:fib.c:(.text+0x20a4): undefined reference to `_imp__PyCode_New'
fib.o:fib.c:(.text+0x2185): undefined reference to `_imp__PyTuple_New'
fib.o:fib.c:(.text+0x21cc): undefined reference to `_imp__PyString_FromStringAndSize'
fib.o:fib.c:(.text+0x224f): undefined reference to `_imp__Py_InitModule4'
fib.o:fib.c:(.text+0x22a4): undefined reference to `_imp__PyModule_GetDict'
fib.o:fib.c:(.text+0x22ef): undefined reference to `_imp__PyImport_AddModule'
fib.o:fib.c:(.text+0x2341): undefined reference to `_imp__PyObject_SetAttrString'
fib.o:fib.c:(.text+0x23ae): undefined reference to `_imp__PyObject_SetAttrString'
fib.o:fib.c:(.text+0x2423): undefined reference to `_imp__PyType_Ready'
fib.o:fib.c:(.text+0x24bf): undefined reference to `_imp__PyDict_SetItem'
fib.o:fib.c:(.text+0x252b): undefined reference to `_imp__PyCFunction_NewEx'
fib.o:fib.c:(.text+0x2577): undefined reference to `_imp__PyDict_SetItem'
fib.o:fib.c:(.text+0x25cb): undefined reference to `_imp__PyDict_New'
fib.o:fib.c:(.text+0x2614): undefined reference to `_imp__PyDict_SetItem'
fib.o:fib.c:(.text+0x26fe): undefined reference to `_imp__PyErr_Occurred'
fib.o:fib.c:(.text+0x2709): undefined reference to `_imp__PyExc_ImportError'
fib.o:fib.c:(.text+0x271b): undefined reference to `_imp__PyErr_SetString'
fib.o:fib.c:(.text+0x2756): undefined reference to `_imp__PyExc_NameError'
fib.o:fib.c:(.text+0x276c): undefined reference to `_imp__PyErr_Format'
fib.o:fib.c:(.text+0x278d): undefined reference to `_imp__PyDict_GetItem'
fib.o:fib.c:(.text+0x27fb): undefined reference to `_imp__PyObject_Call'
fib.o:fib.c:(.text+0x2807): undefined reference to `_imp___PyThreadState_Current'
fib.o:fib.c:(.text+0x281a): undefined reference to `_imp___Py_CheckRecursionLimit'
fib.o:fib.c:(.text+0x2834): undefined reference to `_imp___Py_CheckRecursiveCall'
fib.o:fib.c:(.text+0x286a): undefined reference to `_imp___PyThreadState_Current'
fib.o:fib.c:(.text+0x2888): undefined reference to `_imp__PyErr_Occurred'
fib.o:fib.c:(.text+0x289b): undefined reference to `_imp__PyExc_SystemError'
fib.o:fib.c:(.text+0x28ad): undefined reference to `_imp__PyErr_SetString'
fib.o:fib.c:(.text+0x28d4): undefined reference to `_imp___PyThreadState_Current'
fib.o:fib.c:(.text+0x28e7): undefined reference to `_imp___Py_CheckRecursionLimit'
fib.o:fib.c:(.text+0x2901): undefined reference to `_imp___Py_CheckRecursiveCall'
fib.o:fib.c:(.text+0x2930): undefined reference to `_imp___PyThreadState_Current'
fib.o:fib.c:(.text+0x294e): undefined reference to `_imp__PyErr_Occurred'
fib.o:fib.c:(.text+0x2961): undefined reference to `_imp__PyExc_SystemError'
fib.o:fib.c:(.text+0x2973): undefined reference to `_imp__PyErr_SetString'
fib.o:fib.c:(.text+0x298c): undefined reference to `_imp__PyTuple_New'
fib.o:fib.c:(.text+0x2a16): undefined reference to `_imp__PyCFunction_Type'
fib.o:fib.c:(.text+0x2a51): undefined reference to `_imp__PyType_IsSubtype'
fib.o:fib.c:(.text+0x2ac8): undefined reference to `_imp__PyImport_AddModule'
fib.o:fib.c:(.text+0x2aff): undefined reference to `_imp__PyObject_GetAttrString'
fib.o:fib.c:(.text+0x2b22): undefined reference to `_imp__PyExc_TypeError'
fib.o:fib.c:(.text+0x2b38): undefined reference to `_imp__PyErr_Format'
fib.o:fib.c:(.text+0x2b5e): undefined reference to `_imp__PyExc_TypeError'
fib.o:fib.c:(.text+0x2b74): undefined reference to `_imp__PyErr_Format'
fib.o:fib.c:(.text+0x2b80): undefined reference to `_imp__PyExc_AttributeError'
fib.o:fib.c:(.text+0x2b8a): undefined reference to `_imp__PyErr_ExceptionMatches'
fib.o:fib.c:(.text+0x2b97): undefined reference to `_imp__PyErr_Clear'
fib.o:fib.c:(.text+0x2ba4): undefined reference to `_imp__PyType_Ready'
fib.o:fib.c:(.text+0x2bc8): undefined reference to `_imp__PyObject_SetAttrString'
fib.o:fib.c:(.text+0x2c7d): undefined reference to `_imp__PyString_FromString'
fib.o:fib.c:(.text+0x2ca5): undefined reference to `_imp___Py_NoneStruct'
fib.o:fib.c:(.text+0x2caf): undefined reference to `_imp___Py_NoneStruct'
fib.o:fib.c:(.text+0x2cb6): undefined reference to `_imp___Py_NoneStruct'
fib.o:fib.c:(.text+0x2ce7): undefined reference to `_imp___Py_NoneStruct'
fib.o:fib.c:(.text+0x2d5c): undefined reference to `_imp__PyString_InternFromString'
fib.o:fib.c:(.text+0x2dc7): undefined reference to `_imp__PyExc_TypeError'
fib.o:fib.c:(.text+0x2dd9): undefined reference to `_imp__PyErr_SetString'
fib.o:fib.c:(.text+0x2e80): undefined reference to `_imp__PyExc_TypeError'
fib.o:fib.c:(.text+0x2e92): undefined reference to `_imp__PyErr_SetString'
fib.o:fib.c:(.text+0x2f08): undefined reference to `_imp___Py_NoneStruct'
fib.o:fib.c:(.text+0x2f3a): undefined reference to `_imp__PyDict_New'
fib.o:fib.c:(.text+0x2f8b): undefined reference to `_imp__PyExc_TypeError'
fib.o:fib.c:(.text+0x2f9d): undefined reference to `_imp__PyErr_SetString'
fib.o:fib.c:(.text+0x2fc8): undefined reference to `_imp__PyExc_TypeError'
fib.o:fib.c:(.text+0x2fda): undefined reference to `_imp__PyErr_SetString'
fib.o:fib.c:(.text+0x3056): undefined reference to `_imp___Py_NoneStruct'
fib.o:fib.c:(.text+0x3060): undefined reference to `_imp___Py_NoneStruct'
fib.o:fib.c:(.text+0x3067): undefined reference to `_imp___Py_NoneStruct'
fib.o:fib.c:(.text+0x3086): undefined reference to `_imp___Py_NoneStruct'
fib.o:fib.c:(.text+0x313d): undefined reference to `_imp___Py_NoneStruct'
fib.o:fib.c:(.text+0x3147): more undefined references to `_imp___Py_NoneStruct' follow
fib.o:fib.c:(.text+0x3163): undefined reference to `_imp__PyExc_TypeError'
fib.o:fib.c:(.text+0x3175): undefined reference to `_imp__PyErr_SetString'
fib.o:fib.c:(.text+0x321e): undefined reference to `_imp___Py_NoneStruct'
fib.o:fib.c:(.text+0x3244): undefined reference to `_imp___Py_NoneStruct'
fib.o:fib.c:(.text+0x324e): undefined reference to `_imp___Py_NoneStruct'
fib.o:fib.c:(.text+0x326a): undefined reference to `_imp__PyExc_TypeError'
fib.o:fib.c:(.text+0x327c): undefined reference to `_imp__PyErr_SetString'
fib.o:fib.c:(.text+0x3325): undefined reference to `_imp___Py_NoneStruct'
fib.o:fib.c:(.text+0x334b): undefined reference to `_imp___Py_NoneStruct'
fib.o:fib.c:(.text+0x3370): undefined reference to `_imp__PyExc_TypeError'
fib.o:fib.c:(.text+0x3382): undefined reference to `_imp__PyErr_SetString'
fib.o:fib.c:(.text+0x3406): undefined reference to `_imp__PyDict_New'
fib.o:fib.c:(.text+0x3451): undefined reference to `_imp__PyString_FromString'
fib.o:fib.c:(.text+0x3466): undefined reference to `_imp___PyObject_GC_New'
fib.o:fib.c:(.text+0x358f): undefined reference to `_imp__PyObject_GC_Track'
fib.o:fib.c:(.text+0x3969): undefined reference to `_imp__PyMem_Free'
fib.o:fib.c:(.text+0x398d): undefined reference to `_imp__PyObject_GC_UnTrack'
fib.o:fib.c:(.text+0x39a4): undefined reference to `_imp__PyObject_ClearWeakRefs'
fib.o:fib.c:(.text+0x39bc): undefined reference to `_imp__PyObject_GC_Del'
fib.o:fib.c:(.text+0x3cab): undefined reference to `_imp__PyMethod_New'
fib.o:fib.c:(.text+0x3cb4): undefined reference to `_imp___Py_NoneStruct'
fib.o:fib.c:(.text+0x3cd9): undefined reference to `_imp__PyMethod_New'
fib.o:fib.c:(.text+0x3cf1): undefined reference to `_imp__PyString_AsString'
fib.o:fib.c:(.text+0x3d0c): undefined reference to `_imp__PyString_FromFormat'
fib.o:fib.c:(.text+0x3d2f): undefined reference to `_imp__PyCFunction_Call'
fib.o:fib.c:(.text+0x3d3e): undefined reference to `_imp__PyCFunction_Call'
fib.o:fib.c:(.text+0x3d7c): undefined reference to `_imp__PyCFunction_Type'
fib.o:fib.c:(.text+0x3db7): undefined reference to `_imp__PyType_IsSubtype'
fib.o:fib.c:(.text+0x3e81): undefined reference to `_imp__PyList_New'
fib.o:fib.c:(.text+0x3ea4): undefined reference to `_imp__PyModule_GetDict'
fib.o:fib.c:(.text+0x3eb9): undefined reference to `_imp__PyDict_New'
fib.o:fib.c:(.text+0x3eda): undefined reference to `_imp__PyInt_FromLong'
fib.o:fib.c:(.text+0x3f1d): undefined reference to `_imp__PyObject_CallFunctionObjArgs'
fib.o:fib.c:(.text+0x4182): undefined reference to `_imp__PyMem_Malloc'
fib.o:fib.c:(.text+0x42b0): undefined reference to `_imp__PyMem_Realloc'
fib.o:fib.c:(.text+0x438a): undefined reference to `_imp__PyString_FromString'
fib.o:fib.c:(.text+0x43c3): undefined reference to `_imp__PyString_FromFormat'
fib.o:fib.c:(.text+0x43d5): undefined reference to `_imp__PyString_FromString'
fib.o:fib.c:(.text+0x4469): undefined reference to `_imp__PyCode_New'
fib.o:fib.c:(.text+0x45ae): undefined reference to `_imp___PyThreadState_Current'
fib.o:fib.c:(.text+0x45cb): undefined reference to `_imp__PyFrame_New'
fib.o:fib.c:(.text+0x45ec): undefined reference to `_imp__PyTraceBack_Here'
fib.o:fib.c:(.text+0x465c): undefined reference to `_imp__PySys_GetObject'
fib.o:fib.c:(.text+0x466c): undefined reference to `_imp__PyExc_RuntimeError'
fib.o:fib.c:(.text+0x467e): undefined reference to `_imp__PyErr_SetString'
fib.o:fib.c:(.text+0x46d5): undefined reference to `_imp__PyFile_SoftSpace'
fib.o:fib.c:(.text+0x46ee): undefined reference to `_imp__PyFile_WriteString'
fib.o:fib.c:(.text+0x4720): undefined reference to `_imp__PyFile_WriteObject'
fib.o:fib.c:(.text+0x4748): undefined reference to `_imp__PyString_AsString'
fib.o:fib.c:(.text+0x4758): undefined reference to `_imp__PyString_Size'
fib.o:fib.c:(.text+0x4798): undefined reference to `_imp__PyFile_SoftSpace'
fib.o:fib.c:(.text+0x47cc): undefined reference to `_imp__PyFile_WriteString'
fib.o:fib.c:(.text+0x47e7): undefined reference to `_imp__PyFile_SoftSpace'
fib.o:fib.c:(.text+0x4889): undefined reference to `_imp__PyFile_SoftSpace'
fib.o:fib.c:(.text+0x48a2): undefined reference to `_imp__PyFile_WriteString'
fib.o:fib.c:(.text+0x48c4): undefined reference to `_imp__PyFile_WriteObject'
fib.o:fib.c:(.text+0x48df): undefined reference to `_imp__PyFile_WriteString'
fib.o:fib.c:(.text+0x4977): undefined reference to `_imp__PyOS_snprintf'
fib.o:fib.c:(.text+0x497e): undefined reference to `_imp__Py_GetVersion'
fib.o:fib.c:(.text+0x499f): undefined reference to `_imp__PyOS_snprintf'
fib.o:fib.c:(.text+0x49ed): undefined reference to `_imp__PyOS_snprintf'
fib.o:fib.c:(.text+0x4a0d): undefined reference to `_imp__PyErr_WarnEx'
fib.o:fib.c:(.text+0x4a57): undefined reference to `_imp__PyUnicodeUCS2_DecodeUTF8'
fib.o:fib.c:(.text+0x4a7b): undefined reference to `_imp__PyString_InternFromString'
fib.o:fib.c:(.text+0x4aa1): undefined reference to `_imp__PyString_FromStringAndSize'
fib.o:fib.c:(.text+0x4afe): undefined reference to `_imp__PyByteArray_Type'
fib.o:fib.c:(.text+0x4b0e): undefined reference to `_imp__PyByteArray_Type'
fib.o:fib.c:(.text+0x4b1a): undefined reference to `_imp__PyType_IsSubtype'
fib.o:fib.c:(.text+0x4b2f): undefined reference to `_imp__PyByteArray_Type'
fib.o:fib.c:(.text+0x4b3f): undefined reference to `_imp__PyByteArray_Type'
fib.o:fib.c:(.text+0x4b4b): undefined reference to `_imp__PyType_IsSubtype'
fib.o:fib.c:(.text+0x4b83): undefined reference to `_imp__PyByteArray_Type'
fib.o:fib.c:(.text+0x4b93): undefined reference to `_imp__PyByteArray_Type'
fib.o:fib.c:(.text+0x4b9f): undefined reference to `_imp__PyType_IsSubtype'
fib.o:fib.c:(.text+0x4bd8): undefined reference to `_imp___PyByteArray_empty_string'
fib.o:fib.c:(.text+0x4bf3): undefined reference to `_imp__PyString_AsStringAndSize'
fib.o:fib.c:(.text+0x4c1c): undefined reference to `_imp___Py_TrueStruct'
fib.o:fib.c:(.text+0x4c2d): undefined reference to `_imp___Py_ZeroStruct'
fib.o:fib.c:(.text+0x4c40): undefined reference to `_imp___Py_NoneStruct'
fib.o:fib.c:(.text+0x4c5f): undefined reference to `_imp__PyObject_IsTrue'
collect2.exe: error: ld returned 1 exit status

我会感激任何帮助。

2 个答案:

答案 0 :(得分:2)

我总是用一行gcc编译我的cython:

gcc -shared -Wall -O3 -I Python27/include -L Python27/libs -o fib.so fib.c -l python27

你看,包括python的头文件(-I)还不够。您还必须包含python库(-L)的位置,然后包含要用作该库(-l)的文件的名称。

快乐的Cythonizing!

答案 1 :(得分:1)

还有另一种解决方案可能会在更大的项目中派上用场。您可以在python中创建一个配置文件,而不是在命令行中编写gcc调用,cython将使用该文件自动生成C或C ++代码并使用适当的标志调用gcc编译器。

只需创建一个文件setup.py(或您喜欢的文件名)。然后填写:

from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize

ext = Extension("fib",
            sources=["fib.pyx"],
            language="c++",
            include_dirs=[],
            libraries=[],
            extra_link_args=[])

setup(ext_modules = cythonize([ext]))

然后在终端python setup.py build_ext --inplace

这将生成一个fib.cpp文件,并将其编译为共享对象文件fib.so以及目标文件fib.o(最后一个名为build的文件夹) 。 之后,您import fib个文件中已经*.py了。