Skip to content

Support dynamic linking on Windows

This is related to my recent Discourse post on this topic, where both @david-christiansen and @chreekat encouraged me to make an issue here.

The problem is quite simply that GHC doesn't seem to support dynamic linking on Windows. For example, take my program Brassica, which compiles Haskell code to a library which is linked into a C++ GUI. On Linux, this is easy enough - setting ghc-options: -dynamic -shared -fPIC -fPIE -flink-rts suffices to compile each dependency into a shared library, then do the same to the application. However, on Windows these flags instead result in a bunch of errors like:

src\BrassicaInterop.hs:5:8: error:
    Could not find module `Prelude'
    Perhaps you haven't installed the "dyn" libraries for package `base-4.16.4.0'?
    Use -v (or `:set -v` in ghci) to see a list of the files searched for.
  |
5 | module BrassicaInterop where
  |        ^^^^^^^^^^^^^^^

In fact, -dynamic on its own is enough to trigger this option. So, OK, maybe it would work if -shared alone is used. But no:

Linking main.exe ...
C://ghcup//ghc//9.2.8//mingw//bin/ld.exe: error: export ordinal too large: 74800
collect2.exe: error: ld returned 1 exit status
`gcc.exe' failed in phase `Linker'. (Exit code: 1)
Error: cabal-3.10.1.0.exe: Failed to build brassica-interop-0.1.0.

Even ignoring the fact that Cabal thinks it needs to output an .exe here, this is clearly triggering the issue mentioned in the wiki article on Windows Dynamic Linking. Clearly, if this is enough to befuddle GHC, then dynamic linking on Windows is badly broken.

Steps to reproduce

First:

git clone https://github.com/bradrn/brassica
git checkout windows-dyn-reproducer

Then cabal build brassica-interop to get the first error listed above.

Now, change ./gui/brassica-interop/brassica-interop.cabal to have ghc-options: -shared. This should give the second error listed above.

Environment

  • GHC version used: 9.2.8

Optional:

  • Operating System: Windows 11
  • System Architecture: x86-64
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information