Hi. I'm having trouble calling a C function passing a structure
by value, printing a member and getting it back in D but I must
be doing something wrong.
I have a C file called native.c that defines a function called
'filter' taking a buf_t structure, I compile it using GCC to
native.o. From D, I use extern(C) to access the 'filter' function
defined in native.o.
##Makefile
app: native.o
dmd -ofbin/app app.d native.o
chmod +x bin/app
bin/./app
native.o: native.c
gcc -c native.c -o native.o
##app.d
import std.stdio;
extern(C) struct buf_t {
void* base;
size_t len;
}
extern(C) buf_t filter(const buf_t value);
void main() {
buf_t buf;
buf.len = 10;
auto r = filter(buf);
writefln("D: len %d", r.len);
}
***native.c***
#include <sys/types.h>
#include <stdio.h>
typedef struct {
void* base;
size_t len;
} buf_t;
buf_t filter(buf_t value) {
printf("C: len: %zu\n", value.len);
return value;
}
***The output***
C: len: 140734905718328
D: len 0
"len" should be 10 in both cases. What am I doing wrong?
I'm using:
OS: Mac OSX Lion, 64bits.
D compiler: DMD64 D Compiler v2.058
C compiler: gcc version 4.2.1 (Based on Apple Inc. build 5658)
(LLVM build 2336.1.00)
Thanks,
Johan