C++调用python的方法

一、C++中调用python接口

在线手册:https://docs.python.org/3/c-api/intro.html

Windows环境下
python安装时提供了给C++调用的头文件及库文件。
C++中引用头文件 include <Python.h>,放在所有标准引用之前。
头文件目录库文件目录添加到工程属性。
调用python提供的API,传入模块名、函数名、函数参数(封装成PyObject的形式)
获取返回值并解析成C++理解的数据结构

重要接口:

C++的基础数据类型都要封装成PyObject对象,才能被python识别

  • void Py_Initialize(void) 初始化,首先调用

  • void Py_Finalize(void) 反初始化,结束时调用

    变量转化

  • PyObject* Py_BuildValue(const char*, …) 新建变量

  • PyObject* PyString_FromString(char* moduleName) C字符串转换为str

  • PyObject* PyLong_FromDouble(char* moduleName) long转换为double

  • 以此类推

    执行

  • PyRun_SimpleString(const char*) 高层调用python语句

  • PyObject* PyImport_Import(PyObject* pModule) 从模块名获取模块

  • PyObject* PyObject_GetAttrString(PyObject* pModule, char* funcName) 从模块获取函数指针

  • int PyCallable_Check(PyObject* pFunc) 检测函数有效性

  • PyObject* PyTuple_New(int argNum) 创建元组,大小与函数参数个数一致

  • int PyTuple_SetItem(PyObject* pArgs, Py_ssize_t argNum, PyObject* pValue) 设置元组值

  • PyObject* PyObject_CallObject(PyObject* pFunc, PyObject* pArgs) 传入函数及参数,执行,获取返回值

    返回值解析

  • int PyArg_Parse(PyObject* pVlaue, const char* format, …)

  • int PyArg_ParseTuple(PyObject *arg, char *format, …)

PyArg_ParseTuple用法:

int ok;
int i, j;
long k, l;
char *s;
int size;

ok = PyArg_ParseTuple(args, ""); /* No arguments */
        /* Python call: f() */
ok = PyArg_ParseTuple(args, "s", &s); /* A string */
        /* Possible Python call: f('whoops!') */
ok = PyArg_ParseTuple(args, "lls", &k, &l, &s); /* Two longs and a string */
        /* Possible Python call: f(1, 2, 'three') */
ok = PyArg_ParseTuple(args, "(ii)s#", &i, &j, &s, &size);
        /* A pair of ints and a string, whose size is also returned */
        /* Possible Python call: f((1, 2), 'three') */
{
    char *file;
    char *mode = "r";
    int bufsize = 0;
    ok = PyArg_ParseTuple(args, "s|si", &file, &mode, &bufsize);
    /* A string, and optionally another string and an integer */
    /* Possible Python calls:
       f('spam')
       f('spam', 'w')
       f('spam', 'wb', 100000) */
}
{
    int left, top, right, bottom, h, v;
    ok = PyArg_ParseTuple(args, "((ii)(ii))(ii)",
             &left, &top, &right, &bottom, &h, &v);
    /* A rectangle and a point */
    /* Possible Python call:
       f(((0, 0), (400, 300)), (10, 10)) */
}
{
    Py_complex c;
    ok = PyArg_ParseTuple(args, "D:myfunction", &c);
    /* a complex, also providing a function name for errors */
    /* Possible Python call: myfunction(1+2j) */
}

调用实例:

C++部分:

#include <Python.h>

int main(int argc, char *argv[])
{
    PyObject *pName, *pModule, *pFunc;
    PyObject *pArgs, *pValue;
    int i;

    if (argc < 3) {
        fprintf(stderr,"Usage: call pythonfile funcname [
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值