Golang (cgo) - Support for nested structures with cgo?

I tried using cgo to write a small wrapper for the x264 library and ran into a problem with nested structures. The library uses many complex structures in which some of the fields are anonymous structures.

When I try to use cgo to access these structures, I run compilation errors because go claims that nested structures do not exist.

I managed to collapse the problem into a .h file and a .go file inserted below. Hope this is clear enough to show the problem.

Does anyone know a solution or workaround for this problem?

Thanks.

struct.h

typedef struct param_struct_t {
  int a;
  int b;
  struct {
    int c;
    int d;
  } anon;
  int e;
  struct {
    int f;
    int g;
  } anon2;
} param_struct_t;

main.go

package main
/*
#include "struct.h"
*/
import "C"
import (
  "fmt"
)

func main() {
  var param C.param_struct_t
  fmt.Println(param.a) // Works and should work
  fmt.Println(param.b) // Works and should work
  fmt.Println(param.c) // Works fine but shouldn't work
  fmt.Println(param.d) // Works fine but shouldn't work
  // fmt.Println(param.e) // Produces type error: ./main.go:17: param.e undefined (type C.param_struct_t has no field or method e)
  // fmt.Println(param.anon) // Produces type error: ./main.go:18: param.anon undefined (type C.param_struct_t has no field or method anon)

  // The following shows that the first parameters and the parameters from the
  // first anonymous struct gets read properly, but the subsequent things are
  // read as seemingly raw bytes.
  fmt.Printf("%#v", param) // Prints out: main._Ctype_param_struct_t{a:0, b:0, c:0, d:0, _:[12]uint8{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}}
}
+4
source share
2

Go ? Go 1.1.2, cgo, , .

go tool cgo main.go, _obj/_cgo_gotypes.go :

type _Ctype_param_struct_t _Ctype_struct_param_struct_t

type _Ctype_struct___0 struct {
//line :1
        c       _Ctype_int
//line :1
        d       _Ctype_int
//line :1
}

type _Ctype_struct___1 struct {
//line :1
        f       _Ctype_int
//line :1
        g       _Ctype_int
//line :1
}

type _Ctype_struct_param_struct_t struct {
//line :1
        a       _Ctype_int
//line :1
        b       _Ctype_int
//line :1
        anon    _Ctype_struct___0
//line :1
        e       _Ctype_int
//line :1
        anon2   _Ctype_struct___1
//line :1
}

, c d, anon, , , .

main._Ctype_param_struct_t{a:0, b:0, anon:main._Ctype_struct___0{c:0, d:0}, e:0, anon2:main._Ctype_struct___1{f:0, g:0}}

Go, , . cgo , , , , .

+2

. C go, cgo, . , _C_param_struct_t.

, : http://golang.org/misc/cgo/gmp/gmp.go

0

Source: https://habr.com/ru/post/1502741/


All Articles