使用C++扩展python

2014-11-19 0 685
使用C++扩展python
// demo.cpp : 定义控制台应用程序的入口点。
//

#include "Python.h"

int add(int a, int b)
{
	return (a+b);
}
static PyObject *
demo_add(PyObject *self, PyObject *args)
{
    int ret;
    int a, b;
    if (!PyArg_ParseTuple(args, "ii", &a, &b))
        return NULL;
    ret = add(a, b);
    return Py_BuildValue("i", ret);
}
static PyMethodDef demoMethods[] = {
    {"add",  demo_add, METH_VARARGS, "add two numbers."},
    {NULL, NULL, 0, NULL}        /* Sentinel */
};

static struct PyModuleDef demoModule = {
    PyModuleDef_HEAD_INIT,
    "demo",
    NULL,
    -1,
    demoMethods
};

PyMODINIT_FUNC
PyInit_demo(void)
{
    return PyModule_Create(&demoModule);
}

遇见资源网 python 使用C++扩展python http://www.ox520.com/14895.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务