python调用其他语言教程:

python调用其他语言教程:


调用C语言:

  注意要安装 python-dev (sudo apt-get install python-dev)  

#include <Python.h>

int fact(int n)
{
  if (n <= 1)
    return 1;
  else
    return n * fact(n - 1);
}

PyObject* wrap_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", wrap_fact, METH_VARARGS, "Caculate N!"},
  {NULL, NULL}
};

void initexample()
{
  PyObject* m;
  m = Py_InitModule("example", exampleMethods);
}

  gcc -fPIC test.c -o example.so -shared  -I/usr/include/python2.7 -I/usr/lib/python2.7/config  


原文地址:https://www.cnblogs.com/canbefree/p/4147224.html