代码之家  ›  专栏  ›  技术社区  ›  Graeme Perrow

为什么Go似乎无法识别C头文件中的大小?

go c
  •  1
  • Graeme Perrow  · 技术社区  · 16 年前

    我正在尝试编写一个go库,它将充当C库的前端。如果我的一个C结构包含 size_t ,我得到编译错误。就我所知 大小\u t

    typedef struct mystruct
    {
        char *      buffer;
        size_t      buffer_size;
        size_t *    length;
    } mystruct;
    

    gcc failed:
    In file included from <stdin>:5:
    mydll.h:4: error: expected specifier-qualifier-list before 'size_t'
    
    on input:
    
    typedef struct { char *p; int n; } _GoString_;
    _GoString_ GoString(char *p);
    char *CString(_GoString_);
    #include "mydll.h"
    

    我甚至试过添加 // typedef unsigned long size_t // #define size_t unsigned long 在.go文件中 #include

    我见过 these questions ,然后看了看 example 没有成功。

    3 回复  |  直到 9 年前
        1
  •  10
  •   Georg Fritzsche    16 年前

    根据C99, §7.17 size_t <stddef.h> .

        2
  •  2
  •   Graeme Perrow    16 年前

    原来的问题通过添加 #include <stddef.h>

    第二个问题是我的Go代码 mydll.mystruct 而不是 C.mystruct ,因此根本没有使用C包。cgo编译器中有一个bug,在导入C包而不使用时显示此错误消息。cgo bug已经被修复(由其他人修复)以提供更有用的错误消息。

    详情如下: here .

        3
  •  1
  •   Ken White    16 年前