我有一个C函数-
ERROR foo(int32_t* toptr, int64_t length) {
for (int32_t i = 0; i < length; i++) {
toptr[i] = i;
}
return bar();
}
ERROR
和bar
定义为-
#define ERROR struct Error
struct __attribute__((visibility("default"))) Error {
const char* x;
int64_t hu;
};
inline struct Error
bar() {
struct Error bla;
bla.x = nullptr;
bla.hu = 0;
return bla;
};
如何使用ctypes表示此结构?
根据https://stackoverflow.com/a/38663141/4647107,我应该重新声明它,但我的struct
的格式与该答案略有不同
我可以使用ctypes成功加载函数-
>>> import ctypes
>>> test = ctypes.CDLL("dummy.so")
>>> test.foo
<_FuncPtr object at 0x7f6cec73b7c0>
如果我将占位符返回类型分配给foo
-
test.foo.restype = ctypes.c_bool
然后尝试使用它(基于https://stackoverflow.com/a/4145859/4647107)——
test.foo.argtypes = ctypes.POINTER(ctypes.c_int32), ctypes.c_int64
outarray = [1, 2, 3, 4, 5]
test.foo((ctypes.c_int32 * len(outarray))(*outarray), len(outarray))
我得到一个Segmentation fault (core dumped)
错误
struct Error
未提供。下面是一个声明结构并使用它的示例测试c:
test.py:
输出:
相关问题 更多 >
编程相关推荐