Graeme Perrow
Graeme Perrow

Reputation: 57268

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

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.

Upvotes: 1

Views: 4951

Answers (3)

Graeme Perrow
Graeme Perrow

Reputation: 57268

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.

Upvotes: 2

Ken White
Ken White

Reputation: 125749

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.

Upvotes: 1

Georg Fritzsche
Georg Fritzsche

Reputation: 99074

As per C99, §7.17, size_t is not a builtin type but defined in <stddef.h>.

Upvotes: 10

Related Questions