在pybind11中使用python中的C ++对象

时间:2019-07-09 13:33:04

标签: 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

谢谢。

1 个答案:

答案 0 :(得分:1)

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

请注意,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>