擅长:python、mysql、java
<p>如果有人想知道它在Python 3中是如何工作的,下面是我作为一个Cython新手奋斗后的解决方案。</p>
<p><strong>main.c</strong></p>
<pre><code>#include <Python.h>
#include "caller.h"
int
main()
{
PyImport_AppendInittab("caller", PyInit_caller);
Py_Initialize();
PyImport_ImportModule("caller");
call_quack();
Py_Finalize();
return 0;
}
</code></pre>
<p><strong>呼叫者.pyx</strong></p>
<pre><code># cython: language_level=3
import sys
sys.path.insert(0, '')
from quacker import quack
cdef public void call_quack():
quack()
</code></pre>
<p><strong>嘎嘎声.py</strong></p>
<pre><code>def quack():
print("Quack!")
</code></pre>
<p>最后,这里是编译所有内容的Makefile:</p>
<pre><code>target=main
cybridge=caller
CC=gcc
CFLAGS= `python3-config --cflags`
LDFLAGS=`python3-config --ldflags`
all:
cython $(cybridge).pyx
$(CC) $(CFLAGS) -c *.c
$(CC) $(LDFLAGS) *.o -o $(target)
clean:
rm -f $(cybridge).{c,h,o} $(target).o $(target)
rm -rf __pycache__
</code></pre>