Llamar código python desde c a través de cython
Entonces me gustaría llamar a un código de Python desde c a través de cython. Me las arreglé para llamar al código cython desde c. Y también puedo llamar al código python desde cython. Pero cuando lo agrego todo, faltan algunas cosas.
Aquí está mi código de Python (quacker.pyx
):
def quack():
print "Quack!"
Aquí está mi "puente" de cython (caller.pyx
):
from quacker import quack
cdef public void call_quack():
quack()
Y aquí está el código c (main.c
):
#include <Python.h>
#include "caller.h"
int main() {
Py_Initialize();
initcaller();
call_quack();
Py_Finalize();
return 0;
}
Cuando ejecuto esto obtengo esta excepción:
Exception NameError: "name 'quack' is not defined" in 'caller.call_quack' ignored
Las piezas faltantes que sospecho:
No he llamadoinitquacker()
No he incluidoquacker.h
Cython no produjo ningunaquacker.h
- only quacker.c
caller.c
no importaquacker.h
o llamarinitquacker()
No estoy realmente seguro de que sea posible hacer lo que intento hacer, pero me parece que debería ser así. Me encantaría escuchar cualquier comentario que pueda tener.
Editar:
Así es como cythonize / compile / link / run:
$ cython *.pyx
$ cc -c *.c -I/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7
$ cc -L/System/Library/Frameworks/Python.framework/Versions/2.7/lib -L/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/config -lpython2.7 -ldl *.o -o main
$ ./main