尝试为C导出Haskell库 [英] Trying to export a Haskell lib for C
问题描述
我将示例代码复制到 Haskell FFI中指南,这是将Haskell程序导出为C库的第一步,但无法对其进行编译.我有 foo.hs
:
I copied the example code in the Haskell FFI guide as a first step to exporting my Haskell program as a C library, but can't get it to compile. I have 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.h
和 foo_stub.o
.这是 foo_stub.h
:
#include "HsFFI.h"
#ifdef __cplusplus
extern "C" {
#endif
extern HsInt foo(HsInt a1);
#ifdef __cplusplus
}
#endif
但是我的C程序没有编译:
But then my C program didn't compile:
#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正确指向它们.如有必要,我可以提供更多信息.有关如何解决此问题的任何想法?
I assume I'm missing some header files or haven't pointed gcc to them correctly. I can provide more information if necessary. Any ideas on how to fix this?
推荐答案
我在/usr/local/lib/ghc-7.10.2/include/HsFFI.h
上找到了"HsFFI.h".您应该可以使用 -I
选项指导GCC到那里查看.详细信息此处.
I found "HsFFI.h" at /usr/local/lib/ghc-7.10.2/include/HsFFI.h
. You should be able to direct GCC to look there with the -I
option. More information here.
这篇关于尝试为C导出Haskell库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!