dosi8657 2018-03-16 15:09
浏览 121
已采纳

struct_前缀vs无前缀

I'm currently writing some basic cgo code. According to the cgo documention about Go to C references:

To access a struct, union, or enum type directly, prefix it with struct_, union_, or enum_, as in C.struct_stat.

My understanding of this sentence is that if I have a struct named Foo in C, I have to use the type C.struct_Foo in Go.

I have written some Go code like:

package main

// #cgo LDFLAGS: -lnavilink -lserialport
// #cgo CFLAGS: -I/usr/local/include
// #include<navilink/navilink.h>
import "C"
import "errors"

func (d *Device) navilinkErrorToGoError() error {
    errorC := C.navilink_get_error_description(d.navilinkC)
    return errors.New(C.GoString(errorC))
    return nil
}

// Device représente une interface
// vers le port série connecté au GPS
type Device struct {
    navilinkC *C.struct_NavilinkDevice
}

// Open permettra au développeur d'ouvrir la communication
func (d *Device) Open(path string) error {
    res := C.navilink_open_device_from_name(C.CString(path), d.navilinkC)
    if res < 0 {
        return d.navilinkErrorToGoError()
    }
    return nil
}

// Close permettra au développeur de fermer la communication
func (d *Device) Close() {
    C.navilink_close_device(d.navilinkC)
}

I encounter a compilation error likes:

cannot convert d.navilinkC (type *_Ctype_struct_NavilinkDevice) to type _Ctype_struct___1

The definition of the C struct is the following:

typedef struct {
  struct sp_port* serial_port;
  struct sp_event_set* event_set;
  uint8_t buffer[NAVILINK_MAX_PACKET_SIZE];
  NavilinkInformation informations;
  int firmware_version;
  NavilinkPacket response_packet;
  int last_error_code;
} NavilinkDevice;

If I use C.DeviceNavilink instead of C.DeviceNavilink as the field's type, the compilation succeeds.

Every C functions expect a pointer to a NavilinkDevice C struct as last parameter. Code for the C library can be found here

Why does the upper documentation sentence mean since you can refer the C type without using any prefix? What is the difference between both?

  • 写回答

1条回答 默认 最新

  • dpq59734 2018-03-16 15:36
    关注

    You are declaring your C struct as a typedef and not as a named struct.

    In your c code you have something like this:

    typedef struct{
        int a;
        char *b;
        ...
        char z[20];
     }Foo;
    

    In C you would refer to this as Foo x when declaring an instance of Foo so in Cgo you would refer to it as C.Foo.

    The reason you are getting the weird error is because the compiler does not have a name for the struct you declared just a typedef, so it calls it _0.

    In your example your Go code is expecting

    struct Foo{
        int a;
        char *b;
        ...
        char z[20];
     };
    

    In C you would refer to this as struct Foo x when declaring an instance of Foo so in Cgo you would refer to it as C.struct_Foo.

    Extra Helpful Info

    • If you need to get the size of a typedef struct Cgo use C.sizeof_Foo
    • To see how Cgo declares the Go version of your C struct run go tool cgo <go file> and look at the _obj/_cgo_gotypes.go file
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘