8

Suppose one is using the stackbuild tool to make a Haskell library (importing packages from Hackage, and so forth) to be used with a C/C++ project in which main is located in C/C++.

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

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

EDIT2: Upon reflection, one way to solve this problem would be to use Stack as usual, but migrate your C/C++ main function to Haskell. Is this the best way to do it? Are there huge performance costs to this or anything I should be aware of?

duplode
  • 33,731
  • 7
  • 79
  • 150
George
  • 6,927
  • 4
  • 34
  • 67
  • "one way to solve this problem would be to use Stack as usual, but migrate your C/C++ main function to Haskell." -- This sounds highly suboptimal. I don't think this possibility makes your original question any less relevant. – duplode Nov 05 '16 at 23:37
  • suboptimal in the performance sense? – George Nov 05 '16 at 23:38
  • "suboptimal in the performance sense?" -- In that sense, I genuinely don't know. I just meant suboptimal in terms of project organisation. – duplode Nov 05 '16 at 23:42

1 Answers1

4

Stack can't really do this on its own.

There's support for generating so called "foreign libraries" added to Cabal, but it's not in a released version, yet. See commit 382143 This will produce a shared library that dynamically links against the dynamic versions of each Haskell package used.

You can build your package with stack and then after the fact you can assemble a single native library. In the Galua project we do this with a custom Setup.hs and a separate linking script.

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

Do note that for creating standalone libraries on Linux suitable for being linked into a shared library that you'll need to recompile GHC to generate PIC static libraries (macOS does this by default).

glguy
  • 1,090
  • 7
  • 8
  • Recompiling GHC just to get this working on Linux seems pretty daunting. Do you have any thoughts about force-migrating a C++ project into a Stack project (with Main located in Haskell)? Are there any intrinsic performance hits that this incurs or other problems I should be aware of? – George Dec 05 '16 at 13:47
  • Perhaps it might also be a better approach to manually compile all haskell files yourself with GHC (even the libraries that you download from upstream Hackage; i.e., you'll have to download them yourself and add them to your `Makefile`). This is, after all, how a typicall C++ project is organized with its own files. So why not extend this practice to Haskell within a C++ project? – George Dec 05 '16 at 13:53
  • Basically: does your experience with the Galua project offer any hints at some best practices here? Or are you satisficed with the approach you took by recompiling GHC with PIC support and then using stack to output a `libgalua.a`? – George Dec 05 '16 at 13:54