用C语言写一个最简单的python扩展。
#include<Python.h>
static PyObject *say_hello(PyObject *self, PyObject *args) {
return Py_BuildValue("s", "hello from c.");
}
static PyMethodDef methods[] = {
{"say_hello", say_hello, METH_VARARGS, "say hello."},
{NULL, NULL, 0, NULL}
};
static struct PyModuleDef module_def =
{
PyModuleDef_HEAD_INIT,
"hello",
"say hello",
-1,
methods
};
PyMODINIT_FUNC PyInit_hello(void) {
return PyModule_Create(&module_def);
}
from distutils.core import setup, Extension
module = Extension('hello', sources = ['hello.c'])
setup(name = 'say hello', version = '0.0.1', ext_modules = [module])
$ python setup.py build
注 此时想要调用需要把hello.so
共享库加到PYTHONPATH环境变量中。
#!/usr/bin/env python3
import hello
print(hello.say_hello())
Tue Nov 23 15:44:51 2021