Jump to page: 1 2 3
Thread overview
How to call a function from a dll created with d ?
Jul 01, 2022
Vinod KC
Jul 01, 2022
ryuukk_
Jul 01, 2022
Vinod K Chandran
Jul 01, 2022
mw
Jul 01, 2022
Vinod K Chandran
Jul 01, 2022
mw
Jul 01, 2022
Vinod K Chandran
Jul 01, 2022
Adam D Ruppe
Jul 01, 2022
Vinod K Chandran
Jul 02, 2022
Ruby The Roobster
Jul 02, 2022
Vinod KC
Jul 02, 2022
Ali Çehreli
Jul 02, 2022
Vinod K Chandran
Jul 02, 2022
apz28
Jul 02, 2022
Vinod KC
Jul 02, 2022
mw
Jul 03, 2022
Vinod K Chandran
Jul 03, 2022
frame
Jul 07, 2022
cc
Jul 08, 2022
frame
Jul 08, 2022
Ruby The Roobster
Jul 02, 2022
kinke
Jul 03, 2022
frame
Jul 03, 2022
kinke
Jul 03, 2022
frame
Jul 04, 2022
frame
July 01, 2022

Hi all,
I have created a dll file with this code.

module dimedll;
import core.sys.windows.windows;
import core.sys.windows.dll; // I don't what is this for.
import std.stdio;


mixin SimpleDllMain;

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

So now I have a dll fie named dimedll.dll and a lib file named dimedll.lib.
Now, I have created a d source file called dime.d and wrote this code.

import std.stdio;
import core.sys.windows.windows;
import std.stdio : log = writeln;

pragma(lib, "dimedll.lib");

extern void testFunc();

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

Everything seems to be okay. So I called dmd with this command.
dmd -i -run dime.d
But I got this error message.

dime.obj : error LNK2019: unresolved external symbol __D4dime8testFuncFZv referenced in function __Dmain
dime.exe : fatal error LNK1120: 1 unresolved externals
Error: linker exited with status 1120```
How to fix this ?
July 01, 2022

I think it is extern(D) void testFunc();?

July 01, 2022

On Friday, 1 July 2022 at 20:08:45 UTC, ryuukk_ wrote:

>

I think it is extern(D) void testFunc();?

Thanks for the reply. But the result is same linker error.

July 01, 2022

On Friday, 1 July 2022 at 19:11:16 UTC, Vinod KC wrote:

>

Hi all,
I have created a dll file with this code.

module dimedll;


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

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


dime.obj : error LNK2019: unresolved external symbol __D4dime8testFuncFZv referenced in

I think the problem is the linker looking for dime.testFunc, while your lib function is dimedll.testFunc

July 01, 2022

On Friday, 1 July 2022 at 21:02:20 UTC, mw wrote:

>

I think the problem is the linker looking for dime.testFunc, while your lib function is dimedll.testFunc

Thanks for the reply. What about this mixin SimpleDllMain; I suspect this.

July 01, 2022

On Friday, 1 July 2022 at 21:15:50 UTC, Vinod K Chandran wrote:

>

On Friday, 1 July 2022 at 21:02:20 UTC, mw wrote:

>

I think the problem is the linker looking for dime.testFunc, while your lib function is dimedll.testFunc

Thanks for the reply. What about this mixin SimpleDllMain; I suspect this.

Try follow instructions here:

https://wiki.dlang.org/Win32_DLLs_in_D

July 01, 2022

On Friday, 1 July 2022 at 22:22:42 UTC, mw wrote:

>

Try follow instructions here:

https://wiki.dlang.org/Win32_DLLs_in_D

Thanks. So using a def file is a must I think. At first, I thought I can skip that.

July 01, 2022
On Friday, 1 July 2022 at 22:32:24 UTC, Vinod K Chandran wrote:
> So using a `def` file is a must I think.

no it is not. you just need to mark things export and make sure names match (including module name)
July 01, 2022
On Friday, 1 July 2022 at 22:38:17 UTC, Adam D Ruppe wrote:
> On Friday, 1 July 2022 at 22:32:24 UTC, Vinod K Chandran wrote:
>> So using a `def` file is a must I think.
>
> no it is not. you just need to mark things export and make sure names match (including module name)

Thanks for the reply. These are my questions.
1. `mixin` statement in dll file - Do I need to export it ?
2. There is only one function and that is marked with `export`.
3. Name of the module which I wrote the dll code is `dimedll`. So my dll file's name is `dimedll.dll`. And my lib file's name is `dimedll.lib`. No change in names.
4. Name of my exported function is `testFunc`. And the same name is used in `extern` keyword and the calling site.
So where do I check again ?
July 02, 2022

The solution is to remove the extern declaration. That does it for me, and it prints the expected output. No need for a .def file, unless you are using optlink as your linker (which, as a matter of principle, you should use lld or ld instead.)

« First   ‹ Prev
1 2 3