1

I've followed a simple tutorial on Cython with the following steps:

  1. Make a simple python file, testme.py:

    print( "Hello there!!" )

  2. Create a c file from that using cython:

    cython -a testme.py

  3. Compile the resulting testme.c file with gcc:

    gcc -Os -I /usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framework/Versions/2.7/include/python2.7 -o testme testme.c -lpthread -lm -lutil -ldl

The result is quite a few lines like these:

Undefined symbols for architecture x86_64:
  "_PyCode_New", referenced from:
      _inittestme in testme-4ef125.o
  "_PyDict_New", referenced from:
      _inittestme in testme-4ef125.o
  "_PyDict_SetItem", referenced from:
      _inittestme in testme-4ef125.o
  "_PyErr_Clear", referenced from:
      _inittestme in testme-4ef125.o
      _main in testme-4ef125.o
  "_PyErr_Occurred", referenced from:
      _inittestme in testme-4ef125.o
      _main in testme-4ef125.o
  "_PyErr_Print", referenced from:
      _main in testme-4ef125.o
  "_PyErr_SetString", referenced from:
      _inittestme in testme-4ef125.o
  "_PyErr_WarnEx", referenced from:
      _inittestme in testme-4ef125.o
  "_PyExc_ImportError", referenced from:
      _inittestme in testme-4ef125.o
  "_PyExc_RuntimeError", referenced from:
      _inittestme in testme-4ef125.o

Clearly, there's a missing link library. I have the following

/usr/lib/libpython2.6.dylib -> ../../System/Library/Frameworks/Python.framework/Versions/2.6/Python

but adding that to the gcc command gives me a XXX not found error:

gcc -Os -I /usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framework/Versions/2.7/include/python2.7 -o hello testme.c -l/usr/lib/libpython2.6.dylib -lpthread -lm -lutil -ldl
ld: library not found for -l/usr/lib/libpython2.6.dylib

The path is correct. The library is there.

Any hints?

Chris
  • 171
  • 2
  • 8

1 Answers1

1

You need to link the correct libpython. E.g. -lpython2.7.

FWIW the cython command is a bit low-level (it only handles compilation of individual Cython modules to C), where as the cythonize command is a bit higher level, can work on entire packages, and also includes a --build option that handles compiling the C code. This is generally easier than trying to build the correct gcc command yourself.

Iguananaut
  • 21,810
  • 5
  • 50
  • 63
  • Thanks! Using the right lib did the trick. One thing, I don't see a --build option for cython. Were you referring to something else? – Chris Oct 26 '17 at 23:43
  • Glad it worked. The `--build` option is part of `cythonize` actually. I'll update my answer. – Iguananaut Oct 27 '17 at 11:53