gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 21:55:20 26 4
gpt4 key购买 nike

我的 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com