我无法使用pybind11将全局变量从C语言导出到Python。这个问题可以从一个简单的例子中重现。假设我们有一个头文件(global.h),如下所示:
#ifndef GLOBAL_H
#define GLOBAL_H
extern int array[];
#endif 该数组在C文件(global.c)中定义,如下所示:
#include "global.h"
int array[] = {1, 2, 3, 4};我想使用pybind11和下面的C++文件(pyglobal.cpp)在Python模块中导出这个数组:
#include <pybind11/pybind11.h>
extern "C"
{
#include "global.h"
}
PYBIND11_MODULE(pyglobal, m)
{
m.attr("array") = array;
}当我用CMake (CMakeLists.txt)生成库时,一切正常:
cmake_minimum_required(VERSION 2.8.12)
project(pyglobal)
find_package(pybind11 PATHS ${PYBIND11_DIR} REQUIRED)
pybind11_add_module(pyglobal pyglobal.cpp global.c)但是当我启动一个python3外壳程序并输入
import pyglobal我得到以下错误消息:
> Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyglobal
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: AttributeError: array我在这里做错了什么?
发布于 2019-11-18 07:04:16
该赋值是一个相当不幸的隐式类型转换,因此不会像您认为的那样执行。下面是公开该数组的一种方式,假设您已经安装了numpy:
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
extern "C"
{
#include "global.h"
}
PYBIND11_MODULE(pyglobal, m)
{
auto dtype = pybind11::dtype(pybind11::format_descriptor<int>::format());
m.attr("array") = pybind11::array(dtype, {3}, {sizeof(int)}, array, nullptr);
}如果您不知道大小,可以使用一个空的基数组和一个大的(假的)大小。只要确保不以范围受限的方式迭代数组即可。示例:
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
extern "C"
{
#include "global.h"
}
PYBIND11_MODULE(pyglobal, m)
{
auto dtype = pybind11::dtype(pybind11::format_descriptor<int>::format());
auto base = pybind11::array(dtype, {(unsigned)-1}, {sizeof(uintptr_t)});
m.attr("array") = pybind11::array(dtype, {(unsigned)-1}, {sizeof(int)}, array, base);
}它可以像这样使用:
>>> import pyglobal
>>> for i in range(3):
... print(pyglobal.array[i])
...
1
3
0
>>>但是它例如不能被打印,因为这将在整个(unsigned)-1大小上迭代。
https://stackoverflow.com/questions/48708717
复制相似问题