如何使用Haskell的堆栈构建工具导出要由C/C ++消耗的库? [英] How to Use Haskell's Stack Build Tool to Export a Library to Be Consumed by C/C++?
问题描述
假设有人正在使用stack
生成工具制作一个Haskell库(从Hackage等导入软件包)与main
位于C/C中的C/C ++项目一起使用. C ++ .
Suppose one is using the stack
build 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++.
假设您的项目名为Lib.hs
(使用来自hackage的外部库),是否可以使用堆栈导出Lib.o
,Lib.hi
和Lib_stub.h
供C/C ++使用像gcc
或g++
这样的编译器?
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++
?
编辑:一个相关的问题可能是:如何将Stack用作构建工具,以便将main
放置在C/中的Haskell& C/C ++项目中使用C ++?
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++?
经过反思,一种解决此问题的方法是照常使用Stack,但是将您的C/C ++主函数迁移到Haskell.这是最好的方法吗?我是否应该为此付出任何巨大的性能成本?
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?
推荐答案
Stack不能真正做到这一点.
Stack can't really do this on its own.
在Cabal中添加了对生成所谓的外部库"的支持,但尚未发布.参见commit 382143 这将产生一个共享库,该库动态链接到每个库的动态版本已使用Haskell软件包.
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.
您可以使用堆栈来构建软件包,然后在事实之后就可以组装一个本机库.在 Galua 项目中,我们使用自定义的链接脚本.
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.
此链接过程的结果是,您获得了一个适合包含在C项目中的独立静态链接库:libgalua.a
.
The result of this linking process is that you get a standalone statically linked library suitable for inclusion in a C project: libgalua.a
.
请注意,要在Linux上创建适合链接到共享库的独立库,您将需要重新编译GHC以生成PIC静态库(macOS默认情况下会这样做).
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).
这篇关于如何使用Haskell的堆栈构建工具导出要由C/C ++消耗的库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!