如何在结构上使用 Haskell 的 FFI?
我创建了以下用于读取图像的 C 库:
typedef struct {
unsigned int height;
unsigned int width;
unsigned char* red; //length=height*width
unsigned char* green;
unsigned char* blue;
} Contents;
Contents readJPEGFile(const char* inFilename);
我无法通过外部函数接口真正找到使用数组和结构的任何信息。 我如何才能在 Haskell 中使用我的库?
我尝试使用以下示例作为基础: http://therning.org/magnus/archives/315< /a> 但随后 hsc 文件被编译为仅包含上述 c 代码而仅包含其他内容的 hs 文件(当然它无法编译)。
I have created the following C library for reading an image:
typedef struct {
unsigned int height;
unsigned int width;
unsigned char* red; //length=height*width
unsigned char* green;
unsigned char* blue;
} Contents;
Contents readJPEGFile(const char* inFilename);
I can't really find any info using arrays and structs with the Foreign Function Interface.
How would I proceed to be able to use my library in Haskell?
I tried to use the following example as a base: http://therning.org/magnus/archives/315 but then the hsc file was compiled down to a hs file that only contained the above c-code and nothing more (and of course it can't be compiled).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
基本 FFI 支持仅包括标量类型。 你最终用地址算术做的所有其他事情。 有关外国类型的部分 FFI 文档中提供了基础知识,您可以在 FFI Cookbook 中找到示例。
曾经,您可以使用诸如绿卡和H/Direct 为您生成编组和解组代码。 由于我不明白的原因,这些工具已经很长时间没有更新了。 据我所知,当前选择的工具是
hsc2hs
。编辑:如评论中所述(感谢ephemient),
c2hs
也很流行,而且由于
c2hs
来自 Manuel Chakravarty,因此它可能会很好。The basic FFI support includes only scalar types. Everything else you wind up doing with address arithmetic. The section on foreign types in the FFI documentation gives the basics, and you can find an example in the FFI Cookbook.
At one time you could use tools like Green Card and H/Direct to generate marshalling and unmarshalling code for you. For reasons I don't understand, these tools have not been updated in a long time. As far as I can tell the current tool of choice is
hsc2hs
.Edit: As noted in comment (thanks ephemient),
c2hs
is also popular, and sincec2hs
is from Manuel Chakravarty it is likely to be good.听起来好像您遇到了构建问题; 我似乎记得当我将 FFI 接口写入 Windows Win32 DDEML 库时,我使用了您引用的页面作为示例。 例如,我们使用的结构之一是
#include "ddeml.h"
将其引入到DDEML.hsc
文件中。 我们通过以下方式访问它:不幸的是,我目前无法向您展示它会编译成什么,因为我手边没有 Windows 盒子,但生成的代码与上面一样,除了
#size HSZPAIR 替换为
(64)
或其他内容等等。(如果您确实想查看生成的内容,或者需要帮助进行构建,请给我发送电子邮件,我会帮助您。)
It sounds as if you have a build issue; I do seem to recall that I used the very page you reference as an example when I was writing an FFI interface into the Windows Win32 DDEML library. For example, one of the structures we use is
#include "ddeml.h"
brings this in to theDDEML.hsc
file. We access it with:Unfortunately, I can't show you what this compiles to at the moment because I don't have a Windows box handy, but the generated code was just as above, except with
#size HSZPAIR
replaced with(64)
or whatever and so on.(If you really want to see what was generated, or need help doing your build, e-mail me and I'll help you out.)
Hackage 有几个使用 FFI 的软件包,您可以查看示例。
Hackage has several packages which use FFI which you could look at for examples.