Pythonからcallされるソースコード『py_hello.c』
#include "../../../include/python3.6m/Python.h"
static PyObject* c_helloworld(PyObject* self, PyObject* args)
{
printf("Hello World\n");
return Py_None;
}
static PyMethodDef myMethods[] = {
{ "helloworld", c_helloworld, METH_NOARGS, "Prints Hello World" },
{ NULL }
};
static struct PyModuleDef myModule = {
PyModuleDef_HEAD_INIT,
"myModule",
"Python3 C API Module(Sample 1)",
-1,
myMethods
};
PyMODINIT_FUNC PyInit_myModule(void)
{
return PyModule_Create(&myModule);
}
setup.py
from distutils.core import setup, Extension
setup(name = 'myModule', version = '1.0.0', \
ext_modules = [Extension('myModule', ['py_hello.c'])])
『py_hello.c』を呼ぶソースコード『hello.py』
import myModule
myModule.helloworld()