c++ - 使用 haskell 构建动态库并在 C++ 中使用它

标签 c++ haskell g++ ghc ffi

我想构建一个包含 haskell 函数的动态库。我在 linux 上工作,想从 C++ 代码中调用这个动态库。

我使用了 http://wiki.python.org/moin/PythonVsHaskell 的示例并有以下文件:

Test.hs:

{-# LANGUAGE ForeignFunctionInterface #-}
module Test where

import Foreign.C.Types

hsfun :: CInt -> IO CInt
hsfun x = do
    putStrLn "Hello World"
    return (42 + x)

foreign export ccall
    hsfun :: CInt -> IO CInt

module_init.c:

#define CAT(a,b) XCAT(a,b)
#define XCAT(a,b) a ## b
#define STR(a) XSTR(a)
#define XSTR(a) #a

#include <HsFFI.h>

extern void CAT (__stginit_, MODULE) (void);

static void library_init (void) __attribute__ ((constructor));
static void
library_init (void)
{
  /* This seems to be a no-op, but it makes the GHCRTS envvar work. */
  static char *argv[] = { STR (MODULE) ".so", 0 }, **argv_ = argv;
  static int argc = 1;

  hs_init (&argc, &argv_);
  hs_add_root (CAT (__stginit_, MODULE));
}

static void library_exit (void) __attribute__ ((destructor));
static void
library_exit (void)
{
  hs_exit ();
}

现在我将这些文件编译为动态库:

$ ghc -dynamic -shared -fPIC -optc '-DMODULE=Test' Test.hs module_init.c -o libTest.so
[1 of 1] Compiling Test             ( Test.hs, Test.o )
Linking libTest.so ...

这会创建文件 Test_stub.h:

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

和Test_stub.c:

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

extern StgClosure Test_zdfhsfunzua165_closure;
HsInt32 hsfun(HsInt32 a1)
{
Capability *cap;
HaskellObj ret;
HsInt32 cret;
cap = rts_lock();
cap=rts_evalIO(cap,rts_apply(cap,(HaskellObj)runIO_closure,rts_apply(cap,&Test_zdfhsfunzua165_closure,rts_mkInt32(cap,a1))) ,&ret);
rts_checkSchedStatus("hsfun",cap);
cret=rts_getInt32(ret);
rts_unlock(cap);
return cret;
}
static void stginit_export_Test_zdfhsfunzua165() __attribute__((constructor));
static void stginit_export_Test_zdfhsfunzua165()
{getStablePtr((StgPtr) &Test_zdfhsfunzua165_closure);}
#ifdef __cplusplus
}
#endif

然后我创建一个cpp文件main.cpp:

#include "Test_stub.h"

#include <iostream>

using namespace std;

int main()
{
    cout << hsfun(5);
}

并且想要编译和链接它。但是当我调用 g++ 时,它会说:

$ g++ -I/usr/lib/ghc-7.0.3/include -L. -lTest main.cpp
/tmp/ccFP2AuB.o: In function `main':
main.cpp:(.text+0xa): undefined reference to `hsfun'
collect2: ld gab 1 als Ende-Status zurück

所以我将Test_stub.o文件添加到命令行(虽然我认为hsfun函数应该已经在libTest.so中定义了,它是通过-lTest参数添加的。我不认为,我应该链接Test_stub .o 文件到可执行文件中,因为我想使用动态链接),但这也不起作用:

$ g++ -I/usr/lib/ghc-7.0.3/include -L. -lTest main.cpp Test_stub.o
Test_stub.o: In function `hsfun':
Test_stub.c:(.text+0x9): undefined reference to `rts_lock'
Test_stub.c:(.text+0x16): undefined reference to `rts_mkInt32'
Test_stub.c:(.text+0x1d): undefined reference to `Test_zdfhsfunzua165_closure'
Test_stub.c:(.text+0x28): undefined reference to `rts_apply'
Test_stub.c:(.text+0x2f): undefined reference to `base_GHCziTopHandler_runIO_closure'
Test_stub.c:(.text+0x3a): undefined reference to `rts_apply'
Test_stub.c:(.text+0x4a): undefined reference to `rts_evalIO'
Test_stub.c:(.text+0x5c): undefined reference to `rts_checkSchedStatus'
Test_stub.c:(.text+0x66): undefined reference to `rts_getInt32'
Test_stub.c:(.text+0x70): undefined reference to `rts_unlock'
Test_stub.o: In function `stginit_export_Test_zdfhsfunzua165':
Test_stub.c:(.text.startup+0x3): undefined reference to `Test_zdfhsfunzua165_closure'
Test_stub.c:(.text.startup+0x8): undefined reference to `getStablePtr'
collect2: ld gab 1 als Ende-Status zurück

我必须链接 Test_stub.o 吗?如果是,为什么?我应该将哪些参数传递给链接器?

最佳答案

让 ghc 完成工作可能比与 g++ 摔跤更容易,

ghc main.cpp -o hithere -L. -lTest -lstdc++

按照您的方式创建共享库后为我完成了这项工作。我用 7.2.2 和 7.0.2 测试过,都在这里工作。

关于c++ - 使用 haskell 构建动态库并在 C++ 中使用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8690049/

相关文章:

c++ - 在另一个项目中包含继承类时出现抽象类的链接器错误

c++ - 未启用 SSE 指令集

python - 没有 DLL 的 SWIG + Python

haskell 错误: Could not deduce (Random a0) arising from

c++ - 任何为 C++ 提供 betainv 的库

c++ - 如果成员具有非平凡的 noexcept 赋值运算符,则默认 move 赋值不能显式地为 noexcept

haskell - 包含函数的数据类型的仿函数实例

haskell - 管道是否有 `liftIO` 等价物?

c++ - MinGW C++ : expected primary-expression before '/' token

qt - 使用windeployqt时为"Warning: Cannot find GCC installation directory. g++.exe must be in the path."