www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Interfacing with C - div

reply Juliano Ravasi Ferraz <contact write-my-first-name-here.info> writes:
Hi,

I've been trying to do a simple C interface, but failed miserably until now:

extern (C) {
     struct div_t { int quot, rem; }
     div_t div(int numer, int denom);
}
int main() {
     div_t q = div(35, 10);
     printf("%d/%d\n", q.quot, q.rem);
     return 0;
}

Since my glibc doesn't have debuging symbols, the only useful thing that 
gdb reports is that it bailed out inside the div() call. Does someone 
know where did I fail?

My host is a Linux i686 2.6.6, glibc 2.3.2.

Regards,
Juliano.
Jul 03 2004
parent reply Ben Hinkle <bhinkle4 juno.com> writes:
Juliano Ravasi Ferraz wrote:

 Hi,
 
 I've been trying to do a simple C interface, but failed miserably until
 now:
 
 extern (C) {
      struct div_t { int quot, rem; }
      div_t div(int numer, int denom);
 }
 int main() {
      div_t q = div(35, 10);
      printf("%d/%d\n", q.quot, q.rem);
      return 0;
 }
 
 Since my glibc doesn't have debuging symbols, the only useful thing that
 gdb reports is that it bailed out inside the div() call. Does someone
 know where did I fail?
 
 My host is a Linux i686 2.6.6, glibc 2.3.2.
 
 Regards,
 Juliano.

I don't know why but it works for me when I add an extra int field to div_t: struct div_t { int quot, rem, foo; } very odd. -Ben
Jul 05 2004
next sibling parent Ant <Ant_member pathlink.com> writes:
In article <ccbpb8$29d6$1 digitaldaemon.com>, Ben Hinkle says...
Juliano Ravasi Ferraz wrote:

 Hi,
 
 I've been trying to do a simple C interface, but failed miserably until
 now:
 
 extern (C) {
      struct div_t { int quot, rem; }
      div_t div(int numer, int denom);
 }
 int main() {
      div_t q = div(35, 10);
      printf("%d/%d\n", q.quot, q.rem);
      return 0;
 }
 
 Since my glibc doesn't have debuging symbols, the only useful thing that
 gdb reports is that it bailed out inside the div() call. Does someone
 know where did I fail?
 
 My host is a Linux i686 2.6.6, glibc 2.3.2.
 
 Regards,
 Juliano.

I don't know why but it works for me when I add an extra int field to div_t: struct div_t { int quot, rem, foo; } very odd. -Ben

oh,oh... this looks like the overload bug or the lookup rules in action. If I had phobos here I would check other declarations of div... Ant
Jul 05 2004
prev sibling next sibling parent Juliano Ravasi Ferraz <contact write-my-first-name-here.info> writes:
Ben Hinkle wrote:
 I don't know why but it works for me when I add an extra int field to div_t:
  struct div_t { int quot, rem, foo; }
 very odd.

Wow! Really worked with a third int... sure it is odd... Thanks! Regards, Juliano.
Jul 05 2004
prev sibling parent "Walter" <newshound digitalmars.com> writes:
"Ben Hinkle" <bhinkle4 juno.com> wrote in message
news:ccbpb8$29d6$1 digitaldaemon.com...
 Juliano Ravasi Ferraz wrote:

 Hi,

 I've been trying to do a simple C interface, but failed miserably until
 now:

 extern (C) {
      struct div_t { int quot, rem; }
      div_t div(int numer, int denom);
 }
 int main() {
      div_t q = div(35, 10);
      printf("%d/%d\n", q.quot, q.rem);
      return 0;
 }

 Since my glibc doesn't have debuging symbols, the only useful thing that
 gdb reports is that it bailed out inside the div() call. Does someone
 know where did I fail?

 My host is a Linux i686 2.6.6, glibc 2.3.2.

 Regards,
 Juliano.

I don't know why but it works for me when I add an extra int field to

  struct div_t { int quot, rem, foo; }
 very odd.

D returns structs that are 8 bytes long in EDX,EAX. Longer ones are returned on the stack. Perhaps gcc uses a different convention. An obj2asm on the code will tell the tale.
Jul 05 2004