D calling C?
Glenn Lewis
nospam at nospam.net
Sat Mar 3 21:02:32 PST 2007
> > Hi! I'm trying to write a D program that calls C.
> > I've attached two listings: t1.d and t2.cpp.
> > Here's what happens:
> >
> > C:\tmp>dmd -c t1.d
> > C:\tmp>dmc -c t2.cpp
> > C:\tmp>dmd -oft1.exe t1.obj t2.obj
> > C:\dmd\bin\..\..\dm\bin\link.exe t1+t2,t1.exe,,user32+kernel32/noi;
> > C:\tmp>t1
> > Error: Access Violation
> >
> > I'm using DMD version 1.006 and DMC version 8.42n on WinXP.
> > Anyone have any ideas why this doesn't work?
> The attachments are not understood by thunderbird, they are just shown
> inline in some kind of unreadable encoding. Maybe you could just paste
> the sources into the message?
Oh, sorry. Here they are:
---------------------------------------
// t1.d
import std.stdio;
extern (C) { float func(float v[]); }
void main(char[][] argv)
{
float v[2];
v[0] = 0.0;
v[1] = 1.0;
float x = func(v);
writefln("x=%g", x);
}
------------------------------------------
// t2.cpp
#include <stdio.h>
extern "C" { float func(float* v); }
float func(float v[])
{
printf("v[0]=%g, v[1]=%g", v[0], v[1]);
return v[0] + v[1];
}
--------------------------------------------
-- Glenn
More information about the Digitalmars-d
mailing list