dongyulian5801
2017-06-22 17:37
浏览 227
已采纳

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

I have an extern function decalred in C:

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

To be defined in go:

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

When compiling the code, I get type conflicting errors

# 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*);
             ^~~~~~~~~~

What is the go equivalent to const struct libvlc_event_t*?

图片转代码服务由CSDN问答提供 功能建议

我在 C 中使用了外部函数:

  //外部void goCallback(const struct libvlc_event_t *,void *); 
   
 
 

要在 go 中定义 :

  //导出goCallback 
func goCallback(事件不安全。指针,userData不安全。指针){
 log.Fatal(“ TODO goCallback”)
} 
    
 
 

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

 #github.com/tarrsalah/libvlc-go \ 来自$ WORK / github.com / tarrsalah / libvlc-go / _obj / _cgo_export.c:3:0:
cgo-gcc-export-header-prolog:42:13的nIn文件:错误:'goCallback'的类型冲突 
包含在$ WORK / github.com / tarrsalah / libvlc-go / _obj / _cgo_export.c:3:0:
 ../../../ tarrsalah / libvlc-go / event_manager.go:6中的文件中 :13:注意:'goCallback'的先前声明在此处
 //外部void goCallback(const struct libvlc_event_t *,void *); 
 ^ ~~~~~~~~~~ 
 / tmp / go-build855229382  /github.com/tarrsal  ah / libvlc-go / _obj / _cgo_export.c:17:6:错误:“ goCallback”的类型冲突
 void goCallback(void * p0,void * p1)
 ^ ~~~~~~~~~ \ 从$ WORK / github.com / tarrsalah / libvlc-go / _obj / _cgo_export.c:3:0:
 ../../../ tarrsalah / libvlc-go / event_manager.go:6中包含的nIn文件:  13:注意:'goCallback'的先前声明在这里
 //外部void goCallback(const struct libvlc_event_t *,void *); 
 ^ ~~~~~~~~~~ n  > 
 
 

等同于 const struct libvlc_event_t * go 是什么?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanfengdian7014 2017-06-23 01:52
    已采纳

    Create type alias to the struct with typedef as follows:

    /*
    
    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")
    }
    

    If you want to know go type definition which is compatible to C.clibvlc_event_t, run cgo with -godefs options, i.e.

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

    For example, if the struct is defined as in 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;
    };
    

    then, the compatible struct in go will be:

    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{}))
    

    EDIT:
    Add example struct.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题