使用pybind11从c ++调用Python函数 [英] call a Python function from c++ using pybind11

查看:170
本文介绍了使用pybind11从c ++调用Python函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Pybind11 从包含 main()函数的C ++代码中调用python函数.但是我发现很少有参考资料可用.现有的大多数文档都谈到了相反的方向,即从Python调用C ++.

I am trying to call a python function from a C++ code which contains main() function using Pybind11. But I found very few references are available. Most of existing documents talk about the reversed direction, i.e. calling C++ from Python.

是否有完整的示例说明如何执行此操作?我找到的唯一参考是: https://github.com/pybind/pybind11/issues/30

Is there any complete example showing how to do that? The only reference I found is: https://github.com/pybind/pybind11/issues/30

但是它的信息很少.

推荐答案

您的问题的答案实际上包括两部分:一个关于从C ++调用Python函数,另一个关于嵌入解释器.

The answer to your question really has two parts: one about calling a Python function from C++, the other about embedding the interpreter.

在pybind11中调用一个函数仅是将该函数放入 pybind11 :: object 变量中,您可以在该变量上调用 operator()尝试调用物体.(它不必是一个函数,而只是可以调用的东西:例如,它也可以是具有 __ call __ 方法的对象).例如,要从C ++代码中调用 math.sqrt(2),您可以使用:

Calling a function in pybind11 is simply a matter of getting that function into a pybind11::object variable, on which you can invoke operator() to attempt to call the object. (It doesn't have to be a function, but just something callable: for example, it could also be an object with a __call__ method). For example, to call math.sqrt(2) from C++ code you'd use:

auto math = py::module::import("math");
auto resultobj = math.attr("sqrt")(2);
double result = resultobj.cast<double>();

或者您可以将其压缩为:

or you could condense it all to just:

double result = py::module::import("math").attr("sqrt")(2).cast<double>();

问题的第二部分涉及如何从C ++可执行文件执行此操作.构建可执行文件时(即,当C ++代码包含 main()时),您必须先将Python解释器嵌入二进制文件中,然后才能对Python进行任何操作(例如调用Python函数).

The second part of the question involves how to do this from a C++ executable. When building an executable (i.e. when your C++ code contains main()) you have to embed the Python interpreter in your binary before you can do anything with Python (like calling a Python function).

嵌入式支持是当前pybind11 master 分支(将成为2.2版本)中添加的一项新功能.这是一个基本示例,该示例启动嵌入式Python解释器并调用Python函数( math.sqrt ):

Embedded support is a new feature added in the current pybind11 master branch (which will become the 2.2 release). Here's a basic example that starts an embedded Python interpreter and calls a Python function (math.sqrt):

#include <pybind11/embed.h>
#include <iostream>

namespace py = pybind11;

int main() {
    py::scoped_interpreter python;

    auto math = py::module::import("math");
    double root_two = math.attr("sqrt")(2.0).cast<double>();

    std::cout << "The square root of 2 is: " << root_two << "\n";
}

输出:

The square root of 2 is: 1.41421

有关调用函数和嵌入的更多示例和文档,请参见 http://pybind11.readthedocs.io/en/master/advanced/pycpp/object.html http://pybind11.readthedocs.io/en/master/advanced/embedding.html .

More examples and documentation of calling functions and embedding are available at http://pybind11.readthedocs.io/en/master/advanced/pycpp/object.html and http://pybind11.readthedocs.io/en/master/advanced/embedding.html, respectively.

这篇关于使用pybind11从c ++调用Python函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆