當前位置:成語大全網 - 書法字典 - Python可以和C語言互調嗎?

Python可以和C語言互調嗎?

絕對有可能。

因為Python本身就是壹個C庫。妳看到的可執行python只是壹個存根。真正的python實體是在動態鏈接庫中實現的。在Windows平臺上,該文件位於% systemroot % system32python27.dll中。同樣,您可以在自己的程序中調用Python,如下所示:

#包括

int great _ function _ from _ python(int a){

int res

PyObject *pModule,* pFunc

PyObject *pArgs,* pValue

/*導入*/

p module = py Import _ Import(py string _ from string(" great _ module "));

/* great _ module . great _函數*/

p func = py object _ GetAttrString(p module," great _ function ");

/*構建參數*/

pArgs = py tuple _ New(1);

PyTuple_SetItem(pArgs,0,PyInt _ from long(a));

/*呼叫*/

pValue = py object _ call object(p func,pArgs);

RES = PyInt _ as long(pValue);

返回res

}