使用C++创建Pyd文件扩展Python模块:1、需要说明的是Pyd文件其实就是dll,为了python能正常调用,这个dll规定了导出函数的一些规则。下面代码简单演示使用c++创建一个函数,并使用Python调用该模块。
(注意需要添加python的head路径和lib路径,具体操作,可见下面的视频链接)
#include
#include
//需要绑定的方法 static PyObject* PrintHello(PyObject *self, PyObject *args) { std::cout << "Hello, I am form c++" << std::endl; //测试一下使用c++/clr,让python调用 // System::Windows::Forms::Form^ TestDotNetWindows = gcnew System::Windows::Forms::Form(); // TestDotNetWindows->ShowDialog(); Py_INCREF(Py_None); return Py_None; } //描述方法 static PyMethodDef Methods[] = { {"PrintHelloFn", PrintHello, METH_VARARGS, "文档字符串"}, {NULL, NULL} }; //初始化模块 PyMODINIT_FUNC initPrintHello(void) { Py_InitModule("PrintHello", Methods); }