作为将 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.h
和foo_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() { …
Run Code Online (Sandbox Code Playgroud)