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.

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

报告相同问题?

悬赏问题

  • ¥15 想用@vueuse 把项目动态改成深色主题,localStorge里面的vueuse-color-scheme一开始就给我改成了dark,不知道什么原因(相关搜索:背景颜色)
  • ¥20 OPENVPN连接问题
  • ¥15 flask实现搜索框访问数据库
  • ¥15 mrk3399刷完安卓11后投屏调试只能显示一个设备
  • ¥20 白日门传奇少一个启动区服和启动服务器的快捷键,东西都是全的 , 他们说套一个出来就行了 但我就是弄不好,谁看看,
  • ¥100 如何用js写一个游戏云存档
  • ¥15 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题