Pythonからcallされるソースコード『py_hello.c』

// #include <Python.h> #include "../../../include/python3.6m/Python.h" // C function 'hello world' static PyObject* c_helloworld(PyObject* self, PyObject* args) { printf("Hello World\n"); return Py_None; } // myModule definition(python's name) static PyMethodDef myMethods[] = { { "helloworld", c_helloworld, METH_NOARGS, "Prints Hello World" }, { NULL } }; // myModule definition struct static struct PyModuleDef myModule = { PyModuleDef_HEAD_INIT, "myModule", "Python3 C API Module(Sample 1)", -1, myMethods }; // Initializes myModule 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()