示例程序:

int main(int argc, char *argv[])
{ PyObject
*pName, *pModule, *pDict, *pFunc, *pValue, *pArgs,*pXVec,*c, *xarr1; int i; float fArray[5] = {0,1,2,3,4}; npy_intp m[1] = {5};// Initialize the Python Interpreter Py_Initialize(); PySys_SetArgv(argc, argv); // Build the name object pName = PyString_FromString(argv[1]); // Load the module object pModule = PyImport_Import(pName); printf("check0\n"); // pDict is a borrowed reference pDict = PyModule_GetDict(pModule); printf("check1\n"); // pFunc is also a borrowed reference pFunc = PyDict_GetItemString(pDict, argv[2]); printf("check2\n"); c = PyArray_SimpleNewFromData(1,m,NPY_FLOAT,fArray); printf("check3\n"); pArgs = PyTuple_New(1); PyTuple_SetItem(pArgs,0, c); pValue = PyObject_CallObject(pFunc, pArgs); if (pArgs != NULL) { Py_DECREF(pArgs); }

// Clean up Py_DECREF(pModule); Py_DECREF(pName); // Finish the Python Interpreter Py_Finalize(); return 0; }

运行时出现以下错误:

解决方法:在Py_Initialize()后加入“import_array()”语句即可。

Py_Initialize();
import_array();

注意:当编译"import_array()"时,可能会出现以下错误:

解决方法:在自己安装的python目录中搜索"object.h"文件,将其56行"#define Py_REF_DEBUG"语句注释掉即可。

 

posted on 2017-09-04 16:32  2师兄不会胖  阅读(2561)  评论(0编辑  收藏  举报