1.C++库代码
#include <Python.h>
int Add(int x, int y)
{
return x + y;
}
int Del(int x, int y)
{
return x - y;
}
PyObject *WrappAdd(PyObject *self, PyObject *args)
{
int x, y;
if(!PyArg_ParseTuple(args, "ii", &x, &y))
{
return NULL;
}
return Py_BuildValue("i", Add(x, y));
}
PyObject *WrappDel(PyObject *self, PyObject *args)
{
int x, y;
if(!PyArg_ParseTuple(args, "ii", &x, &y))
{
return NULL;
}
return Py_BuildValue("i", Del(x, y));
}
static PyMethodDef test_methods[] = {
{"Add", WrappAdd, METH_VARARGS, "something"},
{"Del", WrappDel, METH_VARARGS, "something"},
{NULL,NULL}
};
extern "C"
void inittest1()
{
Py_InitModule("test1", test_methods);
}
2.编译C++库
$ g++ -fPIC -shared -o test1.so test.cpp -I$HOME/local/include/python2.7
3.python代码
import sys
import binascii
sys.path.append(".")
import test1
print test1.Add(1, 1)
4.运行程序
$ python example.py
5.运行结果
2