如何在结构上使用 Haskell 的 FFI?

发布于 2024-07-15 18:07:39 字数 535 浏览 5 评论 0原文

我创建了以下用于读取图像的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

痴情 2024-07-22 18:07:39

基本 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 since c2hs is from Manuel Chakravarty it is likely to be good.

星光不落少年眉 2024-07-22 18:07:39

听起来好像您遇到了构建问题; 我似乎记得当我将 FFI 接口写入 Windows Win32 DDEML 库时,我使用了您引用的页面作为示例。 例如,我们使用的结构之一是

typedef struct tagHSZPAIR {
    HSZ     hszSvc;
    HSZ     hszTopic;
} HSZPAIR, *PHSZPAIR;

#include "ddeml.h" 将其引入到 DDEML.hsc 文件中。 我们通过以下方式访问它:

data HSZPair = HSZPair HSZ HSZ
instance Storable HSZPair where
    sizeOf _                     = (#size HSZPAIR)
    alignment                    = sizeOf
    peek ptr                     = do svc   <- (#peek HSZPAIR, hszSvc)   ptr
                                      topic <- (#peek HSZPAIR, hszTopic) ptr
                                      return $ HSZPair svc topic
    poke ptr (HSZPair svc topic) = do (#poke HSZPAIR, hszSvc)   ptr svc
                                      (#poke HSZPAIR, hszTopic) ptr topic

不幸的是,我目前无法向您展示它会编译成什么,因为我手边没有 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

typedef struct tagHSZPAIR {
    HSZ     hszSvc;
    HSZ     hszTopic;
} HSZPAIR, *PHSZPAIR;

#include "ddeml.h" brings this in to the DDEML.hsc file. We access it with:

data HSZPair = HSZPair HSZ HSZ
instance Storable HSZPair where
    sizeOf _                     = (#size HSZPAIR)
    alignment                    = sizeOf
    peek ptr                     = do svc   <- (#peek HSZPAIR, hszSvc)   ptr
                                      topic <- (#peek HSZPAIR, hszTopic) ptr
                                      return $ HSZPair svc topic
    poke ptr (HSZPair svc topic) = do (#poke HSZPAIR, hszSvc)   ptr svc
                                      (#poke HSZPAIR, hszTopic) ptr topic

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.)

你げ笑在眉眼 2024-07-22 18:07:39

Hackage 有几个使用 FFI 的软件包,您可以查看示例。

Hackage has several packages which use FFI which you could look at for examples.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文