[Python] Re use binding from one project to another (Pybind11)

Discussão em 'Python' iniciado por Stack, Julho 9, 2025.

  1. Stack

    Stack Membro Participativo

    I am working in a binding from C++ to Python by using Pybind11.

    Imagine that there are two C++ projects: Project A and Project B.

    Project A has already been bound to python. I installed this binding with a .whl by using pip.

    Project B is the C++ project I am creating myself and I want to bind to Python. I wrote a function whose return type is a complex type defined in Project A. I can get this in C++ by simply including its header like: #include "ProjectA/complexType.h". However, it is very difficult to expose this class to Python with the classic approach since it has a lot of attributes and methods.

    Furthermore I would not like to do it myself since it has been already done since Project A has already been bound previously.

    Is there any way in which I can reuse the binding of project A to make my project recognize this return type in python?

    P.S.: I leave here an example of Project B binding:

    #include <pybind11/pybind11.h>
    #include "ProjectA/complexType.h"

    namespace py = pybind11;

    complexType myfcn(const std::string &filename)
    {
    // Fcn body
    return complexTypeObject;
    }

    PYBIND11_MODULE(mymodule, m)
    {
    // Module doc
    m.doc() = "Doc.";

    // I do not want this since python cannot recognize the attributes of complexType. Also,
    // I do not want to bind this class again
    py::class_<ProjectA::complexType>(m, "complexType");

    // Fcn binding:
    m.def("myfcn", &myfcn, "My fcn description");
    }


    I want to bind a C++ function, whose return type is a data structure. One of the members of that structure is a complex type object that is defined in another binding.

    That other binding has been installed through a .whl by using pip.

    Continue reading...

Compartilhe esta Página