How to use the Haskell Stack build tool to export the library to be used by C / C ++?

Suppose you use the stack build tool to create a Haskell library (import packages from Hackage, etc.) that will be used with a C / C ++ project, in which main is in C / C ++ .

Suppose your project is named Lib.hs (which uses external libraries from hacking), is there a way to use the stack to export your Lib.o , Lib.hi and Lib_stub.h , which will be used by the C / C ++ compiler like gcc or g++ ?

EDIT: A related question might be: "how can I use Stack as a build tool to be used with a Haskell and C / C ++ project, in which main is in C / C ++

EDIT2:. When reflecting, one way to solve this problem would be to use Stack as usual, but port your core C / C ++ function to Haskell. Is this the best way to do this? Are there huge costs to doing this or anything I should know about?

+7
haskell ffi haskell-stack
source share
1 answer

The stack cannot do this on its own.

The creation of so-called “foreign libraries” added to Cabal is supported, but this is not yet in the released version. See Commit 382143. This will create a shared library that dynamically links to dynamic versions of each Haskell package used.

You can create your package with a stack, and then after you can build one native library. In the Galua project, we do this using Setup.hs and a separate binding script .

The result of this linking process is that you get a separate, statically linked library, suitable for inclusion in the C: libgalua.a .

Note that in order to create standalone Linux libraries suitable for connecting to a shared library, you will need to recompile the GHC to create static PIC libraries (by default, macOS does this).

+4
source share

All Articles