Chris
Chris

Reputation: 383

Cython, OS X and link errors

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?

Upvotes: 1

Views: 478

Answers (1)

Iguananaut
Iguananaut

Reputation: 23306

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.

Upvotes: 2

Related Questions