Cython compiling/linking fails on Mac OS X 10.8

南笙酒味 提交于 2019-12-10 15:25:15

问题


I'm a newbee to python and try to play around with Cython with a simple example founded in http://blog.perrygeo.net/2008/04/19/a-quick-cython-introduction/ The cython file is named c1.pyx

Then run cython c1.pyx, it was fine. Then run gcc -c -fPIC -I/usr/include/python2.7/ c1.c, it was fine too. But then run gcc -shared c1.o -o c1.so, it didn't go through.

Can anyone shed some light on this?

Here is the error message:

Undefined symbols for architecture x86_64:
  "_PyCFunction_NewEx", referenced from:
      _initc1 in cc5xSgOI.o
  "_PyCode_New", referenced from:
      ___Pyx_AddTraceback in cc5xSgOI.o
      _initc1 in cc5xSgOI.o
  "_PyDict_GetItem", referenced from:
      ___pyx_pw_2c1_1great_circle in cc5xSgOI.o
  "_PyDict_New", referenced from:
      _initc1 in cc5xSgOI.o
  "_PyDict_Next", referenced from:
      ___pyx_pw_2c1_1great_circle in cc5xSgOI.o
  "_PyDict_Size", referenced from:
      ___pyx_pw_2c1_1great_circle in cc5xSgOI.o
  "_PyErr_Clear", referenced from:
      ___Pyx_GetName in cc5xSgOI.o
  "_PyErr_Format", referenced from:
      ___Pyx_RaiseArgtupleInvalid.constprop.5 in cc5xSgOI.o
      ___pyx_pw_2c1_1great_circle in cc5xSgOI.o
  "_PyErr_Occurred", referenced from:
      ___pyx_pw_2c1_1great_circle in cc5xSgOI.o
      _initc1 in cc5xSgOI.o
  "_PyErr_SetObject", referenced from:
      ___Pyx_GetName in cc5xSgOI.o
  "_PyErr_SetString", referenced from:
      _initc1 in cc5xSgOI.o
  "_PyErr_WarnEx", referenced from:
      _initc1 in cc5xSgOI.o
  "_PyExc_ImportError", referenced from:
      _initc1 in cc5xSgOI.o
  "_PyExc_NameError", referenced from:
      ___Pyx_GetName in cc5xSgOI.o
  "_PyExc_TypeError", referenced from:
      ___Pyx_RaiseArgtupleInvalid.constprop.5 in cc5xSgOI.o
      ___pyx_pw_2c1_1great_circle in cc5xSgOI.o
  "_PyFloat_AsDouble", referenced from:
      ___pyx_pw_2c1_1great_circle in cc5xSgOI.o
  "_PyFloat_FromDouble", referenced from:
      ___pyx_pw_2c1_1great_circle in cc5xSgOI.o
  "_PyFloat_Type", referenced from:
      ___pyx_pw_2c1_1great_circle in cc5xSgOI.o
  "_PyFrame_New", referenced from:
      ___Pyx_AddTraceback in cc5xSgOI.o
  "_PyImport_AddModule", referenced from:
      _initc1 in cc5xSgOI.o
  "_PyInt_FromLong", referenced from:
      _initc1 in cc5xSgOI.o
  "_PyList_New", referenced from:
      _initc1 in cc5xSgOI.o
  "_PyMem_Malloc", referenced from:
      ___Pyx_AddTraceback in cc5xSgOI.o
  "_PyMem_Realloc", referenced from:
      ___Pyx_AddTraceback in cc5xSgOI.o
  "_PyModule_GetDict", referenced from:
      ___Pyx_AddTraceback in cc5xSgOI.o
      _initc1 in cc5xSgOI.o
  "_PyNumber_Add", referenced from:
      ___pyx_pw_2c1_1great_circle in cc5xSgOI.o
  "_PyNumber_Multiply", referenced from:
      ___pyx_pw_2c1_1great_circle in cc5xSgOI.o
  "_PyOS_snprintf", referenced from:
      _initc1 in cc5xSgOI.o
  "_PyObject_Call", referenced from:
      ___pyx_pw_2c1_1great_circle in cc5xSgOI.o
  "_PyObject_CallFunctionObjArgs", referenced from:
      _initc1 in cc5xSgOI.o
  "_PyObject_GetAttr", referenced from:
      ___Pyx_GetName in cc5xSgOI.o
      ___pyx_pw_2c1_1great_circle in cc5xSgOI.o
  "_PyObject_GetAttrString", referenced from:
      _initc1 in cc5xSgOI.o
  "_PyObject_SetAttr", referenced from:
      _initc1 in cc5xSgOI.o
  "_PyObject_SetAttrString", referenced from:
      _initc1 in cc5xSgOI.o
  "_PyString_AsString", referenced from:
      ___pyx_pw_2c1_1great_circle in cc5xSgOI.o
  "_PyString_FromFormat", referenced from:
      ___Pyx_AddTraceback in cc5xSgOI.o
  "_PyString_FromString", referenced from:
      ___Pyx_AddTraceback in cc5xSgOI.o
  "_PyString_FromStringAndSize", referenced from:
      _initc1 in cc5xSgOI.o
  "_PyString_InternFromString", referenced from:
      _initc1 in cc5xSgOI.o
  "_PyString_Type", referenced from:
      ___pyx_pw_2c1_1great_circle in cc5xSgOI.o
  "_PyTraceBack_Here", referenced from:
      ___Pyx_AddTraceback in cc5xSgOI.o
  "_PyTuple_New", referenced from:
      ___pyx_pw_2c1_1great_circle in cc5xSgOI.o
      _initc1 in cc5xSgOI.o
  "_PyUnicodeUCS2_DecodeUTF8", referenced from:
      _initc1 in cc5xSgOI.o
  "_Py_GetVersion", referenced from:
      _initc1 in cc5xSgOI.o
  "_Py_InitModule4_64", referenced from:
      _initc1 in cc5xSgOI.o
  "__PyString_Eq", referenced from:
      ___pyx_pw_2c1_1great_circle in cc5xSgOI.o
  "__PyThreadState_Current", referenced from:
      ___Pyx_AddTraceback in cc5xSgOI.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status

回答1:


You have to include -lpython2.7 (or whatever version of Python you're using) on the linker command-line.

The easiest way to use Cython is with a distutils setup script, though, as it will do all of the nitty-gritty compiler commands for you.

See also: Cython docs for building, Cython docs for compiling.



来源:https://stackoverflow.com/questions/12649689/cython-compiling-linking-fails-on-mac-os-x-10-8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!