传递给 Go 的 C 引用无法识别 typedef void*?

ch


typedef void*       MVar;


C_FUNC(

    MVar*     myvar   //[out], return value

)


测试.go


var cvar unsafe.Pointer


_ = C.C_FUNC(&cvar)


当我运行 test.go 时,它告诉我


cannot use _cgo5 (type *unsafe.Pointer) as type *_Ctype_MVar 

in argument to _Cfunc_C_FUNC

在本文档Command cgo: Go references to C中,它说“C 类型 void* 由 Go 的 unsafe.Pointer 表示。”


ITMISS
浏览 381回答 1
1回答

蝴蝶刀刀

cannot use _cgo5 (type *unsafe.Pointer) as type *_Ctype_MVar&nbsp;in argument to _Cfunc_C_FUNCGo 工具链说你有一个类型不匹配。具有匹配类型的等效工作示例,package mainimport (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "unsafe")/*typedef void* pv_t;void cfunc(pv_t *p);#include <stdio.h>int i;void cfunc(pv_t *p) {&nbsp; &nbsp; i = 42;&nbsp; &nbsp; *p = &i;&nbsp; &nbsp; printf("%p %p %d\n", p, *p, *(int*)(*p));}*/import "C"func main() {&nbsp; &nbsp; var cptr unsafe.Pointer&nbsp; &nbsp; C.cfunc((*C.pv_t)(&cptr))&nbsp; &nbsp; fmt.Println(&cptr, cptr, *(*C.int)(cptr))}输出:$ go run so.go0xc000010028 0x592d18 420xc000010028 0x592d18 42$
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go