用Pybind11投射二维数组

2024-09-30 16:34:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我为一个C++库创建Python绑定,我有头文件/LIB文件,但是这个函数以2D数组作为参数,没有被pyBDN11自动转换。如何正确地转换这个论点?在

头文件将此函数声明为

class __declspec(dllimport) SomeClass {
    public:
        void function(uint32_t arg1, uint32_t arg2, float matrix[3][3]);

我把它绑起来

^{pr2}$

python setup.py bdist_wheel输出以下内容(注释function绑定可以很好地构建轮子)

C:\Users\joona\git\someproject\pybind11\include\pybind11\cast.h(1931,1): error C2664:  'Return pybind11::cpp_function::{ctor}::<lambda_b1f1895366c1273186f52810f5be9d13>::operator ()(Class *,uint32_t ,uint32_t ,float (*)[3]) const': cannot convert argument 4 from 'float *' to 'float (*)[3]' [C:\Users\joona\git\someproject\build\temp.win-amd64-3.7\Release\someproject.vcxproj]

Tags: 文件函数git参数头文件libfunction数组
1条回答
网友
1楼 · 发布于 2024-09-30 16:34:27

我会选择std::array

class __declspec(dllimport) SomeClass {
    public:
        void function(uint32_t arg1, uint32_t arg2,
                      std::array<std::array<float,3>,3> matrix);
...
};

pybind将知道如何将其转换为2d列表。 另外,在cpp代码中,访问权限与float[3][3]相同。在

编辑:

根据您的评论,我认为您可以使用类似的方法(参见from pybind documentation):

^{pr2}$

这里我假设矩阵只是函数的输入;如果结果也是输出的,可以将结果复制回std数组。在

相关问题 更多 >