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

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 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题
  • ¥15 multisim电路设计
  • ¥20 用keil,写代码解决两个问题,用库函数
  • ¥50 ID中开关量采样信号通道、以及程序流程的设计
  • ¥15 U-Mamba/nnunetv2固定随机数种子
  • ¥15 vba使用jmail发送邮件正文里面怎么加图片
  • ¥15 vb6.0如何向数据库中添加自动生成的字段数据。