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用simulink求解一个二阶微分方程,要求截图
  • ¥30 matlab解优化问题代码
  • ¥15 写论文,需要数据支撑
  • ¥15 identifier of an instance of 类 was altered from xx to xx错误
  • ¥100 反编译微信小游戏求指导
  • ¥15 docker模式webrtc-streamer 无法播放公网rtsp
  • ¥15 学不会递归,理解不了汉诺塔参数变化
  • ¥15 基于图神经网络的COVID-19药物筛选研究
  • ¥30 软件自定义无线电该怎样使用
  • ¥15 R语言mediation包做中介分析,直接效应和间接效应都很小,为什么?