我正在Qt
( C++
) 中构建一个应用程序,它使用用 Haskell 编写的库作为后端。如何将Haskell
库的接口导出到C++
?
当 haskell 库中的函数使用简单类型时,int
或者float
这不是问题,但是当它们使用结构作为参数时呢?在这种情况下,最具可扩展性的解决方案是什么?
You'll need to write haskell code to marshal those datastructures into ones you can use in Haskell. The c2hs tool is very helpful in this regard: http://hackage.haskell.org/package/c2hs
While the tool appears to be designed for calling C from Haskell, it is also very useful for the opposite in one sense -- it allows you to marshall complex C types into Haskell types. Since the functions you expose will need to take C types (typically pointers to complex structures), then you can A) construct appropriate C structures, and B) use the features c2hs provides in order to then marshall those structures into Haskell in your exposed functions, so that you can work with them.
Also note that with c2hs style generated tools, you don't need to marshall everything. You can just generate accessors to peek into the parts of C data structures that you actually care about.
迟到了,但我想你想用这个:
特别是类似的东西foreign export ccall foo :: Int -> IO Int
。这将生成一个标题,例如:
#include "HsFFI.h"
extern HsInt foo(HsInt a0);
有关更多详细信息(例如如何集成到您的 C++ 程序中),请参阅链接。