doulingqiu4349 2013-09-18 07:41
浏览 82
已采纳

Golang(cgo)-是否支持cgo的嵌套结构?

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}}
}
  • 写回答

2条回答 默认 最新

  • doutaoer3148 2013-09-18 08:07
    关注

    What version of Go are you using? Using Go 1.1.2, cgo seems to produce the expected output.

    I ran go tool cgo main.go, and the generated _obj/_cgo_gotypes.go file contained the following definitions:

    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
    }
    

    When I modified your program to correctly refr to c and d nested in the anon field and uncommented the other statements, the program compiled and ran with the final statement printing the struct as.

    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}}
    

    If you are using an older version of Go, perhaps try upgrading. You could also try running cgo manually like I did to see what it is generating if you still run into problems.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 怎样才能让鼠标沿着线条的中心线轨迹移动
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?