Search code examples
windowsddub

Linking with C libraries on Windows with Dub


I am trying to link glfw on windows.

On Linux it was fairly straight forward:

dependency "derelict-glfw3" version="~>2.0.0"
subConfiguration "derelict-glfw3" "derelict-glfw3-static"

sourceFiles "deps/glfw/build/src/libglfw3.a" platform="posix"
libs"Xi" "pthread" "X11" "Xxf86vm" "Xrandr" "pthread" "GL" "GLU" "Xinerama" "Xcursor" platform="posix"

If I try to link with a .dll on windows, dub tells me that Error: unrecognized file extension dll.

dependency "derelict-glfw3" version="~>2.0.0"
subConfiguration "derelict-glfw3" "derelict-glfw3-static"

sourceFiles "deps\\glfw\\build\\src\\Debug\\glfw3.dll" platform="windows"

If I try to link with a .lib, dub tells me that COFF is not supported.

dependency "derelict-glfw3" version="~>2.0.0"
subConfiguration "derelict-glfw3" "derelict-glfw3-static"

sourceFiles "deps\\glfw\\build\\src\\Debug\\glfw3.lib" platform="windows"

GLFW was built with vs2013. What do I have to do differently?


Solution

  • There's three cases here:

    • Default 32 bit Windows build. dmd on Windows, by default, builds 32 bits using the old OMF linker format. You cannot link straight to a dll and need a .lib. But since it uses the old format, most .libs provided won't work - you have to make your own.

    (lol i want this in the list item but markdown sucks. whatever)

    Download this thing to get implib.exe: http://ftp.digitalmars.com/bup.zip and use that to make a .lib from the .dll: implib /s yourdll.lib yourdll.dll and try to link with the new lib. (Add it to the files list like you are already doing)

    If it doesn't work, try the command again, but this time without the /s switch.

    It should work by then.

    • -m32mscoff 32 bit builds. With that flag to dmd, it will output a 32 bit program using the new COFF format instead of the old omf format. For it to work, link.exe in your path MUST be the Microsoft linker, from Visual Studio, instead of the default Digital Mars optlink.exe (which, confusingly, was also called link.exe until recently. That was nice when it was a compatible replacement for the Microsoft one.... twenty years ago...).

    Anyway, if you have the Microsoft C++ compiler and linker installed and put that link.exe in your path, dmd -m32mscoff should work using existing .lib files from the dlls.

    TIP: If you used the Windows installer for dmd, open the "D2 64 bit command prompt" from the start menu to get the path set up. It will tell you to use -m64, but you can also use -m32mscoff from that environment and it should work. if everything installed properly.

    • -m64 64 bit builds. Basically the same as the mscoff switch - you need the Visual C++ linker in your path - but does 64 bit instead of 32. Of course, this requires a 64 bit dll and lib to be there too.

    In the comments, it sounds like you might also be facing some bugs, I don't know about that, the above is if everything else is working properly.