C/C++中如何调用Python
来源:互联网 发布:中海达v30网络rtk设置 编辑:程序博客网 时间:2024/04/29 23:09
C++中调用Python脚本的意义就不讲了,至少你可以把它当成文本形式的动态链接库,
需要的时候还可以改一改,只要不改变接口, C++的程序一旦编译好了,再改就没那么方便了
先看Python的代码
代码:
#test function
def add(a,b):
print "in python function add"
print "a = " + str(a)
print "b = " + str(b)
print "ret = " + str(a+b)
return
def foo(a):
print "in python function foo"
print "a = " + str(a)
print "ret = " + str(a * a)
return
把上面的Python代码存为pytest.py
接下来是c++ 的代码
代码:
#include "Python.h"
int main(int argc, char** argv)
{
// 初始化Python
//在使用Python系统前,必须使用Py_Initialize对其
//进行初始化。它会载入Python的内建模块并添加系统路
//径到模块搜索路径中。这个函数没有返回值,检查系统
//是否初始化成功需要使用Py_IsInitialized。
Py_Initialize();
// 检查初始化是否成功
if ( !Py_IsInitialized() )
{
return -1;
}
// 添加当前路径
//把输入的字符串作为Python代码直接运行,返回0
//表示成功,-1表示有错。大多时候错误都是因为字符串
//中有语法错误。
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append('./')");
PyObject *pName,*pModule,*pDict,*pFunc,*pArgs;
// 载入名为pytest的脚本
pName = PyString_FromString("pytest");
pModule = PyImport_Import(pName);
if ( !pModule )
{
printf("can't find pytest.py");
getchar();
return -1;
}
pDict = PyModule_GetDict(pModule);
if ( !pDict )
{
return -1;
}
// 找出函数名为add的函数
pFunc = PyDict_GetItemString(pDict, "add");
if ( !pFunc || !PyCallable_Check(pFunc) )
{
printf("can't find function [add]");
getchar();
return -1;
}
// 参数进栈
*pArgs;
pArgs = PyTuple_New(2);
// PyObject* Py_BuildValue(char *format, ...)
// 把C++的变量转换成一个Python对象。当需要从
// C++传递变量到Python时,就会使用这个函数。此函数
// 有点类似C的printf,但格式不同。常用的格式有
// s 表示字符串,
// i 表示整型变量,
// f 表示浮点数,
// O 表示一个Python对象。
PyTuple_SetItem(pArgs, 0, Py_BuildValue("l",3));
PyTuple_SetItem(pArgs, 1, Py_BuildValue("l",4));
// 调用Python函数
PyObject_CallObject(pFunc, pArgs);
//下面这段是查找函数foo 并执行foo
pFunc = PyDict_GetItemString(pDict, "foo");
if ( !pFunc || !PyCallable_Check(pFunc) )
{
printf("can't find function [foo]");
getchar();
return -1;
}
pArgs = PyTuple_New(1);
PyTuple_SetItem(pArgs, 0, Py_BuildValue("l",2)); //
PyObject_CallObject(pFunc, pArgs);
Py_DECREF(pName);
Py_DECREF(pArgs);
Py_DECREF(pModule);
// 关闭Python
Py_Finalize();
return 0;
}
需要的时候还可以改一改,只要不改变接口, C++的程序一旦编译好了,再改就没那么方便了
先看Python的代码
代码:
#test function
def add(a,b):
print "in python function add"
print "a = " + str(a)
print "b = " + str(b)
print "ret = " + str(a+b)
return
def foo(a):
print "in python function foo"
print "a = " + str(a)
print "ret = " + str(a * a)
return
把上面的Python代码存为pytest.py
接下来是c++ 的代码
代码:
#include "Python.h"
int main(int argc, char** argv)
{
// 初始化Python
//在使用Python系统前,必须使用Py_Initialize对其
//进行初始化。它会载入Python的内建模块并添加系统路
//径到模块搜索路径中。这个函数没有返回值,检查系统
//是否初始化成功需要使用Py_IsInitialized。
Py_Initialize();
// 检查初始化是否成功
if ( !Py_IsInitialized() )
{
return -1;
}
// 添加当前路径
//把输入的字符串作为Python代码直接运行,返回0
//表示成功,-1表示有错。大多时候错误都是因为字符串
//中有语法错误。
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append('./')");
PyObject *pName,*pModule,*pDict,*pFunc,*pArgs;
// 载入名为pytest的脚本
pName = PyString_FromString("pytest");
pModule = PyImport_Import(pName);
if ( !pModule )
{
printf("can't find pytest.py");
getchar();
return -1;
}
pDict = PyModule_GetDict(pModule);
if ( !pDict )
{
return -1;
}
// 找出函数名为add的函数
pFunc = PyDict_GetItemString(pDict, "add");
if ( !pFunc || !PyCallable_Check(pFunc) )
{
printf("can't find function [add]");
getchar();
return -1;
}
// 参数进栈
*pArgs;
pArgs = PyTuple_New(2);
// PyObject* Py_BuildValue(char *format, ...)
// 把C++的变量转换成一个Python对象。当需要从
// C++传递变量到Python时,就会使用这个函数。此函数
// 有点类似C的printf,但格式不同。常用的格式有
// s 表示字符串,
// i 表示整型变量,
// f 表示浮点数,
// O 表示一个Python对象。
PyTuple_SetItem(pArgs, 0, Py_BuildValue("l",3));
PyTuple_SetItem(pArgs, 1, Py_BuildValue("l",4));
// 调用Python函数
PyObject_CallObject(pFunc, pArgs);
//下面这段是查找函数foo 并执行foo
pFunc = PyDict_GetItemString(pDict, "foo");
if ( !pFunc || !PyCallable_Check(pFunc) )
{
printf("can't find function [foo]");
getchar();
return -1;
}
pArgs = PyTuple_New(1);
PyTuple_SetItem(pArgs, 0, Py_BuildValue("l",2)); //
PyObject_CallObject(pFunc, pArgs);
Py_DECREF(pName);
Py_DECREF(pArgs);
Py_DECREF(pModule);
// 关闭Python
Py_Finalize();
return 0;
}
编译选项, 需要手动指定Python 的include 路径, 和链接接路径,
g++ Python.cpp -o Python -I/usr/include/python2.5 -L/usr/lib/python2.5 -lpython2.5
0 0
- C/C++中如何调用Python方法
- C/C++中如何调用Python
- C中调用PYTHON
- c中调用python
- C中如何调用
- C中如何调用C++?C++中如何调用C?
- C中如何调用C++?C++中如何调用C
- python如何调用c语言
- 转帖:C/C++中如何调用Python方法
- 如何在python中调用C语言代码
- C调用python,如何调用pymongo模块
- python中调用c库
- python中调用c库
- C语言中调用python
- python调用c中函数
- C中如何调用LUA
- extern "C"/C中如何调用C++
- Python如何调用C语言接口
- 请问MFC里CHECKBOX用法
- [Unity3D]开发环境安装与配置
- 点在多边形内算法——判断一个点是否在一个复杂多边形的内部翻译 (转)
- 【bzoj4028】【HEOI2015】公约数数列【分块暴力】
- 如何写程序判断自己电脑是大端字节序还是小端字节序?
- C/C++中如何调用Python
- 在字符串中找到全部是由指定的字符组成的最长子串
- STM32输出PWM控制无刷电调心得
- [BZOJ 1867][NOI 1999]钉子和小球(DP水题)
- 例说uboot从命令到驱动
- Android TextView 跑马灯滚动效果
- 设计模式六大原则
- Oracle中的自动维护任务
- 如何切换、显示、新建、删除目录