How to compile Haskell to a static library?

 ̄綄美尐妖づ 提交于 2019-11-28 04:57:42

The canonical way of is this:

  1. Export the functions (via FFI) to initialise RTS (runtime system) by the foreign program
  2. Export actual functions you would like to implement in Haskell

The following sections of manual describe this: [1] [2]

On the other way, you can try technique described in this blog post (which mine, by the way):

http://mostlycode.wordpress.com/2010/01/03/shared-haskell-so-library-with-ghc-6-10-4-and-cabal/

It boils down to creating a small C file which is called automatically right after a library is loaded. It should be linked together into the library.

#define CAT(a,b) XCAT(a,b)
#define XCAT(a,b) a ## b
#define STR(a) XSTR(a)
#define XSTR(a) #a

#include

extern void CAT (__stginit_, MODULE) (void);

static void library_init (void) __attribute__ ((constructor));
static void
library_init (void)
{
      /* This seems to be a no-op, but it makes the GHCRTS envvar work. */
      static char *argv[] = { STR (MODULE) ".so", 0 }, **argv_ = argv;
      static int argc = 1;

      hs_init (&argc, &argv_);
      hs_add_root (CAT (__stginit_, MODULE));
}

static void library_exit (void) __attribute__ ((destructor));
static void
library_exit (void)
{
    hs_exit ();
}

Edit: Original blog post describing this technique is this: http://weblog.haskell.cz/pivnik/building-a-shared-library-in-haskell/

This makes ghc compile statically (note that the pthread is before optl-static): ghc --make -static -optl-pthread -optl-static test.hs

Edit: But the static compilation seems to be a bit risky. Most of the times there are some errors. And on my x64 fedora it doesn't work at all. The resulting binary is also quite large, 1.5M for main = putStrLn "hello world"

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!