Python C 拡張 - Himeyama/himeyama GitHub Wiki

#include <Python.h>

static
PyObject* hello(PyObject* self, PyObject* args){
    return PyUnicode_FromFormat("Hello, World!");
}

static PyMethodDef hello_methods[] = {
    {"hello", hello, METH_VARARGS},
    {NULL, NULL, 0, NULL}
};

static struct PyModuleDef mod_hello = {
    PyModuleDef_HEAD_INIT,
    "hello",
    NULL,
    -1,
    hello_methods
};

PyMODINIT_FUNC
PyInit_hello(void){
    return PyModule_Create(&mod_hello);
}
CC = gcc
PY_LIB = /home/hikari/anaconda3/lib
PY_INC = /home/hikari/anaconda3/include/python3.9

hello.so: hello.c
	$(CC) $< -I$(PY_INC) -L$(PY_LIB) -lpython3.9 -shared -o $@
⚠️ **GitHub.com Fallback** ⚠️