为什么 Go 似乎无法识别 C 头文件中的 size_t ?
我正在尝试编写一个 Go 库,它将充当 C 库的前端。如果我的 C 结构之一包含 size_t
,我会收到编译错误。 AFAIK size_t
是一个内置的 C 类型,那么为什么不去识别它呢?
我的头文件看起来像:
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
在#include
之前的 .go 文件,然后我得到“gcc 没有产生输出”。
I am trying to write a go library that will act as a front-end for a C library. If one of my C structures contains a size_t
, I get compilation errors. AFAIK size_t
is a built-in C type, so why wouldn't go recognize it?
My header file looks like:
typedef struct mystruct
{
char * buffer;
size_t buffer_size;
size_t * length;
} mystruct;
and the errors I'm getting are:
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"
I've even tried adding // typedef unsigned long size_t
or // #define size_t unsigned long
in the .go file before the #include
, and then I get "gcc produced no output".
I have seen these questions, and looked over the example with no success.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据 C99,§7.17,
size_t
不是内置类型,而是在
中定义。As per C99, §7.17,
size_t
is not a builtin type but defined in<stddef.h>
.最初的问题通过添加
#include
得到解决 - 感谢 Ken 和 Georg。第二个问题是我的 Go 代码使用的是 mydll.mystruct 而不是 C.mystruct,因此根本没有使用 C 包。 cgo 编译器中存在一个错误,当导入 C 包但未使用时,会显示此错误消息。 cgo 错误已被修复(由其他人修复),以提供更有用的错误消息。
详细信息位于此处。
The original problem was solved by adding the
#include <stddef.h>
- thanks Ken and Georg.The second problem was that my Go code was using
mydll.mystruct
rather thanC.mystruct
, so the C package was not being used at all. There was a bug in the cgo compiler that displayed this error message when the C package was imported and not used. The cgo bug has been fixed (by someone else) to give a more useful error message.Details are here.
在 MSC 中,size_t 在 STDDEF.H 中定义(以及其他位置)。我怀疑您也会在 gcc 中找到它,因此您需要在库 (DLL) 源中添加对该标头的引用。
In MSC, size_t is defined (among other places) in STDDEF.H. I'd suspect that's where you'll find it in gcc as well, so you'll need to add a reference to that header in your library (DLL) source.