title: Cython: Segmentation Fault Using API Embedding Cython to C
body: <p>I'm trying to embed Cython code into C following <a href="https://github.com/hnakamur/exposing_cython_code_to_c/tree/master/mechanism2" rel="noreferrer">O'reilly Cython book</a> chapter 8. I found this paragraph on Cython's <a href="http://docs.cython.org/src/userguide/external_C_code.html#c-api-declarations" rel="noreferrer">documentation</a> but still don't know what should I do:</p>
  
  <blockquote>
    <p>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.</p>
  </blockquote>
  
  <p>I'm running Python 3.4, Cython 0.23 and GCC 5 on OS X. The source code are <code>transcendentals.pyx</code> and <code>main.c</code>:</p>
  
  <p><code>main.c</code></p>
  
  <pre><code>#include "transcendentals_api.h"
  #include &lt;math.h&gt;
  #include &lt;stdio.h&gt;
  
  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;
  }
  </code></pre>
  
  <p><code>transcendentals.pyx</code></p>
  
  <pre><code>cdef api double get_pi():
      return 3.1415926
  
  cdef api double get_e():
      print("calling get_e()")
      return 2.718281828
  </code></pre>
  
  <p>I'm compiling those files using <code>setup.py</code> and <code>Makefile</code>:</p>
  
  <p><code>setup.py</code>:</p>
  
  <pre><code>from distutils.core import setup
  from distutils.extension import Extension
  from Cython.Build import cythonize
  
  setup(
      ext_modules=cythonize([
          Extension("transcendentals", ["transcendentals.pyx"])
      ])
  )
  </code></pre>
  
  <p><code>Makefile</code></p>
  
  <pre><code>python-config=/Users/spacegoing/anaconda/bin/python3-config
  ldflags:=$(shell $(python-config) --ldflags)
  cflags:=$(shell $(python-config) --cflags)
  
  a.out: main.c transcendentals.so
      gcc-5 $(cflags) $(ldflags) transcendentals.c main.c
  
  transcendentals.so: setup.py transcendentals.pyx
      python setup.py build_ext --inplace
      cython transcendentals.pyx
  
  
  clean:
      rm -r a.out a.out.dSYM build transcendentals.[ch] transcendentals.so transcendentals_api.h
  </code></pre>
  
  <p>However, I came to error <code>Segmentation fault: 11</code>. Any idea can help with this? Thanks!</p>