45

If a friend wants to run my Haskell binaries, does he have to first install Haskell, or can he immediately run the binary by itself?

Is the answer the same on Mac, Windows, and Linux?

Don Stewart
  • 137,316
  • 36
  • 365
  • 468
mcandre
  • 22,868
  • 20
  • 88
  • 147
  • See also this earlier answer on dynamic linking, http://stackoverflow.com/questions/6115459/small-haskell-program-compiled-with-ghc-into-huge-binary – Don Stewart Jun 17 '11 at 19:36
  • And on static linking, http://stackoverflow.com/questions/5131182/how-to-compile-haskell-to-a-static-library – Don Stewart Jun 17 '11 at 19:36

4 Answers4

44

GHC does produce stand-alone binaries that do not require GHC itself to be installed, however they do link against some dynamic libraries, most notably libgmp. The remaining libraries are commonly found out of the box on most Linux systems. I believe the situation is similar on Windows.

You can check which dynamic libraries you depend on using ldd on Linux. Here's what I get on Ubuntu Natty for a simple Hello World program:

$ echo 'main = putStrLn "Hello World"' > Hello.hs                                                   
$ ghc --make Hello.hs                                                                     
[1 of 1] Compiling Main             ( Hello.hs, Hello.o )
Linking Hello ...
$ ldd Hello                                                                                
    linux-vdso.so.1 =>  (0x00007fffe45ff000)
    libgmp.so.3 => /usr/lib/libgmp.so.3 (0x00007f8874cf9000)
    libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f8874a74000)
    librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f887486b000)
    libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f8874667000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f88742d3000)
    libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f88740b4000)
    /lib64/ld-linux-x86-64.so.2 (0x00007f8874f7a000)
hammar
  • 138,522
  • 17
  • 304
  • 385
  • 2
    `ldd` should work on Mac OS X as well, and on Windows, you can use [Dependency Walker](http://www.dependencywalker.com/) to get the same information. – Daniel Pryden Jun 17 '11 at 18:24
  • 10
    @Daniel Pryden: `ldd` isn't on OS X, but you can use `otool -L` to get similar information. – mipadi Jun 17 '11 at 18:38
24

GHC compiles Haskell to object code, with a linked runtime. That means that you do not need a Haskell compiler installed to execute Haskell programs.

The generated executable will use some variant of static and dynamic linking, for both C and Haskell library dependencies. Anything that is statically linked does not need to be installed on the user's machine. Anything that is dynamically linked must be installed.

To see what you need to ship along with the executable, on Linux (or Cygwin), use ldd. You can force static linking of almost everything by passing -static to GHC.

Don Stewart
  • 137,316
  • 36
  • 365
  • 468
12

If it's desirable to statically link some C libraries with your Haskell executable, on Linux you can use --whole-archive with the GNU linker; for example:

  ghc --make HelloZ.hs \
    -optl-Wl,--whole-archive \
      -optl/usr/lib/x86_64-linux-gnu/libffi.a \
      -optl/usr/lib/x86_64-linux-gnu/libz.a \
    -optl-Wl,--no-whole-archive

While they're quite common, libffi and libz are not ubiquitous (libffi is one that I often see in my Haskell binaries).

This approach was first suggested to me quite recently, on haskell-cafe.

solidsnack
  • 1,631
  • 16
  • 26
  • 1
    I'm sorry, I made an error when I gave this answer earlier -- I had been testing in a build environment where I had already copied some `.a` files in to GHC's libdir. It's important that all the libraries also have `-optl` in front of them, as given in the (now amended) example above, otherwise GHC reorders them with respect to the linker options. – solidsnack Sep 26 '12 at 14:48
5

An installation of GHC is not required for most binaries. Some (e.g. xmonad) use Haskell as their configuration language; in those cases you will need a compiler.

There is also a question of static vs. dynamic linking. I believe the default for now is still static linking, in which case it should be quite easy to migrate a binary from one machine to the other (just need to have the same architecture and OS).

Daniel Wagner
  • 145,880
  • 9
  • 220
  • 380