mercredi 17 juin 2020

Using PYBIND11_MODULE properly and smart pointers properly

I am starting to use Pybind11, but don't understand why I am getting the error message

error: function "pybind11_init_wrapper" has already been defined

I am planning to use Pybind11 in a complex project for exposing c++ structures to python, therefore I need to understand how to use PYBIND11_MODULE properly what are the rules for using it

#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>

int add(int i, int j) {
    return i + j;
}

namespace py = pybind11;

PYBIND11_MODULE(cmake_example, m) {
    m.doc() = R"pbdoc(
        Pybind11 example plugin
        -----------------------
        .. currentmodule:: cmake_example
        .. autosummary::
           :toctree: _generate
           add
           subtract
    )pbdoc";

    m.def("add", &add, R"pbdoc(
        Add two numbers
        Some other explanation about the add function.
    )pbdoc");

    m.def("subtract", [](int i, int j) { return i - j; }, R"pbdoc(
        Subtract two numbers
        Some other explanation about the subtract function.
    )pbdoc");

#ifdef VERSION_INFO
    m.attr("__version__") = VERSION_INFO;
#else
    m.attr("__version__") = "dev";
#endif
}

PYBIND11_MODULE(example, n) {
    n.doc() = "pybind11 example module";

    // Add bindings here
    n.def("foo", []() {
        return "Hello, World!";
    });
}

will be helpful to see an example showing how to expose and manipulate arrays using the following structures:

template <typename T>
struct point;

template <typename T>
struct quaternion;

template <typename T>
struct point{
T _x, _y, _z;
};

template <typename T>
struct quaternion{
T _a, _b_i, _c_j, _d_k;
};

It is a good idea to use std::shared_ptr pointers. How to deal with smart pointer in pybind11?

Aucun commentaire:

Enregistrer un commentaire