digitalmars.D.learn - Linking with C on Windows
- Mark Isaacson (24/24) Jun 05 2014 I'm having a very difficult time figuring out exactly how to do
- bearophile (4/6) Jun 05 2014 Try using the dmc compiler for the C code.
- Mark Isaacson (5/12) Jun 05 2014 I'd considered that, but in the long term that will not be a
- Mark Isaacson (12/12) Jun 05 2014 Still unresolved, but a thought:
- Ellery Newcomer (14/16) Jun 05 2014 how would using dmc conflict with this goal?
I'm having a very difficult time figuring out exactly how to do this. I've compiled my D code into a .obj file with dmd, but I don't know what C compiler I should use (or if it makes any difference). I've attempted to use MinGW gcc, which spits out .o files, and Visual Studio, which does... something else. My attempts to have either the MinGW linker or the Visual Studio linker accept my D .objs have all failed. I have tried both extern(C) and extern(Windows) in the D code, and I'm not sure which of them is more appropriate. In any case, this is the code I'm trying to compile: dimpl.d: extern(Windows) int square(int x) { return x^^2; } ciface.c: #include <assert.h> int square(int x); int main() { assert(square(2) == 4); return 0; } I compiled dimpl.d with: dmd -c dimpl.d Not really sure where to go from here, though I'll post back if I figure anything out. I'm more than willing to take whatever answer I get here and add it to appropriate wiki/site pages :) Thanks!
Jun 05 2014
Mark Isaacson:My attempts to have either the MinGW linker or the Visual Studio linker accept my D .objs have all failed.Try using the dmc compiler for the C code. Bye, bearophile
Jun 05 2014
On Thursday, 5 June 2014 at 22:59:48 UTC, bearophile wrote:Mark Isaacson:I'd considered that, but in the long term that will not be a viable solution for me. I need to eventually be able to export a dll that can talk with anything. I just figured this would be an easy first stepping stone to that end.My attempts to have either the MinGW linker or the Visual Studio linker accept my D .objs have all failed.Try using the dmc compiler for the C code. Bye, bearophile
Jun 05 2014
Still unresolved, but a thought: I decided to take a step back and try to link with C on Linux first. I found out that if I did the linking step with dmd things worked, but not with gcc. The reason then became apparent: dmd knows to pass druntime and phobos and all of that stuff to the linker. Running on the assumption that this was also my problem on Windows, I ran dmd with -v (verbose) to try and get the linker line it uses for a simple program that just uses some Phobos functions, the line I get is: C:\D\dmd2\windows\bin\link.exe test,nul,,user32+kernel32/noi; Which does not appear to be helpful and seems to indicate that Phobos does not need to be passed to the linker?
Jun 05 2014
On Thursday, 5 June 2014 at 23:12:56 UTC, Mark Isaacson wrote:I need to eventually be able to export a dll that can talk with anything.how would using dmc conflict with this goal? dmd/dmc output omf object files, windows infrastructure is all coff object files. linux/mingw/etc are.. something else.. your options are 1. use dmd/dmc, and if you need to link to windows api dlls or others, use coffimplib (here -> ftp://ftp.dlang.org/) to convert the appropriate lib files. 2. if you can stomach the loss of generality, use dmd -m64, which does output coff object files and go your merry way with vc++. (I don't do this, so don't quote me on this) 3. use the mingw builds of ldc/gcc (I don't do this either and can't comment on how much fun you'll have getting dlls out of them/linking to other dlls)
Jun 05 2014