15

I'm currently developing some web application written in Haskell. All Haskell libraries are statically linked; although this "bloats" the executable, it not so much of a problem since it will be the only Haskell programm running on the server.

However, I'd also like to get rid on the dependency on libgmp.so, i.e. I would like to link the multiprecision library statically into the program as well, but keep all the other system libraries (such as pthread, libc, and libm) dynamically linked.

Which linker switches to ghc do that trick?

EDIT to account for a side question

Is it possible to disable default linkage of the standard libraries, that are pulled in by default into every Haskell programm? Something like the GCC-equivalent to -nostdlib?

Community
  • 1
  • 1
datenwolf
  • 159,371
  • 13
  • 185
  • 298

2 Answers2

9

dcoutts posted this as a reddit comment:

You can do exactly the same with ghc.

gcc -c prog.c -o prog.o
gcc prog.o libfoo.a -o prog

and lo, with ghc it's the same...

ghc -c prog.hs -o prog.o
ghc prog.o libfoo.a -o prog 
darrint
  • 418
  • 4
  • 8
  • On my tries by `ghc 8.6.3` and for `libffi` this does not work. using `-optl-v` I can see the linker command, somehow it not seems to really realted to the sequence of linker commands I pass, and in this case `ghc` adds two `/tmp/ghcXXXX/ghc_X.o` after the `libffi.a` which they might have reference to `libffi` and an `-lffi` in the end `-l`s series. here is [the command on pastebin](https://pastebin.com/6fRaUTW5) – 2i3r Jan 01 '19 at 22:08
6

You can use -optl to pass options directly to the linker, so to link everything statically, you can use:

ghc --make Main.hs -optl-static -optl-pthread

or put these in GHC-Options if you're using Cabal.

You can probably tweak this futher to have more fine grained control over what to link statically or dynamically. The -v (verbose) option is helpful here to see the final linker command.

hammar
  • 138,522
  • 17
  • 304
  • 385
  • I'd like to link only libgmp.so statically, but the rest of the system libraries shall remain linked dynamically. Every now and then GMP will undergo some ABI changes, while the other libraries are quite ABI stable. (Note the 'B' I'm not talking about 'P'). I want this so that I don't have to install a full blown Haskell Platform for recompilation on the webserver. – datenwolf Oct 20 '11 at 08:38
  • @datenwolf: I understand, but I'm afraid I don't know how to do that. I'm guessing it probably involves some more use of `-optl` or possibly invoking the linker yourself. – hammar Oct 20 '11 at 08:44