C++中嵌入python程序——參數(shù)傳遞,使用API接口,從函數(shù)到類
C++中嵌入python程序——使用API接口,從函數(shù)到類
上一篇博客介紹了C++中使用命令行模式調(diào)用python,但是感覺(jué)交互性相當(dāng)不足,本文介紹使用API接口調(diào)用python程序的函數(shù)和類對(duì)象。?
開(kāi)發(fā)環(huán)境為win7 64位,vs2010,python2.7.12?
首先需要配置環(huán)境,創(chuàng)建win32控制臺(tái)程序,然后對(duì)工程進(jìn)行配置。?
項(xiàng)目–>屬性–>vc++目錄–>包含目錄 添加python安裝目錄中include目錄?
項(xiàng)目–>屬性–>vc++目錄–>庫(kù)目錄 添加python安裝目錄中l(wèi)ibs目錄?
鏈接器–>輸入–>附加依賴項(xiàng) 添加python27.lib
注意,如果C++工程采用debug版本,需要將將 python安裝目錄里libs文件夾下的python27.lib文件復(fù)制,并將名字改成 python27_d.lib
完成上述配置后,我們開(kāi)始創(chuàng)建一個(gè)myclass.py的python文件,文件內(nèi)容如下:
#!/usr/bin/env?python #?-*-?coding:utf8?-*- class?hi_class: ????def?sayHi(self): ????????print?'Hi!?I?am?a?python?class!' def?hi_function(): ????print?'Hi!?I?am?a?python?function!'
上面創(chuàng)建了一個(gè)類和一個(gè)函數(shù),下面我們來(lái)編寫C++代碼
#include#include#include#include#includeusing?namespace?std;??
int?main()??
{
????Py_Initialize();?//?初始化,這是必須的,用來(lái)初始化python所需的環(huán)境
????if?(!Py_IsInitialized())
????????return?-1;
????//?導(dǎo)入模塊
????PyObject*?pModule?=??NULL;
????PyRun_SimpleString("import?sys");
????PyRun_SimpleString("sys.path.append('./')");
????pModule?=?PyImport_ImportModule("myclass");
????if?(!pModule)?{
????????printf("Cant?open?python?file!/n");
????????return?-1;
????}
????//?模塊的字典列表
????PyObject*?pDict?=?PyModule_GetDict(pModule);
????if?(!pDict)?{
????????printf("Cant?find?dictionary./n");
????????return?-1;
????//?演示函數(shù)調(diào)用
????cout<<"calling?python?function..."<<endl;
????PyObject*?pFunHi?=?PyDict_GetItemString(pDict,?"hi_function");
????PyObject_CallFunction(pFunHi,?NULL,?NULL);
????Py_DECREF(pFunHi);
????//演示類調(diào)用
????cout<<"calling?python?class..."<<endl;
????//?演示構(gòu)造一個(gè)Python對(duì)象,并調(diào)用Class的方法
????//?獲取hi_class類
????PyObject*?phi_class?=?PyDict_GetItemString(pDict,?"hi_class");
????if?(!phi_class?)?{
????????printf("Cant?find?phi_class?class./n");
????????return?-1;
????}
????//構(gòu)造hi_class的實(shí)例
????PyObject*?pInstance_hi_class?=?PyInstance_New(phi_class?,?NULL,?NULL);
????//如果python類中有初始化參數(shù),那么創(chuàng)建實(shí)例的時(shí)候需要將初始化參數(shù)以元組形式傳遞進(jìn)去(親測(cè)可用)PyObject*?pInstance_hi_class?=?PyInstance_New(phi_class?,?PyObject*類型的元組(需要在C++里創(chuàng)建python元組),?NULL);
????//C++中創(chuàng)建并初始化python元組示例如下兩行:
????//PyObject?*pArgs3?=?PyTuple_New(1);
????//PyTuple_SetItem(pArgs3,?0,?Py_BuildValue("i",?3));
????if?(!pInstance_hi_class)?{
????????printf("Cant?create?instance./n");
????????return?-1;
????}
????//調(diào)用hi_class類實(shí)例pInstance_hi_class里面的方法
????PyObject_CallMethod(phi_class,?"sayHi",?"O",?pInstance_hi_class?);
????//釋放
????Py_DECREF(phi_class);
????Py_DECREF(pInstance_hi_class?);
????Py_DECREF(pModule);
????Py_Finalize();?//?與初始化對(duì)應(yīng)
????system("pause");
????return?0;
}這樣,C++調(diào)用python函數(shù)和類的基本方法已經(jīng)介紹完畢。?
但是,又有一個(gè)問(wèn)題,如果我想在C++里面存儲(chǔ)python類(也就是python class作為C++的中間變量)該怎么實(shí)現(xiàn)呢??
如果你仔細(xì)看看C++里面的代碼,你就會(huì)發(fā)現(xiàn),其實(shí)C++程序中PyObject* 不正好可以存儲(chǔ)嗎?所以這個(gè)問(wèn)題不必?fù)?dān)心。?
我們重新開(kāi)始吧,對(duì)上面的程序重新做一下修改?
myclass.py
#!/usr/bin/env?python #?-*-?coding:utf8?-*- class?hi_class: ????def?sayHi(self): ????????print?'Hi!?I?am?a?python?class!' def?hi_function(): ????py_class?=?hi_class() ????return?py_class
C++程序
#include#include#include#include#includeusing?namespace?std;??
int?main()??
{
????Py_Initialize();?//?初始化,這是必須的,用來(lái)初始化python所需的環(huán)境
????if?(!Py_IsInitialized())
????????return?-1;
????//?導(dǎo)入模塊
????PyObject*?pModule?=??NULL;
????PyRun_SimpleString("import?sys");
????PyRun_SimpleString("sys.path.append('./')");
????pModule?=?PyImport_ImportModule("myclass");
????if?(!pModule)?{
????????printf("Cant?open?python?file!/n");
????????return?-1;
????}
????//?模塊的字典列表
????PyObject*?pDict?=?PyModule_GetDict(pModule);
????if?(!pDict)?{
????????printf("Cant?find?dictionary./n");
????????return?-1;
????//?函數(shù)調(diào)用
????cout<<"calling?python?program..."<<endl;
????PyObject*?pFunHi?=?PyDict_GetItemString(pDict,?"hi_function");
????//?利用python函數(shù)返回hi_class類(hi_function函數(shù)返回hi_class類,看我python程序)
????PyObject*?phi_class?=?PyObject_CallFunction(pFunHi,?NULL,?NULL);
????if?(!phi_class?)?{
????????printf("Cant?find?phi_class?class./n");
????????return?-1;
????}
????//構(gòu)造hi_class的實(shí)例
????PyObject*?pInstance_hi_class?=?PyInstance_New(phi_class?,?NULL,?NULL);
????if?(!pInstance_hi_class)?{
????????printf("Cant?create?instance./n");
????????return?-1;
????}
????//調(diào)用hi_class類實(shí)例pInstance_hi_class里面的方法
????PyObject_CallMethod(phi_class,?"sayHi",?"O",?pInstance_hi_class?);
????//釋放
????Py_DECREF(phi_class);
????Py_DECREF(pInstance_hi_class?);
????Py_DECREF(pFunHi);
????Py_DECREF(pModule);
????Py_Finalize();?//?與初始化對(duì)應(yīng)
????system("pause");
????return?0;
}看,我們已經(jīng)從python函數(shù)返回值獲取了python類,我們可以添加我們所需的任何功能了。比如,我們做機(jī)器學(xué)習(xí),訓(xùn)練好了自己的分類器并保存成了文件,然后我們就可以用python加載并返回給 C++,這樣我們就能在C++里面使用這個(gè)分類器了,很方便吧。





