为什么必须从共享库调用hs_init(不是静态链接)

时间:2018-05-15 03:16:11

标签: haskell ffi

我写了一个简单的haskell模块,我可以将其转换为共享库:

{-# LANGUAGE ForeignFunctionInterface #-}

module Safe where

import Foreign.C.Types

add :: Int -> Int -> Int
add p q = p + q

hs_add :: CInt -> CInt -> CInt
hs_add a b = fromIntegral (add (fromIntegral a) (fromIntegral b))

foreign export ccall hs_add :: CInt -> CInt -> CInt

使用命令

ghc -O2 -dynamic -shared -fPIC -o libAdd.so Add.hs -lHSrts-ghc8.2.2

以下程序运行正常:

#include <stdio.h>
#include <dlfcn.h>
#include <HsFFI.h>

#define _LIB_INIT

int main(int argc, char *argv[])
{
    void* handle;

    static char *argv_N[] = { NULL }, **argv_ = argv_N;
    static int argc_ = 0;

    handle = dlopen("./libAdd.so", RTLD_NOW);
    if (!handle)
    {
        fprintf(stderr, "%s\n", dlerror());
        return 12;
    }
    else
        fprintf(stderr, "Successfully loaded library\n");

#ifdef _LIB_INIT
    void (*hs_init_l)(int *argc, char **argv[]) = dlsym(handle, "hs_init");
    hs_init_l(&argc_, &argv_);
#else
    hs_init(&argc_, &argv_);
#endif

    int (*hs_add)(int a, int b) = dlsym(handle, "hs_add");
    if (!hs_add)
    {
        fprintf(stderr, "Unable to load func : %s\n", dlerror());
        return 3;
    }
    else
        fprintf(stderr, "Successfully loaded function\n");

    int a=8, b=12;
    printf("%d + %d = %d\n", a, b, hs_add(a, b));

    dlclose(handle);

    return 0;
}

当我用

编译它时
ghc --make -no-hs-main -optc-O test.c -o test

但是,如果我删除定义_LIB_INIT的行,我会

Successfully loaded library
Successfully loaded function
newBoundTask: RTS is not initialised; call hs_init() first

换句话说,如果我从共享库中调用hs_init它可以正常工作,但如果我将其称为静态链接副本,则hs_add函数无法识别它。

这些之间有什么区别?

0 个答案:

没有答案