尝试为 C 导出 Haskell 库

Ell*_*sky 1 c haskell

作为将 Haskell 程序导出为 C 库的第一步,我复制了Haskell FFI 指南中的示例代码,但无法编译。我有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))
Run Code Online (Sandbox Code Playgroud)

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

#include "HsFFI.h"
#ifdef __cplusplus
extern "C" {
#endif
extern HsInt foo(HsInt a1);
#ifdef __cplusplus
}
#endif
Run Code Online (Sandbox Code Playgroud)

但后来我的 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.
Run Code Online (Sandbox Code Playgroud)

错误:

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.
Run Code Online (Sandbox Code Playgroud)

我假设我缺少一些头文件或者没有正确地将 gcc 指向它们。如有必要,我可以提供更多信息。有想法该怎么解决这个吗?

cro*_*eea 5

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

  • 对于未来的读者,请使用 GHC 打印出 libdir:`ghc --print-libdir`。我编译了一个程序,如下所示: `gcc main.c -I "\`ghc --print-libdir\`/include"` (2认同)