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

相当于指向结构体的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*?

  • 写回答

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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?