#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}
};
extern "C" __declspec(dllexport) void initpythonTest()
{
PyObject* m;
m = Py_InitModule("pythonTest", exampleMethods);
}
设置python的include和lib路径 编译为x64的dll文件,之
后将其后缀修改为pyd
.pyd文件并不是使用python编写而成,.pyd文件一般是其他语言编写的python扩展模块
extern "C",是为了避免编译器添加额外的东西
import pythonTest
pythonTest.fact(4)