英文:
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 <stddef.h>
.
答案2
得分: 2
原始问题通过添加#include <stddef.h>
来解决-感谢Ken和Georg。
第二个问题是我的Go代码使用了mydll.mystruct
而不是C.mystruct
,所以根本没有使用C包。当导入C包但未使用时,cgo编译器会显示此错误消息。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 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论