[Haskell-cafe] Ghc / cgi static linking

Don Stewart dons at galois.com
Sun Jun 1 18:26:00 EDT 2008

> Hello,
> I'm researching the use of Haskell to replace some perl scripts (in a web app).
> The app is deployed with a webhosting provider.
> CGI scripts can be executed =>  I can use Haskell. I've tried some
> hello world cgi scripts, compiled them on the same linux
> the hosting company uses and deployed them. It worked!
> But I would like to implement a search feature for the website.  For
> Java/php/perl there 's lucene.
> For haskell there 's holumbus. Unfortunately, sqlite is a requirement
> for holumbus. It is not installed at the server of the
> hosting company.
> Is it possible to staticly link the sqlite3 library using ghc ?

Yes, it is entirely possible to statically link entire CGI apps.

For example, this simple program,

    import Database.SQLite
    main = print "hey, test this"

when compiled as $ ghc A.hs --make is dynamically linked against:

    $ ldd A
        Start            End              Type Open Ref GrpRef Name
        0000000000000000 0000000000000000 exe  1    0   0      A
        0000000041a85000 0000000041ee5000 rlib 0    1   0      /usr/local/lib/libsqlite3.so.9.0
        0000000049b04000 0000000049f1d000 rlib 0    1   0      /usr/lib/libm.so.2.3
        0000000042213000 000000004264f000 rlib 0    1   0      /usr/local/lib/libgmp.so.7.0
        0000000047d0e000 00000000481e0000 rlib 0    1   0      /usr/lib/libc.so.42.0
        0000000047900000 0000000047900000 rtld 0    1   0      /usr/libexec/ld.so

Now, we can just pass some linker flags through to statically link this lot,

    $ ghc A.hs --make -optl-static -no-recomp
    $ ldd A                                  
    ldd: A: not a dynamic executable
    $ file A
    A: ELF 64-bit LSB executable, AMD64, version 1, for OpenBSD, statically linked, not stripped

I've added this information to the web programming FAQ,


Note it also works for fastcgi, which when combined with Haskell's lightweight
threads, makes a good option for performance-oriented web apps.

-- Don

More information about the Haskell-Cafe mailing list