使用C ++中的haskell函数:未定义的引用错误

时间:2011-12-28 17:09:42

标签: c++ haskell g++ ghc ffi

我想用C ++调用haskell函数并使用http://www.haskell.org/ghc/docs/7.0.2/html/users_guide/ffi-ghc.html上的教程

所以我有一个haskell文件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))

并致电

ghc Foo.hs

创建了一个Foo_stub.h:

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

一个Foo_stub.c:

#define IN_STG_CODE 0
#include "Rts.h"
#include "Stg.h"
#ifdef __cplusplus
extern "C" {
#endif

extern StgClosure Foo_zdffoozualy_closure;
HsInt foo(HsInt a1)
{
Capability *cap;
HaskellObj ret;
HsInt cret;
cap = rts_lock();
cap=rts_evalIO(cap,rts_apply(cap,(HaskellObj)runIO_closure,rts_apply(cap, Foo_zdffoozualy_closure,rts_mkInt(cap,a1))) ,&ret);
rts_checkSchedStatus("foo",cap);
cret=rts_getInt(ret);
rts_unlock(cap);
return cret;
}
static void stginit_export_Foo_zdffoozualy() __attribute__((constructor));
static void stginit_export_Foo_zdffoozualy()
{getStablePtr((StgPtr) &Foo_zdffoozualy_closure);}
#ifdef __cplusplus
}
#endif

它还创建了一个Foo_stub.o文件。

我现在创建了一个main.cpp文件:

#include "Foo_stub.h"
int main()
{
  foo(3);
}

并尝试编译它。

使用g ++失败:

$ g++ -I/usr/lib/ghc-7.0.3/include/ main.cpp Foo_stub.o
Foo_stub.o: In function `foo':
Foo_stub.c:(.text+0xa): undefined reference to `rts_lock'
Foo_stub.c:(.text+0x18): undefined reference to `rts_mkInt'
Foo_stub.c:(.text+0x20): undefined reference to `Foo_zdffoozualy_closure'
Foo_stub.c:(.text+0x28): undefined reference to `rts_apply'
Foo_stub.c:(.text+0x30): undefined reference to `base_GHCziTopHandler_runIO_closure'
Foo_stub.c:(.text+0x38): undefined reference to `rts_apply'
Foo_stub.c:(.text+0x48): undefined reference to `rts_evalIO'
Foo_stub.c:(.text+0x58): undefined reference to `rts_checkSchedStatus'
Foo_stub.c:(.text+0x62): undefined reference to `rts_getInt'
Foo_stub.c:(.text+0x6d): undefined reference to `rts_unlock'
Foo_stub.o: In function `stginit_export_Foo_zdffoozualy':
Foo_stub.c:(.text+0x80): undefined reference to `Foo_zdffoozualy_closure'
Foo_stub.c:(.text+0x85): undefined reference to `getStablePtr'
collect2: ld returned status 1

所以我尝试了ghc --make。但这也失败了:

$ ghc --make main.cpp Foo_stub.o
cc1plus: Warning: Option »-Wimplicit« is valid for C/ObjC, but not for C++ [activated by default]
Foo_stub.o: In function `foo':
Foo_stub.c:(.text+0x20): undefined reference to `Foo_zdffoozualy_closure'
Foo_stub.o: In function `stginit_export_Foo_zdffoozualy':
Foo_stub.c:(.text+0x80): undefined reference to `Foo_zdffoozualy_closure'
collect2: ld returned status 1

我该如何编译?

1 个答案:

答案 0 :(得分:7)

试试这个:

$ ghc --make -no-hs-main main.cpp Foo.hs

我认为问题在于GHC认为你传入的存根对象文件只是另一个要正常链接的对象,但是你没有传递Foo.o文件,所以存根代码什么也没有要链接到!直接传入源文件应解决它。 (这只是基于文档和您看到的错误的猜想,我还没有测试过。)

我认为你可以像这样单独编译:

$ ghc --make Foo.hs
$ ghc --make -no-hs-main main.cpp Foo.o

但我不确定。您可能还希望将Haskell代码构建为shared library,然后通常使用g++将其链接到主程序。

当您的程序的入口点不是Haskell值-no-hs-main时,

Main.main是必需的,如您链接的文档中所述。