I was trying to use cgo to write a little wrapper for the x264 library and came across a problem with nested structs. The library uses a lot of complicated structs where some of the fields are anonymous structs themselves.
When trying to use cgo to access these structs, I run in to compilation errors because go claims that the nested structs do not exist.
I've managed to boil down the problem into a .h file and a .go file pasted below. Hopefully that is clear enough to show the problem.
Does anyone know a solution or workaround to 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}}
}