pybind11

How to link my project library to my pybind module?

How to link my project library to my pybind module? Question: I have a cpp program called my_exec. I want to create a python-binding for it. (with pybind11) In my exec I have 1 simple function : int add(int i, int j) { return i+j; } And this is my pybind .cpp file : #include …

Total answers: 1

pybind11 crashes (segmentation fault (core dumped)) while importing ONNX python module

pybind11 crashes (segmentation fault (core dumped)) while importing ONNX python module Question: I am using pybind11 in my C++ code. When I try to import onnx, my code crashes with Segmentation fault (core dumped). However, if I import onnxruntime, everything is well. Of course both onnx and onnxruntime are installed on my system via pip. …

Total answers: 1

Function with dynamic return type in pybind11

Function with dynamic return type in pybind11 Question: In python you can define functions which dynamically return different types: def func(b): if b: return 42 else: return "hello" How can I implement in C++ a function like this and export it with pybind11? Ideally it would be something like: m.def("func", [](bool b) -> py::object { …

Total answers: 1

How to set python function as callback for c++ using pybind11?

How to set python function as callback for c++ using pybind11? Question: typedef bool (*ftype_callback)(ClientInterface* client, const Member* member ,int member_num); struct Member{ char x[64]; int y; }; class ClientInterface { public: virtual int calc()=0; virtual bool join()=0; virtual bool set_callback(ftype_callback on_member_join)=0; }; It is from SDK which I can call the client from dynamic …

Total answers: 2

Embedding a Python interpreter in a multi-threaded C++ program with pybind11

Embedding a Python interpreter in a multi-threaded C++ program with pybind11 Question: I’m trying to use pybind11 in order to make a 3rd party C++ library call a Python method. The library is multithreaded, and each thread creates a Python object, and then does numerous calls to the object’s methods. My problem is that the …

Total answers: 2

How to call a python built-in function in c++ with pybind11

How to call a python built-in function in c++ with pybind11 Question: I am using pybind11 to call a python built-in function like range in c++ code. But I only found way to call a function in module like this: py::object os = py::module::import("os"); py::object makedirs = os.attr("makedirs"); makedirs("/tmp/path/to/somewhere"); But a python built-in function like …

Total answers: 2

How can I build manually C++ extension with mingw-w64, Python and pybind11?

How can I build manually C++ extension with mingw-w64, Python and pybind11? Question: My final goal is to compile Python C++ extension from my C++ code. Currently to get started I am following a simple example from the first steps of pybind11 documentation. My working environment is a Windows 7 Professional 64 bit, mingw-w64 (x86_64-8.1.0-posix-seh-rt_v6-rev0) …

Total answers: 1

PyBind11 destructor not invoked?

PyBind11 destructor not invoked? Question: I have a c++ class wrapped with PyBind11. The issue is: when the Python script ends the c++ destructor is not being automatically invoked. This causes an untidy exit because networking resources need to be released by the destructor. As a work-around it is necessary to explicitly delete the Python …

Total answers: 3

pybind11 modify numpy array from C++

pybind11 modify numpy array from C++ Question: EDIT: It works now, I do not know why. Don’t think I changed anything I want to pass in and modify a large numpy array with pybind11. Because it’s large I want to avoid copying it and returning a new one. Here’s the code: #include <pybind11/pybind11.h> #include <pybind11/stl.h> …

Total answers: 2