Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4066876
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T16:10:51+00:00 2026-05-20T16:10:51+00:00

Hey, I’m learning Haskell and I’m interested in using it to make static libraries

  • 0

Hey,
I’m learning Haskell and I’m interested in using it to make static libraries for using in Python and probably C. After some googling I found out how to get GHC to output a shared object, but it dynamically depends on GHC`s libraries.
The resulting ELF from compiling in GHC is dynamically dependand only on C libs and it’s a bit under a MB in size – it has been statically linked with GHC`s libs. How and if can this be achieved for shared objects?

Example of current state:

$ ghc --make -dynamic -shared -fPIC foo.hs -o libfoo.so
$ ldd libfoo.so
    linux-vdso.so.1 =>  (0x00007fff125ff000)
    libHSbase-4.2.0.2-ghc6.12.3.so => /usr/lib/ghc-6.12.3/base-4.2.0.2/libHSbase-4.2.0.2-ghc6.12.3.so (0x00007f7d5fcbe000)
    libHSinteger-gmp-0.2.0.1-ghc6.12.3.so => /usr/lib/ghc-6.12.3/integer-gmp-0.2.0.1/libHSinteger-gmp-0.2.0.1-ghc6.12.3.so (0x00007f7d5faac000)
    libgmp.so.10 => /usr/lib/libgmp.so.10 (0x00007f7d5f816000)
    libHSghc-prim-0.2.0.0-ghc6.12.3.so => /usr/lib/ghc-6.12.3/ghc-prim-0.2.0.0/libHSghc-prim-0.2.0.0-ghc6.12.3.so (0x00007f7d5f591000)
    libHSffi-ghc6.12.3.so => /usr/lib/ghc-6.12.3/libHSffi-ghc6.12.3.so (0x00007f7d5f383000)
    libc.so.6 => /lib/libc.so.6 (0x00007f7d5f022000)
    /lib/ld-linux-x86-64.so.2 (0x00007f7d60661000)

$ ghc foo.hs
$ ldd foo
    linux-vdso.so.1 =>  (0x00007fff2d3ff000)
    libgmp.so.10 => /usr/lib/libgmp.so.10 (0x00007f50014ec000)
    libm.so.6 => /lib/libm.so.6 (0x00007f5001269000)
    librt.so.1 => /lib/librt.so.1 (0x00007f5001061000)
    libdl.so.2 => /lib/libdl.so.2 (0x00007f5000e5d000)
    libc.so.6 => /lib/libc.so.6 (0x00007f5000afc000)
    libpthread.so.0 => /lib/libpthread.so.0 (0x00007f50008df000)
    /lib/ld-linux-x86-64.so.2 (0x00007f5001759000)

If I try to compile it with(without ‘-dynamic’):

$ ghc --make -shared -fPIC foo.hs -o libfoo.so
Linking libfoo.so ...
/usr/bin/ld: foo.o: relocation R_X86_64_32S against `stg_CAF_BLACKHOLE_info' can not be used when making a shared object; recompile with -fPIC
foo.o: could not read symbols: Bad value
collect2: ld returned 1 exit status

When googling I found something about this whole issue – that it may come from the fact that GHC is compiled in a specific way(dynamic/static?) and so static linking is not possible. If this is true how is it possible that the ELF binary is statically linked?

Anyway, I am hoping someone can shed some light on this since a huge amount of googling left me with more questions than I started with.

Huge thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T16:10:51+00:00Added an answer on May 20, 2026 at 4:10 pm

    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/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey there, I'm at the moment trying to make a product management application. The
Hey guys. I'm not much of a programmer, but still need to do some
Hey I am very new to Web Programming. I have been learning PHP from
Hey guys, first time using stackoverflow. can you guys help me debug? Heres the
hey all my question about how the developers makes their interface the buttons, slider,
Hey all, I am not quite getting why this doesn't seem to be working.
Hey everyone, I am working on parsing the PDF content stream to be able
Hey guys, I have a client who wants their front site to have a
Hey guys, I have a little button functionality that is not erring, but also
Hey guys. I'm trying to configure TFS2010 on a home server. I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.