|
有时候需要利用不同语言的特性共同完成一个任务,甚至一个项目。比如在目标识别中,如果用python去实现,有些步骤会比较慢,我们会将这些步骤用c语言来写,然后再用python去调用写好的c库。那有时候,也可能会用到c语言去调用python库,因为python太活跃了,工具库层数不穷,直接用python造好的轮子来高效完成任务。本文将介绍如何用c调用python
环境:centos7、python3
安装Python.h头文件,本文例子为python3.6
yum install python3-devel调用的print函数
#include <Python.h>
int main(int argc, char *argv[]) {
Py_Initialize();
PyRun_SimpleString(&#34;print(&#39;hello world in python and c&#39;)&#34;);
Py_Finalize();
return 0;
}编译
g++ call_py.c -I /usr/include/python3.6m -lpython3.6m -o call_py运行
./call_py 调用带参的内嵌函数
调用math里面的pow函数
#include <Python.h>
int main()
{
Py_Initialize();
if (!Py_IsInitialized())
{
return -1; //init python failed
}
PyObject *pmodule = PyImport_ImportModule(&#34;math&#34;);
if (!pmodule)
{
printf(&#34;cannot find math module \n&#34;);
return -1;
}
else
{
printf(&#34;PyImport_ImportModule success\n&#34;);
}
PyObject *pfunc = PyObject_GetAttrString(pmodule, &#34;pow&#34;);
if (!pfunc)
{
printf(&#34;cannot find pow\n&#34;);
Py_XDECREF(pmodule);
return -1;
}
else
{
printf(&#34;PyObject_GetAttrString success\n&#34;);
}
PyObject *pArgs = PyTuple_New(2);
PyObject *pVal = Py_BuildValue(&#34;i&#34;, 3);
PyObject *pVal1 = Py_BuildValue(&#34;i&#34;, 5);
PyTuple_SetItem(pArgs, 0, pVal);
PyTuple_SetItem(pArgs, 1, pVal1);
PyObject *pResult = PyObject_CallObject(pfunc, pArgs);
int a;
float b;
PyArg_Parse(pResult, &#34;i&#34;, &a);
// PyArg_ParseTuple(pResult,&#34;if&#34;,&a,&b);
printf(&#34;%f\n&#34;, a);
Py_XDECREF(pmodule);
Py_XDECREF(pfunc);
Py_XDECREF(pArgs);
Py_XDECREF(pResult);
Py_Finalize();
return 0;
}编译
g++ call_py1.c -I /usr/include/python3.6m -lpython3.6m -o call_py1运行
./call_py1调用带参的自建函数脚本
python脚本: call_py.py
import logging
LOG_FORMAT = &#34;[%(asctime)s][%(levelname)s][%(module)s.py:%(lineno)d]---> %(message)s&#34;
DATE_FORMAT = &#34;%Y%m%d %T&#34;
logging.basicConfig(filename=&#34;hello.log&#34;, level=logging.DEBUG, format=LOG_FORMAT, datefmt=DATE_FORMAT)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
ch.setFormatter(logging.Formatter(LOG_FORMAT,DATE_FORMAT))
logging.getLogger().addHandler(ch)
def func1(vender,dataid,tuple):
logging.info(&#34;vender = %d, dataid = %d&#34; % (vender,dataid))
logging.info(&#34;type tuple = [%s] tuple = [%s]&#34; % (str(type(tuple)), str(tuple)))
return (15,5.6)
c代码
#include <Python.h>
int main()
{
Py_Initialize();
if (!Py_IsInitialized())
{
return -1; //init python failed
}
PyRun_SimpleString(&#34;import sys&#34;);
PyRun_SimpleString(&#34;sys.path.append(&#39;./&#39;)&#34;);
PyObject *pmodule = PyImport_ImportModule(&#34;call_py&#34;);
if (!pmodule)
{
printf(&#34;cannot find call_py.py\n&#34;);
return -1;
}
else
{
printf(&#34;PyImport_ImportModule success\n&#34;);
}
PyObject *pfunc = PyObject_GetAttrString(pmodule, &#34;func1&#34;);
if (!pfunc)
{
printf(&#34;cannot find func\n&#34;);
Py_XDECREF(pmodule);
return -1;
}
else
{
printf(&#34;PyObject_GetAttrString success\n&#34;);
}
PyObject *pArgs = PyTuple_New(3);
PyObject *pVender = Py_BuildValue(&#34;i&#34;, 2);
PyObject *pDataID = Py_BuildValue(&#34;i&#34;, 10001);
PyObject *pyTupleList = PyTuple_New(2);
float arr_f[2];
arr_f[0] = 78;
arr_f[1] = 3.41;
for (int i = 0; i < 2; i++)
{
PyTuple_SetItem(pyTupleList, i, Py_BuildValue(&#34;f&#34;, arr_f));
}
PyTuple_SetItem(pArgs, 0, pVender);
PyTuple_SetItem(pArgs, 1, pDataID);
PyTuple_SetItem(pArgs, 2, pyTupleList);
PyObject *pResult = PyObject_CallObject(pfunc, pArgs);
int a;
float b;
// PyArg_Parse(pResult, &#34;i&#34;, &a);
PyArg_ParseTuple(pResult,&#34;if&#34;,&a,&b);
printf(&#34;%d %f\n&#34;, a,b);
Py_XDECREF(pmodule);
Py_XDECREF(pfunc);
Py_XDECREF(pArgs);
Py_XDECREF(pResult);
Py_Finalize();
return 0;
}编译
g++ call_py2.c -I /usr/include/python3.6m -lpython3.6m -o call_py2运行
./call_py2 |
|