www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Help with C struct by value on OSX 64bits

reply "Johan Hernandez" <thepumpkin1979 gmail.com> writes:
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
Apr 25 2012
next sibling parent reply simendsjo <simendsjo gmail.com> writes:
On Wed, 25 Apr 2012 09:57:39 +0200, Johan Hernandez  
<thepumpkin1979 gmail.com> wrote:

 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.

Could be this one: http://d.puremagic.com/issues/show_bug.cgi?id=5570
Apr 25 2012
parent Jacob Carlborg <doob me.com> writes:
On 2012-04-25 20:36, Johan Hernandez wrote:
 On Wednesday, 25 April 2012 at 08:38:02 UTC, simendsjo wrote:
 Could be this one: http://d.puremagic.com/issues/show_bug.cgi?id=5570

Thank you for your response. It's a huge issue :(

Compile as a 32bit binary. For DMD add "-m32" do the flags. For gcc add "-arch i386". -- /Jacob Carlborg
Apr 25 2012
prev sibling next sibling parent "Johan Hernandez" <thepumpkin1979 gmail.com> writes:
On Wednesday, 25 April 2012 at 08:38:02 UTC, simendsjo wrote:
 Could be this one: 
 http://d.puremagic.com/issues/show_bug.cgi?id=5570

Thank you for your response. It's a huge issue :(
Apr 25 2012
prev sibling next sibling parent simendsjo <simendsjo gmail.com> writes:
On Wed, 25 Apr 2012 20:36:31 +0200, Johan Hernandez  
<thepumpkin1979 gmail.com> wrote:

 On Wednesday, 25 April 2012 at 08:38:02 UTC, simendsjo wrote:
 Could be this one: http://d.puremagic.com/issues/show_bug.cgi?id=5570

Thank you for your response. It's a huge issue :(

I agree. Let's hope it gets fixed now that it's confirmed on OSX too. You could use the voting system in bugzilla to give the issue more focus.
Apr 25 2012
prev sibling parent "Johan Hernandez" <thepumpkin1979 gmail.com> writes:
On Wednesday, 25 April 2012 at 19:43:27 UTC, Jacob Carlborg wrote:

 Compile as a 32bit binary. For DMD add "-m32" do the flags. For 
 gcc add "-arch i386".

Thanks Jacob, that worked!!! simendsjo: I upvoted and commented the same day bro.
Apr 26 2012