您的位置:首页 > 编程语言 > C语言/C++

Python与C++互动编程

2015-11-17 09:59 423 查看

C++嵌入Python

实现原理



SKILL

能够实现在C++中嵌入对Python函数的调用、对象的创建、类的调用;

代码

#include <iostream>
#include "Python.h"
using namespace std;

int main(int argc, char *argv[])
{
int j;
int  i = 12;
Py_Initialize();
if (!Py_IsInitialized())
{
cout << "not INit" << endl;
getchar();
return -1;
}
PyObject *pModule = NULL;
PyObject *pFunc = NULL;
PyObject *pNum = NULL;
PyObject *pGet = NULL;
PyObject *pStu = NULL;
PyObject *pInstan = NULL;
PyDict_New();
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append('./')");
pModule = PyImport_ImportModule("hello");
if (!pModule)
{
cout << "not module" << endl;
getchar();
return -1;
}
pFunc = PyObject_GetAttrString(pModule, "hi");
if (!pFunc)
{
cout << "not func" << endl;
getchar();
return -1;
}

//pGet = PyInt_FromLong(i);
pGet = Py_BuildValue("(i)",i);
//pGet = PyTuple_New(1); //
//PyTuple_SetItem(pGet, 0, PyInt_FromLong(i));
//
//pNum = PyObject_CallFunction(pFunc, "i",1);
pNum = PyObject_CallObject(pFunc, pGet);
//pNum = PyEval_CallObject(pFunc, pGet);
//
j = PyInt_AsLong(pNum);
cout << j << endl;
Py_DECREF(pFunc);
pFunc = PyObject_GetAttrString(pModule, "Student");
if (!pFunc)
{
cout << "not func" << endl;
getchar();
return -1;
}
pStu = PyInstance_New(pFunc, NULL, NULL);
if (!pStu)
{
cout << "notstu" << endl;
getchar();
return -1;
}
PyObject_CallMethod(pStu, "SetNam","s","lilei");
PyObject_CallMethod(pStu, "print_name",NULL,NULL);
getchar();
Py_Finalize();
return 0;
}


遇到并解决的问题

编译时需要VS工具选项中调试-符号的符号自动加载来为编译文件自动加载符号;

项目的预编头文件需要加上WIN;

需要把py文件放到exe生成文件的同级目录下才能进行查找

未解决的问题

pGet = PyInt_FromLong(i)用这样的方式创建一个PyObject变量无法创建并导致后面的函数执行全部出错;

如果要获取Py文件中已经赋值了的对象,是否只能通过py文件中的函数才能获取得到?

C++扩展Python

实现原理



SKILL

能够实现在将C++的函数封装,转化成可被Python调用的模板、Python C API的函数应用;

代码

#include <iostream>
#include "Python.h"
using namespace std;

int numchan(int a)
{
return a + 1;
}

static PyObject * _numchan(PyObject *self, PyObject *argc)
{
int a;
int re;
PyArg_ParseTuple(argc, "i", &a);
re = numchan(a);
cout << re << endl;
return PyInt_FromLong(re);
}

static PyMethodDef numchanModuleMethods[] = {
{
"numchan",
_numchan,
METH_VARARGS,
""
},
{NULL,NULL,0,NULL}
};

PyMODINIT_FUNC initnumchan(void)
{
Py_InitModule("numchan", numchanModuleMethods);
}


遇到并解决的问题

函数初始化入口的名称必须和之前的封装函数的名称完全一致;

生成pyd文件的时候需要通过VS命令行编译生成;

封装的函数和该函数返回的对象必须都是PyObject;

未解决的问题

如果不使用生成pyd或者dll文件的方式进行扩展Python的话,那么扩展的具体实现是怎么样的?把C++转化成Python可调用的对象是什么对象?PyObject??然后Python调用的时候通过函数直接去调用吗?还是通过Import?

猜测是否是下图模式

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python C++