用C语言写一个最简单的python扩展。

一. 上代码

  1. hello.c
#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);
}
  1. setup.py
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