gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 05:48:51 25 4
gpt4 key购买 nike

我创建了以下 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);

我确实找不到任何使用外部函数接口(interface)的数组和结构的信息。我如何才能在 Haskell 中使用我的库?

我尝试使用以下示例作为基础:http://therning.org/magnus/archives/315但随后 hsc 文件被编译为仅包含上述 c 代码的 hs 文件(当然它无法编译)。

最佳答案

基本的 FFI 支持仅包括标量类型。你最终用地址算术做的所有其他事情。 section on foreign types FFI 文档中提供了基础知识,您可以在 FFI Cookbook 中找到示例.

您曾经可以使用 Green Card 等工具和 H/Direct为您生成编码和解码代码。由于我不明白的原因,这些工具已经很长时间没有更新了。据我所知,当前选择的工具是 hsc2hs

<小时/>

编辑:如评论中所述(感谢ephemient),c2hs也很受欢迎,而且由于 c2hs 来自 Manuel Chakravarty,因此它可能会很好。

关于haskell - 如何在结构上使用 Haskell 的 FFI?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/670221/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com