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

frame frame86 at live.com
Sun Jul 3 08:15:38 UTC 2022


On Saturday, 2 July 2022 at 14:06:03 UTC, kinke wrote:
> 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)`.

> This is from dll

Are you sure? You import `testFunc` as normal import, the 
compiler ignores `pragma(lib)` - that's only for the linker which 
will ignore it too since the symbol is already in your 
executable. If you can run your exectuable without dimedll.dll 
present, then the function is **not** statically linked in.

A static linked function should generate a very small lib-file 
and yours look too big to me.

I don't know about LDC but with DMD I struggle with static linked 
DLLs because the library generated does not link to the DLL. To 
get right results, I need to pass the linker flag -`-L=/IMPLIB` 
(or `-L=/DLL` for 64bit) to generate a lib-file that is really 
linked to the DLL later.


More information about the Digitalmars-d-learn mailing list