Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to selectively link certain system libraries statically into Haskell program binary?

Tags:

haskell

linker

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?

like image 695
datenwolf Avatar asked Oct 20 '11 06:10

datenwolf


2 Answers

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 
like image 182
darrint Avatar answered Sep 22 '22 23:09

darrint


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.

like image 21
hammar Avatar answered Sep 22 '22 23:09

hammar