GHC Statically link dynamic libraries

Viewed 813

On Arch Linux, installed Haskell libraries are dynamically linked by default. So to make anything compile in ghc, I have to use the -dynamic flag, otherwise it doesn't even discover the libraries. However, I would like to produce statically linked binaries that I can distribute to other systems.

Is there any way to produce a statically linked binary from dynamic/shared libraries with ghc?

I tried -optl-static from this related post but that led to countless "undefined reference" errors.

2 Answers

Libraries compiled for dynamic linking are missing information needed for static linking (and vice versa). For details, see:

This is intrinsic in the design of the OS linker, and beyond any limitation of cabal or GHC. For example, this cannot be simply done in C either.

To achieve single-file redistributable binaries, you could try bundling the dynamic libs into the executable, using a format such as AppImage on Linux, or the windres resource scheme on Windows, but you would have to manually set up your code and cabal to find the libraries in the right place.

Related