- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 pybind11 创建一个 python 绑定(bind),它引用一个 C++ 实例,其内存在 C++ 端处理。下面是一些示例代码:
import <pybind11/pybind11>
struct Dog {
void bark() { printf("Bark!\n"); }
};
int main()
{
auto dog = new Dog;
Py_Initialize();
initexample(); // Initialize the example python module for import
// TBD - Add binding between dog and example.dog .
PyRun_StringFlags("import example\n"
"\n"
"example.dog.bark()\n" // Access the C++ allocated object dog.
, Py_file_input, main_dict, main_dict, NULL);
Py_Finalize();
}
我卡在如何在 python example.dog
之间创建链接和 C++ dog
多变的。
我不能使用 py:class_<Dog>.def(py::init<>())
因为这将分配一个新实例 Dog
,这不是我想要的。
最佳答案
我找到了我自己问题的答案。技巧是以下两个概念的组合:
以下示例说明了该技术:
#include <Python.h>
#include <pybind11/pybind11.h>
namespace py = pybind11;
using namespace pybind11::literals;
// Singleton to wrap
struct Singleton
{
Singleton() : x(0) {}
int exchange(int n) // set x and return the old value
{
std::swap(n, x);
return n;
}
// Singleton reference
static Singleton& instance()
{
static Singleton just_one;
return just_one;
}
int x;
};
PYBIND11_PLUGIN(example) {
py::module m("example", "pybind11 example plugin");
// Use this function to get access to the singleton
m.def("get_instance",
&Singleton::instance,
py::return_value_policy::reference,
"Get reference to the singleton");
// Declare the singleton methods
py::class_<Singleton>(m, "Singleton")
// No init!
.def("exchange",
&Singleton::exchange,
"n"_a,
"Exchange and return the current value"
)
;
return m.ptr();
}
int main(int argc, char **argv)
{
Py_Initialize();
PyObject* main_module = PyImport_AddModule("__main__");
PyObject* main_dict = PyModule_GetDict(main_module);
initexample();
// Call singleton from c++
Singleton::instance().exchange(999);
// Populate the example class with two static pointers to our instance.
if (PyRun_StringFlags("import example\n"
"\n"
"example.s1 = example.get_instance()\n"
"example.s2 = example.get_instance()\n",
Py_file_input, main_dict, main_dict, NULL) == nullptr)
PyErr_Print();
// Test referencing the singleton references
if (PyRun_StringFlags("from example import *\n"
"\n"
"for i in range(3):\n"
" print s1.exchange(i*2+1)\n"
" print s2.exchange(i*2+2)\n"
"print dir(s1)\n"
"print help(s1.exchange)\n"
,
Py_file_input, main_dict, main_dict, NULL) == nullptr)
PyErr_Print();
Py_Finalize();
exit(0);
}
关于python - 在 pybind11 中引用 C++ 分配的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39860405/
首先,感谢大家努力解决我的这个疑问。我正在转换一个最小的 C++ 项目以在 Python 中使用。这种努力背后的真正原因是为了速度。 我遇到了 PyBind,对它的功能以及他们提供的文档数量感到非常惊
我有以下类结构(我实际实现的简化示例): /* TestClass.hpp */ #pragma once template class CurRecTemplate { protected:
我有以下设置(1 个基类、1 个派生类、1 个容器)。容器采用 shared_ptr作为输入。 #include namespace py = pybind11; struct Base { };
我刚刚安装了 pybinding,我正在尝试运行该库文档中提出的第一个示例。 import pybinding as pb import numpy as np import matplotlib.p
我正在使用 pybind 包装一些 C++ 函数,然后在 Python 中使用它。我需要一些结构,但我不知道如何在 Python 中访问它的属性。我的结构没有只有方法的属性,所以我认为绑定(bind)
我使用以下命令创建了一个 miniconda 环境: conda create -n build_a_python_cpp_module xtensor-python -c conda-forge 激
为了更好地理解如何使用 pybind 库将参数从 Python 传递到 C++ 函数,我想构建一个小的虚拟/演示代码,我可以在其中接收 C++ 端的 Python 列表,将其转换为浮点指针对象,然后打
我正在尝试编译 Pybind11 C++ 中的模块它调用了几个头文件(.h)在上面。由于我有很多头文件,我决定做一个 Makefile ,没有问题,除了创建目标共享对象文件(s.o) .我需要这个共享
pybind 的新手 - 阅读文档,但我不了解如何将其应用于二维数组。 我有两个数组存储 3d 坐标 shape = (10,3) a = np.zeros(shape=(10,3)) b = np.
TL;博士 将 pybind11 绑定(bind)添加到工作中的 C++ DLL 项目允许我在 Python 中导入和使用生成的 DLL,但会破坏我使用 boost/dll 机制在 C++ 代码中使用
在 Pybind 中,可以通知 Python 关于函数的参数名称: m.def("add", &add, "A function which adds two numbers", py::arg(
当我运行 pip install .在我有 setup.py 的目录中。我正在使用 pybind11 为我的 C++ 项目构建一个 python 模块。 (同样,在 Windows 10 上) 我收到
我正在尝试使用 PyBind 在 C++ 中嵌入一些 Python 代码。大多数文档都是关于使用 C++ 扩展 Python,但我对嵌入感兴趣: 关于 http://pybind11.readthed
我正在尝试使用 pybind11 实现 C++ 到 python,并使用 pybind11-multiprocessing-hangs 。不同之处在于我想在c++中使用python对象并继续从c++调
我是一名优秀的程序员,十分优秀!