I'm trying to embed Cython code into C following O'reilly Cython book chapter 8. I found this paragraph on Cython's documentation but still don't know what should I do:
If the C code wanting to use these functions is part of more than one shared library or executable, then import_modulename() function needs to be called in each of the shared libraries which use these functions. If you crash with a segmentation fault (SIGSEGV on linux) when calling into one of these api calls, this is likely an indication that the shared library which contains the api call which is generating the segmentation fault does not call the import_modulename() function before the api call which crashes.
I'm running Python 3.4, Cython 0.23 and GCC 5 on OS X. The source code are transcendentals.pyx
and main.c
:
main.c
#include "transcendentals_api.h"
#include <math.h>
#include <stdio.h>int main(int argc, char **argv)
{Py_SetPythonHome(L"/Users/spacegoing/anaconda");Py_Initialize();import_transcendentals();printf("pi**e: %f\n", pow(get_pi(), get_e()));Py_Finalize();return 0;
}
transcendentals.pyx
cdef api double get_pi():return 3.1415926cdef api double get_e():print("calling get_e()")return 2.718281828
I'm compiling those files using setup.py
and Makefile
:
setup.py
:
from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonizesetup(ext_modules=cythonize([Extension("transcendentals", ["transcendentals.pyx"])])
)
Makefile
python-config=/Users/spacegoing/anaconda/bin/python3-config
ldflags:=$(shell $(python-config) --ldflags)
cflags:=$(shell $(python-config) --cflags)a.out: main.c transcendentals.sogcc-5 $(cflags) $(ldflags) transcendentals.c main.ctranscendentals.so: setup.py transcendentals.pyxpython setup.py build_ext --inplacecython transcendentals.pyxclean:rm -r a.out a.out.dSYM build transcendentals.[ch] transcendentals.so transcendentals_api.h
However, I came to error Segmentation fault: 11
. Any idea can help with this? Thanks!