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 these docs. Trying to compile a basic hello world example, however, the compiler can't see my cython-defined function.
The relevant code:
hello.pyx:
cdef public api char* say_hi():
return "hello from python!"
which I then compile into c with cython hello.pyx
sayhi.c:
#include <Python.h>
#include "hello.h"
int main() {
Py_Initialize();
char *hi = say_hi();
printf("%s\n", hi);
Py_Finalize();
}
which I then attempt to compile into an executable with
gcc `python2-config --cflags --ldflags` -o compiledhi sayhi.c
which is just what I swiped from this earlier SO answer.
But compilation fails with the following error:
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)
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...
Since I'm using python 3, and just in case it makes a difference, I also tried
gcc `python3-config --cflags --ldflags` -o compiledhi sayhi.c
just as a guess, but no dice.
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:
#include <Python.h>
#include "hello_api.h"
int main() {
Py_Initialize();
import_hello();
char *hi = say_hi();
printf("%s\n", hi);
Py_Finalize();
}
this compiles, but when I run it, it segfaults.
Help? I think I'm following the docs correctly, though I don't actually know C...