c - 尝试为 C 导出 Haskell 库

标签 c haskell

我复制了the Haskell FFI guide中的示例代码作为将我的 Haskell 程序导出为 C 库的第一步,但无法编译。我有 foo.hs:

module Foo where
foreign export ccall foo :: Int -> IO Int

foo :: Int -> IO Int
foo n = return (length (f n))

f :: Int -> [Int]
f 0 = []
f n = n:(f (n-1))

这成功编译为 foo_stub.hfoo_stub.o。这是 foo_stub.h:

#include "HsFFI.h"
#ifdef __cplusplus
extern "C" {
#endif
extern HsInt foo(HsInt a1);
#ifdef __cplusplus
}
#endif

但是我的 C 程序没有编译:

#include "foo_stub.h"
main() { foo(1); } // I realize this is probably wrong, and would also like advice on doing this part correctly, but note the error is not here.

错误:

gcc foo.c
In file included from foo.c:1:0:
foo_stub.h:1:19: fatal error: HsFFI.h: No such file or directory
 #include "HsFFI.h"
                   ^
compilation terminated.

我假设我丢失了一些头文件或者没有正确地将 gcc 指向它们。如有必要,我可以提供更多信息。关于如何解决此问题的任何想法?

最佳答案

我在 /usr/local/lib/ghc-7.10.2/include/HsFFI.h 找到了“HsFFI.h”。您应该能够使用 -I 选项指示 GCC 到那里查看。更多信息 here .

关于c - 尝试为 C 导出 Haskell 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33272417/

相关文章:

haskell - 使用镜头替换(键,值)列表的特定元素

haskell - 如何生成随机的类型化函数

haskell - `threadDelay (maxBound::Int)` 会触发 GHC 错误还是什么?

c - 我无法将一些 C 代码转换为 Delphi

c - 使用指向字符串的指针数组将字符串中的子字符串替换为c中的另一个子字符串

C程序-找到数组中最大的序列

haskell - 使用管道的顺序二进制数据解码

c - 我如何在 c 中使用 fork() 对一个包含 2 个子项的数组进行排序

C 程序 - 我无法在后台运行 shell 脚本

haskell - 显示一般情况下每个记录显示一行的实现