2 tat1012176682 tat1012176682 于 2014.12.12 16:44 提问

这个go语言封装c函数,使用cgo,参数怎么传?

需要用go语言把下列的c函数封装一下,c的函数如下:
int raw_to_span(OPResult result, int *num, int **time, short **status, double **value);

下面是我的写法,不正确,编译不通过:
func RawToSpan(result uintptr) (num int, time []int, status []int16, value []float64, ret int) {
var t = make([]C.int, num)
var s = make[]C.short, num)
var v = make([]C.double, num)
var n C.int
ret = int(C.raw_to_span(C.OPResult(result), &n, &t, &s, &v))
k := C.op2_num_rows(C.OPResult(result))
num = int(n)
for j := 0; j < int(k); j++ {
time = append(time, int(t[j]))
status = append(status, int16(s[j]))
value = append(value, float64(v[j]))
}
return
}
编译结果:
.\api.go:533: cannot use &t (type *[]C.int) as type **C.int in argument to _Cfunc_op2_raw_to_span
.\api.go:533: cannot use &s (type *[]C.short) as type **C.short in argument to _Cfunc_op2_raw_to_span
.\api.go:533: cannot use &v (type *[]C.double) as type **C.double in argument to _Cfunc_op2_raw_to_span

我也尝试了将t,s,v声明为:var t *C.int 这样写的确是在调用c的时候可以传进去了,但是返回值的时候怎么办?

在线等解答,谢谢
更多 0

1个回答

qq_24456169
qq_24456169   2014.12.12 16:57

需要用go语言把下列的c函数封装一下,c的函数如下:
int raw_to_span(OPResult result, int *num, int **time, short **status, double **value);

下面是我的写法,不正确,编译不通过:
func RawToSpan(result uintptr) (num int, time []int, status []int16, value []float64, ret int) {
var t = make([]C.int, num)
var s = make[]C.short, num)
var v = make([]C.double, num)
var n C.int
ret = int(C.raw_to_span(C.OPResult(result), &n, &t, &s, &v))
k := C.op2_num_rows(C.OPResult(result))
num = int(n)
for j := 0; j < int(k); j++ {
time = append(time, int(t[j]))
status = append(status, int16(s[j]))
value = append(value, float64(v[j]))
}
return
}
编译结果:
.\api.go:533: cannot use &t (type *[]C.int) as type **C.int in argument to _Cfunc_op2_raw_to_span
.\api.go:533: cannot use &s (type *[]C.short) as type **C.short in argument to _Cfunc_op2_raw_to_span
.\api.go:533: cannot use &v (type *[]C.double) as type **C.double in argument to _Cfunc_op2_raw_to_span

我也尝试了将t,s,v声明为:var t *C.int 这样写的确是在调用c的时候可以传进去了,但是返回值的时候怎么办?

在线等解答,谢谢

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!