www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - DLL

reply bsving <bsving_member pathlink.com> writes:
Hello

I am just looking into this D language mostly just for fun, but seem to be
unable to make a working DLL in D. I try to follow the instructions on the web
side, but with no progress.

I have copied the dll.d (where the dllmain is) and then i have a transform_d.d
file looking like:

import std.math;

export void transform(double *x, double *y, double theta)
{
double c;
double s;
double x_tmp;

x_tmp = *x;
c = cos(theta);
s = sin(theta);
*x = x_tmp*c + *y*s;
*y = *y*c - x_tmp*s;
}


and a .def file looking like
LIBRARY "transform_d.dll"
DESCRIPTION 'DLL written in D'

EXETYPE NT
CODE PRELOAD DISCARDABLE
DATA PRELOAD SINGLE

EXPORTS
transform  2


To compile i use the command:
dmd -oftransform_d.dll transform_d.d dll.d transform_d.def

The compilation seem to go ok, but with the linker (which start automatically) i
get the error message:

OPTLINK (R) for Win32  Release 7.50B1
Copyright (C) Digital Mars 1989 - 2001  All Rights Reserved

OPTLINK : Error 180: No Match Found for Export/ENTRY -  : transform
OPTLINK : Error 81: Cannot EXPORT : transform
--- errorlevel 2


What am i doing wrong?

thanks
May 25 2006
parent reply Kirk McDonald <kirklin.mcdonald gmail.com> writes:
bsving wrote:
 I have copied the dll.d (where the dllmain is) and then i have a transform_d.d
 file looking like:
 
 import std.math;
 
 export void transform(double *x, double *y, double theta)
 {
 double c;
 double s;
 double x_tmp;
 
 x_tmp = *x;
 c = cos(theta);
 s = sin(theta);
 *x = x_tmp*c + *y*s;
 *y = *y*c - x_tmp*s;
 }

 What am i doing wrong?
 
 thanks

Try declaring transform as extern (C), i.e.: export extern (C) void transform(double *x, double *y, double theta) { // ... } -Kirk McDonald
May 25 2006
parent reply bsving <bsving_member pathlink.com> writes:
In article <e55f2s$rr7$1 digitaldaemon.com>, Kirk McDonald says...

export extern (C) void transform(double *x, double *y, double theta) { // ... } -Kirk McDonald

WOW, thanks. it works like a charm. Just for the fun of it I was investigating if it was possible to write D DLLs to be used in LabVIEW, and yes it works. But why do i need this extern (C)? the web site at Digitalmars says nothing about that.
May 25 2006
parent Sjoerd van Leent <svanleent gmail.com> writes:
bsving schreef:
 In article <e55f2s$rr7$1 digitaldaemon.com>, Kirk McDonald says...
 Try declaring transform as extern (C), i.e.:

 export extern (C)
 void transform(double *x, double *y, double theta) {
     // ...
 }

 -Kirk McDonald

WOW, thanks. it works like a charm. Just for the fun of it I was investigating if it was possible to write D DLLs to be used in LabVIEW, and yes it works. But why do i need this extern (C)? the web site at Digitalmars says nothing about that.

Hello Kirk, D normally mangles the names, into quite difficult to read object-file labels. So that you are able to do overloading, and having the same name in different modules. So this becomes possible: module test1; void transform(double *x); module test2; void transform(int[] y); void transform(int[] y, int[] z); Using the extern(C) directive eliminates the name mangling, making the object-file label looking like: _transform just as it would be in C Regards, Sjoerd
May 25 2006