How to call a function from a dll created with d ?

kinke noone at nowhere.com
Sat Jul 2 14:06:03 UTC 2022


With LDC, this is sufficient for this trivial example:

```d
module dimedll;

export void testFunc() { // export only needed when compiling 
with `-fvisibility=hidden`
     import std.stdio;
     writeln("This is from dll");
}
```

`ldc2 -shared dimedll.d` generates import lib + DLL.

```d
import dimedll : testFunc;

pragma(lib, "dimedll");

void main() {
     import std.stdio;
     writeln("Lets build our own ime");
     testFunc();
}
```

`ldc2 -link-defaultlib-shared dime.d` generates the .exe and 
makes it share the druntime/Phobos DLLs with `dimedll.dll`. (More 
complex cases might need `-dllimport=all`).

```
C:\temp\dllTest>dime
Lets build our own ime
This is from dll

C:\temp\dllTest>dir
…
07/02/2022  03:54 PM               155 dime.d
07/02/2022  03:57 PM            18,432 dime.exe
07/02/2022  03:57 PM            19,679 dime.obj
07/02/2022  03:56 PM               162 dimedll.d
07/02/2022  03:57 PM            20,480 dimedll.dll
07/02/2022  03:57 PM             7,534 dimedll.exp
07/02/2022  03:56 PM            13,036 dimedll.lib
07/02/2022  03:57 PM            21,233 dimedll.obj
```

On Posix, the only difference is that one would have to link 
`libdimedll.{so,dylib}` explicitly via `-L-ldimedll` instead of 
the `pragma(lib)`.


More information about the Digitalmars-d-learn mailing list