| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #include "Python.h" PyObject* hello(PyObject* self, PyObject* args){ const char * str; if (! PyArg_ParseTuple(args, "s", &str)) return NULL; return Py_BuildValue("s",str); } static PyMethodDef helloMethods[] = { {"hello", hello, METH_VARARGS, "hello comments"}, {NULL, NULL, 0, NULL} }; PyMODINIT_FUNC inithello(void){ Py_InitModule("hello", helloMethods); } |
| 1 2 3 4 5 6 7 8 9 10 | all: hello.so hello.so: hello.o g++ -shared hello.o -o hello.so hello.o: hello.cpp g++ -fPIC -c -I /usr/include/python2.6/ -o hello.o hello.cpp clean: rm -f hello.o hello.so |
| 1 2 3 4 5 | #!/usr/bin/env python import hello print hello.hello("world") |