digitalmars.D - Linking D DLL to C
- Kirk McDonald (40/40) May 23 2006 I am about 50% certain I am missing something obvious. I'm playing with
- BCS (5/20) May 23 2006 First of all I'd try the using dmc/optlink instead of gcc. I haven't
- Kirk McDonald (9/12) May 23 2006 Interesting. That did it:
- d (8/9) Jun 01 2006 I would actually put this down to gcc not recognising the .lib format yo...
- Unknown W. Brackets (5/23) Jun 01 2006 If you want a dll, and you're staticly linking it, you have to create an...
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
May 23 2006
Kirk McDonald wrote: [...]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 McDonaldFirst of all I'd try the using dmc/optlink instead of gcc. I haven't tried this but dmd on win does have some issues with object/lib file compatibility (at least with VS).
May 23 2006
BCS wrote:First of all I'd try the using dmc/optlink instead of gcc. I haven't tried this but dmd on win does have some issues with object/lib file compatibility (at least with VS).Interesting. That did it: C:>dmc -c test2.c C:>dmc test2.obj mydll.lib link test2,,,mydll+user32+kernel32/noi; C:>test2 hello dll world Are there any plans to fix this? Is this perceived as a serious problem? -Kirk McDonald
May 23 2006
"Kirk McDonald" <kirklin.mcdonald gmail.com> wrote in message news:e506jv$9gv$1 digitaldaemon.com...Are there any plans to fix this? Is this perceived as a serious problem?I would actually put this down to gcc not recognising the .lib format you generated. Have a look at the mingw/cygwin gcc specs. I'm sure you have to generate gcc specific .a files, or at least gcc specific .lib files from the .def file. IIRC there is a tutorial on the Net about creating dll's with Mingw, might be worth looking at that to see what is going on. M
Jun 01 2006
If you want a dll, and you're staticly linking it, you have to create an import library from the dll... To link dynamically, you'll typically use LoadLibrary(), etc. at least on Windows. -[Unknown]BCS wrote:First of all I'd try the using dmc/optlink instead of gcc. I haven't tried this but dmd on win does have some issues with object/lib file compatibility (at least with VS).Interesting. That did it: C:>dmc -c test2.c C:>dmc test2.obj mydll.lib link test2,,,mydll+user32+kernel32/noi; C:>test2 hello dll world Are there any plans to fix this? Is this perceived as a serious problem? -Kirk McDonald
Jun 01 2006