gpt4 book ai didi

go - 什么是 cgo 类型,相当于指向结构的 const 指针?

转载 作者:数据小太阳 更新时间:2023-10-29 03:13:24 24 4
gpt4 key购买 nike

我在 C 中定义了一个外部函数:

//extern void goCallback(const struct libvlc_event_t*, void*);

go 中定义:

//export goCallback
func goCallback(event unsafe.Pointer, userData unsafe.Pointer) {
log.Fatal("TODO goCallback")
}

编译代码时出现类型冲突错误

# github.com/tarrsalah/libvlc-go
In file included from $WORK/github.com/tarrsalah/libvlc-go/_obj/_cgo_export.c:3:0:
cgo-gcc-export-header-prolog:42:13: error: conflicting types for ‘goCallback’
In file included from $WORK/github.com/tarrsalah/libvlc-go/_obj/_cgo_export.c:3:0:
../../../tarrsalah/libvlc-go/event_manager.go:6:13: note: previous declaration of ‘goCallback’ was here
//extern void goCallback(const struct libvlc_event_t*, void*);
^~~~~~~~~~
/tmp/go-build855229382/github.com/tarrsalah/libvlc-go/_obj/_cgo_export.c:17:6: error: conflicting types for ‘goCallback’
void goCallback(void* p0, void* p1)
^~~~~~~~~~
In file included from $WORK/github.com/tarrsalah/libvlc-go/_obj/_cgo_export.c:3:0:
../../../tarrsalah/libvlc-go/event_manager.go:6:13: note: previous declaration of ‘goCallback’ was here
//extern void goCallback(const struct libvlc_event_t*, void*);
^~~~~~~~~~

什么是 go 等同于 const struct libvlc_event_t*

最佳答案

使用 typedef 创建结构的类型别名如下:

/*

typedef const struct libvlc_event_t clibvlc_event_t;
extern void goCallback(clibvlc_event_t*, void*);
*/
import "C"

//export goCallback
func goCallback(event *C.clibvlc_event_t, userData unsafe.Pointer) {
log.Fatal("TODO goCallback")
}

如果你想知道goC.clibvlc_event_t 兼容的类型定义, 运行 cgo-godefs选项,即

go tool cgo -godefs <YOUR-GO-FILE>

例如,如果结构定义为 https://docs.libreoffice.org/avmedia/html/Types_8hxx_source.html

struct libvlc_event_t
{
int type; // event type
void *p_obj; // object emitting that event

union // so far we don't need this.
{
struct {
const char *dummy1;
const char *dummy2;
} padding;
} u;
};

然后,go 中的兼容结构将是:

type VLCEvent struct {
Type int32
Pad_cgo_0 [4]byte
Obj *byte
U [16]byte
}

//Cast C struct to Go struct
ev := (*VLCEvent)(unsafe.Pointer(event))

//Cast Go struct to C struct
p := (*C.clibvlc_event_t)(unsafe.Pointer(&VLCEvent{}))

编辑:
添加示例结构。

关于go - 什么是 cgo 类型,相当于指向结构的 const 指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44706042/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com