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

2022-07-03 Thread frame via Digitalmars-d-learn

On Sunday, 3 July 2022 at 12:54:45 UTC, kinke wrote:

On Sunday, 3 July 2022 at 08:15:38 UTC, frame wrote:

Are you sure?


100%, just try yourself.

Why would the symbol be defined in the executable? `dimedll.d` 
isn't compiled into the executable.


The code is using Phobos std.stdio.writeln templates, so the 
~20 KB for both exe and DLL are to be expected and IMO 
absolutely acceptable.


DMD's DLL support is waaay behind LDC's, especially once stuff 
gets more interesting than trivial examples.


Yeah, I tried LDC and there are differences:

The library file generated by LDC contains the link to the DLL.
The library file generated by DMD is missing that link.

So linking the DMD library would embed the symbol from the 
library - that was my confusion with your example.


Only the -H switch or manual linker command generates a valid 
link to the DLL with DMD but then it's missing all the other 
library contents (also it needs `SimpleDllMain` or bails out 
linking errors to `_calloc` and Windows symbols) :\


Also the -H switch doesn't work correctly. Without supplying 
-L/DLL flag too, I get link error:

```
1561: entry point must be defined
```


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

2022-07-03 Thread kinke via Digitalmars-d-learn

On Sunday, 3 July 2022 at 08:15:38 UTC, frame wrote:

Are you sure?


100%, just try yourself.

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.


Why would the symbol be defined in the executable? `dimedll.d` 
isn't compiled into the executable.


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


The code is using Phobos std.stdio.writeln templates, so the ~20 
KB for both exe and DLL are to be expected and IMO absolutely 
acceptable.


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.


DMD's DLL support is waaay behind LDC's, especially once stuff 
gets more interesting than trivial examples.


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

2022-07-03 Thread frame via Digitalmars-d-learn

On Saturday, 2 July 2022 at 20:43:41 UTC, Vinod KC wrote:


But I got this error message.
dime.obj : error LNK2001: unresolved external symbol 
__D7dimedll12__ModuleInfoZ

dime.exe : fatal error LNK1120: 1 unresolved externals
Error: linker exited with status 1120


I tried the -H switch. You can't rely on that. I comment the 
lines that shouldn't be there - then it should work:


dimedll.di
```d
// D import file generated from 'dimedll.d'
module dimedll;
// import core.sys.windows.dll;
// import std.stdio;
// mixin SimpleDllMain!();
export void testFunc();
```

dimedll.d:
```d
module dimedll;

import core.sys.windows.dll;
import std.stdio;

mixin SimpleDllMain;

export void testFunc() {
writeln("This is from dll");
}
```

app.d:
```d
module app;
import dimedll;

import std.stdio;
import std.stdio : log = writeln;

pragma(lib, "dimedll.lib");

void main() {   
   log("Lets build our own ime");
   testFunc();  
}
```

You should be able to change contents in the DLL and run the 
executable wihtout re-compiling (the library file should be round 
~2kB).


PS: ddemangle just waits for your input. You copy in the mangled 
symbol like `__D7dimedll12__ModuleInfoZ` and press enter ;-)


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

2022-07-03 Thread frame via Digitalmars-d-learn

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 PM18,432 dime.exe
07/02/2022  03:57 PM19,679 dime.obj
07/02/2022  03:56 PM   162 dimedll.d
07/02/2022  03:57 PM20,480 dimedll.dll
07/02/2022  03:57 PM 7,534 dimedll.exp
07/02/2022  03:56 PM13,036 dimedll.lib
07/02/2022  03:57 PM21,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.


Re: Importing module from the perspective of submodule.

2022-07-03 Thread Paul Backus via Digitalmars-d-learn

On Sunday, 3 July 2022 at 05:40:30 UTC, BoQsc wrote:
Is it possible to import module that is not in the module's 
current directory's folder or subfolders?


For example:

I want to import `somemodule2.d` and `somemodule3.d` into a 
**`somemodule.d`**


**.\somefolder\somemodule.d**
.\somemodule2.d
.\someotherfolder\somemodule3.d


Yes. You just have to pass the folders with the modules in them 
to the compiler with the `-I` flag, or (if you're using dub) add 
the folders to your `importPaths`.


```
dmd -Isomeotherfolder -c somefolder\somemodule.d
```