title: gcc can't see my cython hello world function
body: <p>I'm trying some basic experiments with cython (with the ultimate goal being to make a library that I can use in other languages), following <a href="http://docs.cython.org/en/latest/src/userguide/external_C_code.html" rel="nofollow noreferrer">these docs</a>.  Trying to compile a basic hello world example, however, the compiler can't see my cython-defined function.</p>
  
  <p>The relevant code: </p>
  
  <p>hello.pyx: </p>
  
  <pre><code>cdef public api char* say_hi():
      return "hello from python!"
  </code></pre>
  
  <p>which I then compile into c with <code>cython hello.pyx</code></p>
  
  <p>sayhi.c: </p>
  
  <pre><code>#include &lt;Python.h&gt;
  #include "hello.h"
  
  int main() {
    Py_Initialize();
    char *hi = say_hi();
    printf("%s\n", hi);
    Py_Finalize();
  }
  </code></pre>
  
  <p>which I then attempt to compile into an executable with </p>
  
  <pre><code>gcc `python2-config --cflags --ldflags` -o compiledhi sayhi.c
  </code></pre>
  
  <p>which is just what I swiped from <a href="https://stackoverflow.com/a/34643949/4386239">this earlier SO answer</a>.  </p>
  
  <p>But compilation fails with the following error: </p>
  
  <pre><code>Undefined symbols for architecture x86_64:
    "_say_hi", referenced from:
        _main in sayhi-302608.o
  ld: symbol(s) not found for architecture x86_64
  clang: error: linker command failed with exit code 1 (use -v to see invocation)
  </code></pre>
  
  <p>If it matters, I'm running this all from within a pipenv shell using python 3.7, on a mac with the current version of xcode installed... </p>
  
  <p>Since I'm using python 3, and just in case it makes a difference, I also tried </p>
  
  <pre><code>gcc `python3-config --cflags --ldflags` -o compiledhi sayhi.c
  </code></pre>
  
  <p>just as a guess, but no dice.  </p>
  
  <p>The cython docs linked above describe an api mode as an alternative way to do this, which I also tried, by replacing the c code above with:</p>
  
  <pre><code>#include &lt;Python.h&gt;
  #include "hello_api.h"
  
  int main() {
    Py_Initialize();
    import_hello();
    char *hi = say_hi();
    printf("%s\n", hi);
    Py_Finalize();
  }
  </code></pre>
  
  <p>this compiles, but when I run it, it segfaults. </p>
  
  <p>Help?  I think I'm following the docs correctly, though I don't actually know C...</p>