C++中嵌入python
#include
#include
using namespace std;
int main(int argc, char* argv[]) {
PyObject *pModule, *pDict, *pFunc;
PyObject *pArgs, *pArg, *pResult;
Py_Initialize();
// ...
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append('./')");
// ...
pModule = PyImport_ImportModule("callback");
if(pModule != NULL) {
pDict = PyModule_GetDict(pModule);
pFunc = PyDict_GetItemString(pDict, "hello");
if(pFunc && PyCallable_Check(pFunc)) {
pResult = PyObject_CallObject(pFunc, NULL);
if(pResult != NULL) {
cout << PyString_AsString(pResult) << endl;
Py_DECREF(pResult);
}
}
Py_XDECREF(pFunc);
Py_DECREF(pDict);
Py_DECREF(pModule);
}
// Py_DECREF(pArgs);
// Py_DECREF(pArg);
Py_Finalize();
return 0;
}
內容粗略,僅是記錄一下,有待更新。