[英]Including 3rd party python module in c++ application
我正在嘗試構建一個應用程序來解析c ++中的netCDF4文件。
我做了什么:
我需要什么幫助:
我知道的:
感謝您的任何建議。
歡迎來到社區! 我希望我正確理解了這個問題-將python模塊導入c ++嗎? 如果是這樣,請嘗試以下教程: 在C / C ++中嵌入Python:第一部分 。
基本上,換句話說,不要使用代碼轉換或翻譯-它不必要地很復雜。 只需使python代碼與C代碼進行通信即可,如下所示。 感謝上面的代碼,我的評論。
#include <Python.h> //get the scripts to interact
int main(int argc, char *argv[])
{
PyObject *pName, *pModule, *pDict, *pFunc, *pValue; //Arguments that are required.
if (argc < 3)
{
printf("Usage: exe_name python_source function_name\n");
return 1;
}
// Allow your program to understand python
Py_Initialize();
// Initialize Name
pName = PyString_FromString(argv[1]);
// Import the module from python
pModule = PyImport_Import(pName);
// pDict is a borrowed reference
pDict = PyModule_GetDict(pModule);
// same situiation as above
pFunc = PyDict_GetItemString(pDict, argv[2]);
if (PyCallable_Check(pFunc))
{
PyObject_CallObject(pFunc, NULL);
} else
{
PyErr_Print();
}
// Tidy Everything Up.
Py_DECREF(pModule);
Py_DECREF(pName);
// End interpreter
Py_Finalize();
return 0;
}
我建議您閱讀上面的教程。
希望這可以幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.