Search code examples
cgocompilationcgo

struct_ prefix vs no prefix


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?


Solution

  • 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