tags:

views:

152

answers:

3

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 either of // 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.

+5  A: 

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

Georg Fritzsche
+1  A: 

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.

Ken White
I added `#include <stddef.h>` to the .h file and got "gcc produced no output". I changed the `<>` to `""` and got the same thing. Then I put it the .go file in front of `// #include "mydll.h"` and again got the same thing. I'm using gcc 4.1.2, if it matters.
Graeme Perrow
@Graeme Perrow: You are now probably trying to run gcc just on header files or on .c files with no actual (non-static or inline) code.
nategoose
@nategoose: I added a function with a body in the .h file and called it from the go file. Same result.
Graeme Perrow
@Graeme Perrow: You don't add "function with body" to the .h file. You declare it in the .h file, implement it in the corresponding .c source, and then use the function in your application or DLL. If you don't actually use (call) the function from code that can execute, the linker doesn't include it.
Ken White
A: 

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.

Graeme Perrow