python - 在 python 中使用 C++ 对象与 pybind11

标签 python c++ pybind11

我的 main.cpp 文件如下所示:

// Embeding the interpreter into c++
// https://pybind11.readthedocs.io/en/master/advanced/embedding.html


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

// Define namespace for pybind11
namespace py = pybind11;

class Vehiclee
{ 
    // Access specifier 
    public: 

    // Data Members 
    int vehicle_id;
    std::string vehicle_name; 
    std::string vehicle_color;

    // Member Functions() 
    void printname() 
    { 
       std::cout << "Vehicle id is: " << vehicle_id; 
       std::cout << "Vehicle name is: " << vehicle_name; 
       std::cout << "Vehicle color is: " << vehicle_color; 
    } 
}; 


int main() {
    // Initialize the python interpreter
    py::scoped_interpreter python;

    // Import all the functions from scripts by file name in the working directory
    py::module simpleFuncs = py::module::import("simpleFuncs");   

    // Test if C++ objects can be passed into python functions
    Vehiclee car1;
    car1.vehicle_id = 1234;
    car1.vehicle_name = "VehicleName";
    car1.vehicle_color = "red";
    py::object car2 = py::cast(car1);   // <-- PROBLEM
    simpleFuncs.attr("simplePrint")(car2);

    return 0;
}

我有一个simpleFuncs.py:

def simplePrint(argument):
    print(argument)

我基本上尝试在 python 中打印该对象,如果可能的话,还尝试在 C++ 中定义的属性。当前的问题在于转换线无法将 C++ 对象转换为 Python 对象。 Here我阅读了如何来回转换,但仍然出现错误,并且不知道该怎么办。

使用 make 编译工作正常,但如果我执行它,我会收到此错误:

terminate called after throwing an instance of 'pybind11::cast_error'
  what():  make_tuple(): unable to convert argument of type 'object' to Python object
Aborted (core dumped)

如果你想自己编译并运行它,这里是我的CMakeLists.txt:

cmake_minimum_required(VERSION 3.0)
project(wrapper)
add_subdirectory(pybind11)
add_executable(wrapper main.cpp)
target_link_libraries(wrapper PRIVATE pybind11::embed)

执行以下步骤:

git clone https://github.com/pybind/pybind11
cmake .
make

提前致谢。

最佳答案

您需要为Vehiclee添加绑定(bind)代码并导入相应的模块。

请注意,py::cast 调用不是必需的

嵌入式模块文档:https://pybind11.readthedocs.io/en/stable/advanced/embedding.html#adding-embedded-modules

// Embeding the interpreter into c++
// https://pybind11.readthedocs.io/en/master/advanced/embedding.html


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

// Define namespace for pybind11
namespace py = pybind11;

class Vehiclee
{
  // Access specifier
public:

  // Data Members
  int vehicle_id;
  std::string vehicle_name;
  std::string vehicle_color;

  // Member Functions()
  void printname()
  {
    std::cout << "Vehicle id is: " << vehicle_id;
    std::cout << "Vehicle name is: " << vehicle_name;
    std::cout << "Vehicle color is: " << vehicle_color;
  }
};

PYBIND11_EMBEDDED_MODULE(embeded, module){

  py::class_<Vehiclee> animal(module, "Vehiclee");
}

int main() {
  // Initialize the python interpreter
  py::scoped_interpreter python;

  // Import all the functions from scripts by file name in the working directory
  auto simpleFuncs = py::module::import("simpleFuncs");
  auto embeded = py::module::import("embeded");


  // Test if C++ objects can be passed into python functions
  Vehiclee car1;
  car1.vehicle_id = 1234;
  car1.vehicle_name = "VehicleName";
  car1.vehicle_color = "red";

  simpleFuncs.attr("simplePrint")(car1);

  return 0;
}

可能的输出:

<embeded.Vehiclee object at 0x7f8afb59aa78>

关于python - 在 python 中使用 C++ 对象与 pybind11,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56953822/

相关文章:

python - ZeroMQ 2 REQ/REP 以允许嵌套对话

c++ - react 任务在检测任务通知之前等待

python - 在 Python/Seaborn 的图例中显示置信区间

python - 在几个列表中查找重复项

c++ - 使用 C++ 和 openssl 计算 STREEBOG256 和 STREEBOG512 哈希值

python - 指向外部托管的 C++ 智能指针(例如 : Python) resources?

c++ - 重载标记为 const 的类成员函数

python - 这个 "score"到底是什么?使用 sklearn/Python 的额外树分类器

c++ - 双变量作为循环计数器