Creating Haskell Shared Libraries on OS X

I am trying to create a shared library from Haskell source code.

I tried following the instructions here: http://weblog.haskell.cz/pivnik/building-a-shared-library-in-haskell/ , but I was just out of luck.

When I compile with 64-bit Haskell (ghc 7.0.4 from 2011.4.0.0), I get the following error:

ld: pointer in read-only segment not allowed in slidable image, used in ___gmpn_modexact_1c_odd 

As an alternative, I also tried the 32-bit version and depending on the exact flags that I use for links, I get errors such as:

 Library not loaded: /usr/local/lib/ghc-7.0.4/base-4.3.1.0/libHSbase-4.3.1.0-ghc7.0.4.dylib 

I managed to get a little more by adding -lHSrts to the linker line. This allowed me to successfully link and load the library, but I could not find the function name using dlsym (or manually using nm | grep)

Any hints are welcome, evaluating the make or build file that has successfully built (and used) the shared library in OS X. I am completely new to Haskell and do not know if I should continue to bang my head, believing that the problem is at my end, or different For reasons I should not expect this to work on OS X.

A git repo with all the combinations I tried is available here: https://github.com/bennoleslie/haskell-shared-example I managed to get something working for 32-bit ghc but not yet 64-bit.

+43
haskell ghc shared-libraries macos
Mar 24 2018-12-12T00:
source share
2 answers

It is possible to create working shared libraries in 64-bit OS X with the latest version of the Haskell Platform (2012.4 64bit)

The call line works for me:

 ghc -O2 --make \ -no-hs-main -optl '-shared' -optc '-DMODULE=Test' \ -o libTest.so Test.hs module_init.c 

module_init.c should look something like this:

 #define CAT(a,b) XCAT(a,b) #define XCAT(a,b) a ## b #define STR(a) XSTR(a) #define XSTR(a) #a #include <HsFFI.h> 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(); } 

This git repo: https://github.com/bennoleslie/haskell-shared-example contains a working example.

All loans are from this source: http://weblog.haskell.cz/pivnik/building-a-shared-library-in-haskell/

+9
Jan 03 '13 at 13:20
source share

You might want to try the ghc port in Homebrew - https://github.com/mxcl/homebrew/blob/master/Library/Formula/ghc.rb

+1
Oct 18
source share



All Articles