c++扩展python

http://docs.python.org/extending/building.html

http://docs.python.org/extending/extending.html



#include <Python.h>

int fact(int n) {
    return n*n;
}

PyObject *warp_fact(PyObject* self, PyObject *args) {
    int n,result;
    if (!PyArg_ParseTuple(args, "i:fact", &n)) {
        return NULL;
    }
    result = fact(n);
    return Py_BuildValue("i", result);
}

static PyMethodDef  exampleMethods[] = {
    {"fact", warp_fact, 1},
    {NULL, NULL},
};


extern "C"

void initaa() {

     PyObject* m;

    m = Py_InitModule("aa", exampleMethods);
}





makefile:

cc:=g++-4.6

cflags:= -I/usr/include/python2.7
cflags+= -fPIC -lpthread -lm -ldl -lutil /usr/lib/python2.7/config/libpython2.7.a

aa: aa.cpp
    $(cc) $(cflags) -shared -o $@ $<


你可能感兴趣的:(C++,python,null,扩展,makefile)