Linking D DLL to C
Kirk McDonald
kirklin.mcdonald at gmail.com
Tue May 23 16:13:46 PDT 2006
I am about 50% certain I am missing something obvious. I'm playing with
DLLs in D, and following the example here:
http://digitalmars.com/d/dll.html
The end result of this tutorial are some D source files:
dll.d
mydll.d
mydll2.d
test.d
and also a .def file (mydll.def), a .lib file (mydll.lib), and of course
the .dll (mydll.dll). The command
C:>dmd test.d mydll.lib
goes fine, and the resultant executable works fine.
Fine.
Now I want to call this DLL from a C program. First, I re-wrote mydll.d
and mydll2.d and recompiled the DLL and the test executable:
[mydll.d]
export extern(C) void dllprint();
[mydll2.d]
module mydll;
export extern(C)
void dllprint() { printf("hello dll world\n"); }
The original .exe still works. Fine. Then I wrote a C header file and a
test2.c:
[mydll.h]
__declspec(dllimport) void dllprint(void);
[test2.c]
#include "mydll.h"
int main(int argc, char* argv[]) {
dllprint();
return 0;
}
This does not work. I am using MinGW 3.4.2. The following command works:
C:>gcc -c test2.c
test2.o is generated without issue. However:
C:>gcc -L. -lmydll test2.o
./mydll.dll: file not recognized: File format not recognized
collect2: ld returned 1 exit status
The linker fails. Again, I'm pretty sure I'm missing something obvious.
Any help?
-Kirk McDonald
More information about the Digitalmars-d
mailing list