cgo を使用して x264 ライブラリ用の小さなラッパーを作成しようとしたところ、ネストされた構造体に関する問題が発生しました。ライブラリは、いくつかのフィールドが匿名の構造体である複雑な構造体を多数使用しています。
cgo を使用してこれらの構造体にアクセスしようとすると、ネストされた構造体が存在しないと go が主張するため、コンパイル エラーが発生します。
問題を .h ファイルと以下に貼り付けた .go ファイルにまとめることができました。うまくいけば、問題を示すのに十分明確です。
この問題の解決策または回避策を知っている人はいますか?
ありがとう。
構造体.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}}
}