tat1012176682 2014-12-12 08:44 采纳率: 0%
浏览 2817

这个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 2014-12-12 08: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的时候可以传进去了,但是返回值的时候怎么办?

    在线等解答,谢谢

    评论

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题