为什么Go似乎无法识别C头文件中的size_t类型?

huangapple go评论92阅读模式
英文:

Why does Go not seem to recognize size_t in a C header file?

问题

我正在尝试编写一个Go库,它将作为C库的前端。如果我的C结构体中包含size_t,我会得到编译错误。据我所知,size_t是一个内置的C类型,那为什么Go不识别它呢?

我的头文件如下所示:

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"

我甚至尝试在.go文件中的#include之前添加// typedef unsigned long size_t// #define size_t unsigned long,然后我得到了“gcc produced no output”的错误。

我已经看过这些 问题,并查看了示例,但没有成功。

英文:

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.

答案1

得分: 10

根据C99标准的第7.17节,size_t不是内置类型,而是在<stddef.h>中定义的。

英文:

As per C99, §7.17, size_t is not a builtin type but defined in &lt;stddef.h&gt;.

答案2

得分: 2

原始问题通过添加#include &lt;stddef.h&gt;来解决-感谢Ken和Georg。

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

详细信息在这里

英文:

The original problem was solved by adding the #include &lt;stddef.h&gt; - thanks Ken and Georg.

The second problem was that my Go code was using mydll.mystruct rather than C.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.

答案3

得分: 1

在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.

huangapple
  • 本文由 发表于 2010年5月21日 00:37:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/2875883.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定