2016-02-29 94 views
0

我剛開始學習cython,這是我第一次嘗試編譯.pyx文件。 我想在我的CMD運行:編譯pyx文件

用Cython fib.pyx

這給了我fib.c文件。

然後我運行:

GCC -c fib.c -I/Python27 /包括

這給了我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 

我將不勝感激任何幫助。

+0

你錯過了'-l'的某處鏈接到python庫。 –

+2

[如何從Cython用gcc編譯.c代碼]可能的重複(http://stackoverflow.com/questions/6985109/how-to-compile-c-code-from-cython-with-gcc) –

回答

2

我總是編譯我用Cython用gcc的一行:

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

你看,包括頭文件(-I)爲Python是不夠的。您還必須包含python庫的位置(-L),然後包含用作該庫的文件的名稱(-l)。

快樂Cythonizing!

1

還有另一種解決方案可能適用於更大的項目。您可以使用python創建一個配置文件,而不用編寫自己的gcc調用命令行,您可以使用它來自動生成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文件夾中)。 之後,您的*.py文件中已經有import fib了。