[英]Ctypes returns wrong Result
fI try to wrap ac function using ctypes, for example: 我尝试使用ctypes包装ac函数,例如:
#include<stdio.h>
typedef struct {
double x;
double y;
}Number;
double add_numbers(Number *n){
double x;
x = n->x+n->y;
printf("%e \n", x);
return x;
}
I compile the c file with the option 我用选项编译c文件
gcc -shared -fPIC -o test.so test.c
to a shared library. 到共享库。
The Python code looks like this: Python代码如下所示:
from ctypes import *
class Number(Structure):
_fields_=[("x", c_double),
("y", c_double)]
def main():
lib = cdll.LoadLibrary('./test.so')
n = Number(10,20)
print n.x, n.y
lib.add_numbers.argtypes = [POINTER(Number)]
lib.add_numbers.restypes = [c_double]
print lib.add_numbers(n)
if __name__=="__main__":
main()
The printf statement in the add_numbers function returns the expected value of 3.0e+1, but the return value of the lib.add_numbers function is always zero. add_numbers函数中的printf语句返回期望值3.0e + 1,但lib.add_numbers函数的返回值始终为零。 I don't see the error, any Idea? 我没有看到错误,有任何想法吗?
Change this: 更改此:
lib.add_numbers.restypes = [c_double]
to this: 对此:
lib.add_numbers.restype = c_double
Note that it is restype
, not restypes
. 请注意,它是restype
,而不是restypes
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.