xeruf
xeruf

Reputation: 2990

GHC Statically link dynamic libraries

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.

Upvotes: 4

Views: 1904

Answers (2)

Ari Fordsham
Ari Fordsham

Reputation: 2515

If you use The Stack build system, it will automatically download and manage a fixed version of GHC and all libraries per project (and store them in a hidden folder in your project directory), so if you get it set up right, the whole lot will be static. Stack doesn't have amazing static support right now, but it can be made to work. Some resources:

Upvotes: 0

Ari Fordsham
Ari Fordsham

Reputation: 2515

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.

Upvotes: 1

Related Questions