我在 C 中有一个 mul.so 库,它包含以下函数(将 thcount 流中的矩阵 a 和 b 相乘并返回结果):
double** mul_m(double **a, double **b, int n, int thcount) {
//...
}
我想在使用 Ctypes 的 python 代码中使用这个函数。这是我尝试这样做的方法:
import ctypes as ct
mul_m = ct.CDLL("./mul.so").mul_m
mul_m.argtypes = [ct.pointer(ct.pointer(ct.c_double)),ct.pointer(ct.pointer(ct.c_double)), ct.c_int ]
mul_m.restype = ct.pointer(ct.pointer(ct.c_double)
但是这个选项不起作用(错误TypeError: \_type_ must have storage info)。帮我弄清楚如何在python代码中使用这个函数,如何调用它,可以给它什么数据(库中还有一个函数可以生成一个double**数组,但是有没有可能以某种方式提交,例如,一个 Python 列表( list()))
要指定指针 (
int*) 的类型,您应该ctypes.POINTER(c_int)改为使用ctypes.pointer(c_int)(后者试图获取特定指针 (&int),这没有意义)。例如,您可以
char **environ使用以下方式进行打印ctypes:打印
char*C 行(b'\0'-terminated—表示为bytes)直到遇到空指针(表示为None此处)。两种调用形式iter()都被使用:iter(iterable)和iter(function, terminator)。结论
通常你可以通过
os.environ,os.environbget 来获取这些值(可能会有细微差别)。