pybind11

Deserialize protobuf buffer in Python from C++ with pybind11

此生再无相见时 提交于 2020-01-05 04:29:26
问题 I have a char *buffer which I convert to a C++ string std::string sbuffer(buffer); because I want to pass it to python. C++ can work with: protoObj.ParseFromArray(buffer, sbuffer.size()); I pass the buffer to python via: py::scoped_interpreter python; py::module calc = py::module::import("Calculation"); py::object Calculation = calc.attr("Calculation"); py::object calculation = Calculation(); calculation.attr("funcName")(sbuffer.data(), sbuffer.size()); The python file looks kinda like this:

import multiple cpp files with pybind11 and cppimport

让人想犯罪 __ 提交于 2019-12-25 18:34:51
问题 How can import multiple cpp files with pybind11? As soon as my project spans over more than a single cpp file, I get an error: raise LinkError(msg) E distutils.errors.LinkError: command 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Tools\\MSVC\\14.24.28314\\bin\\HostX86\\x64\\link.exe' failed with exit status 1120 Any suggestions what I can do about it? Here an example how it could look like: other.h void MyFunc(); main.cpp /* <% setup_pybind11(cfg) %> */ #include

error: no matching function for call to ‘pybind11::buffer_info::buffer_info

泪湿孤枕 提交于 2019-12-24 10:37:46
问题 I'm trying to wrap a c++ function that uses Armadillo library using pybind11 and cppimport . But when I try to do something simple like matrix multiplication I get the following error. error: no matching function for call to ‘pybind11::buffer_info::buffer_info(double*, long unsigned int, std::__cxx11::string, int, <brace-enclosed initializer list>, <brace-enclosed initializer list>)’ ); ^ In file included from /home/muah/anaconda3/envs/ising/include/python3.6m/pybind11/pytypes.h:13:0, from

correct setup.py for mixing Python and C++

亡梦爱人 提交于 2019-12-24 08:06:00
问题 I'm trying to mix both languages and I'm following the nice example provided by pybind here. I actually checked this post to improve on it so I can fall back to Python functions whenever a compiled function doesn't exist. The problem I have now is that my configure.py is not building the correct package. Let me develop: the structure of my code is something like this: $ tree . . ├── AUTHORS.md ├── CMakeLists.txt ├── LICENSE ├── MANIFEST.in ├── Makefile ├── README.md ├── conda.recipe │ ├── bld

How to wrap a singleton class using pybind11?

你。 提交于 2019-12-24 05:00:14
问题 I have a singleton class in C++ (no public constructor, C++ programmers call class.instance() to create the singleton or return the existing one). I'd prefer to hide this at the Python level. If I was writing a Python singleton, I'd handle that in __new__ . If a class has no public constructor I don't think I can create an __init__ wrapper (my attempts at that have failed). I saw no mention of __new__ in the pybind11 docs (though might have missed it, and Google seems happy to elide

Python and C++: How to use pybind11 with Cmakelists including GSL libraries

我的梦境 提交于 2019-12-24 03:58:17
问题 I want to be able to call my C++ code as a python package. To do this I am using pybind11 with CMakelists (following this example https://github.com/pybind/cmake_example). My problem is that I have to include GSL libraries in the compilation of the code, and these need an explicit linker -lgsl . If I were just to compile and run the C++ without wrapping it with python, the following Cmakelists.txt file does the job cmake_minimum_required(VERSION 3.0) set(CMAKE_BUILD_TYPE Debug) set(CMAKE_CXX

pybind11: how to package c++ and python code into a single package?

我与影子孤独终老i 提交于 2019-12-20 12:46:32
问题 I am trying to package together an existing Python code and a new C++ 11 code using CMake and pybind 11. I think I am missing something simple to add into CMake scripts, but can't find it anywhere: pybind11 examples have only C++ code and none of Python, other online resources are rather convoluted and not up-to-date -- so I just can't figure out how to package functions in both languages together and make them available via Python's import my_package down the line... as an example, I have

Embedding pybind11 with virtual environment

跟風遠走 提交于 2019-12-13 16:25:03
问题 I'm trying to embed python using pybind11 into my C++ application. Using the following CMake property, I managed to compile against a virtual environment of my project. -DPYTHON_EXECUTABLE:FILEPATH=C:/Python/Envs/myproject/Scripts/python.exe When I run the application I get an error (below) without a specific error. However I assume it fails to load the module numpy which I'm loading. abort() has been called #include <iostream> #include <pybind11/embed.h> namespace py = pybind11; int main() {

protected virtual destructor in pybind11 [duplicate]

隐身守侯 提交于 2019-12-11 14:22:33
问题 This question already has answers here : protected destructor with unique_ptr (2 answers) Closed 6 months ago . I want to bind with the class from third party library. The class have some pure virtual functions but the destructor is protected and virtual. In order to bind the class, I have to write a derived class that overrides the pure virtual functions (https://pybind11.readthedocs.io/en/stable/advanced/classes.html) so the code is like this class Parent { public: virtual void foo() = 0;

Binding a function with std::initializer_list argument using pybind11

孤街浪徒 提交于 2019-12-11 14:08:52
问题 I am attempting to create python bindings using pybind11(v2.2.2+) and cannot figure out how to invoke a C function that has a single std::initializer_list argument. void list_ints(std::initializer_list<int>) And the pybind11 binding is: m.def("list_ints", &list_ints) From python, I'm trying to invoke like this: list_ints(1, 2, 3) Here is the sample C code compiled using llvm on MacOS with -std=C++14 : #include <iostream> #include <pybind11/pybind11.h> #include <pybind11/stl.h> using namespace