使用 GHC 的共享库

Shared library using GHC

提问人:varajala 提问时间:8/31/2021 最后编辑:varajala 更新时间:8/31/2021 访问量:319

问:

我正在尝试从 Haskell 源代码创建一个简单的共享库。 该库由两个文件组成:Add.hs 和 lib.c。 lib.c 提供了用于初始化和退出 haskell 运行时的函数。

地址.hs

module Add where

foreign export ccall add :: Int -> Int -> Int

add :: Int -> Int -> Int
add a b = a + b

库.c

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

#if defined(__GLASGOW_HASKELL__)
#include "Foo_stub.h"
#endif


int add(int a, int b);


void lib_init()
{
    int argc = 2;
    char *argv[] = { "+RTS", "-A32m", NULL };
    char **pargv = argv;

    hs_init(&argc, &pargv);
}


void lib_exit()
{
    hs_exit();
}

我正在使用以下命令构建共享库:

ghc -c Add.hs -dynamic -fPIC -odir ./obj -stubdir ./include
gcc -c lib.c -o ./obj/lib.o -I ./include -I /usr/lib/ghc/include
ghc ./obj/lib.o ./obj/Add.o -dynamic -shared -o libadd.so

这不会给我任何错误。但是,当我尝试将我的主程序与此库链接时,我得到以下输出:

gcc test.c -o ./obj/test.o -ladd -L. -o test-exe
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_writeTVarzh'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `reportStackOverflow'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `performMajorGC'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `getRTSStats'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_newMutVarzh'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stopProfTimer'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `getOrSetSystemEventThreadEventManagerStore'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_makeStablePtrzh'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_maskUninterruptiblezh'
/usr/bin/ld: ./libadd.so: undefined reference to `hs_exit'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_SRT_15_info'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_ap_5_upd_info'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `getRTSStatsEnabled'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `rtsSupportsBoundThreads'
/usr/bin/ld: /usr/lib/ghc/ghc-prim-0.5.3/libHSghc-prim-0.5.3-ghc8.6.5.so: undefined reference to `stg_newSmallArrayzh'
/usr/bin/ld: ./libadd.so: undefined reference to `rts_mkInt'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_readTVarIOzh'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_ap_ppp_fast'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `libdwPoolRelease'
/usr/bin/ld: /usr/lib/ghc/ghc-prim-0.5.3/libHSghc-prim-0.5.3-ghc8.6.5.so: undefined reference to `stg_SMALL_MUT_ARR_PTRS_FROZEN_DIRTY_info'
/usr/bin/ld: /usr/lib/ghc/ghc-prim-0.5.3/libHSghc-prim-0.5.3-ghc8.6.5.so: undefined reference to `stg_mkApUpd0zh'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_tryTakeMVarzh'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `newSpark'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_killThreadzh'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `errorBelch'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_ap_pppv_info'
/usr/bin/ld: ./libadd.so: undefined reference to `rts_checkSchedStatus'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_ap_0_fast'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_gc_unpt_r1'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_ap_pp_fast'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_forkzh'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `rts_getThreadId'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_SRT_3_info'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_ap_ppv_fast'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `debugBelch'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_ap_pppppp_info'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `resumeThread'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `getNumberOfProcessors'
/usr/bin/ld: /usr/lib/ghc/ghc-prim-0.5.3/libHSghc-prim-0.5.3-ghc8.6.5.so: undefined reference to `stg_cloneSmallMutableArrayzh'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_clearCCSzh'
/usr/bin/ld: /usr/lib/ghc/ghc-prim-0.5.3/libHSghc-prim-0.5.3-ghc8.6.5.so: undefined reference to `stg_casSmallArrayzh'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `reportHeapOverflow'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_retryzh'
/usr/bin/ld: ./libadd.so: undefined reference to `rts_lock'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_ap_pppp_fast'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `getOrSetSystemEventThreadIOManagerThreadStore'
/usr/bin/ld: /usr/lib/ghc/ghc-prim-0.5.3/libHSghc-prim-0.5.3-ghc8.6.5.so: undefined reference to `stg_compactAllocateBlockzh'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_ap_v_info'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_noDuplicatezh'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_sel_4_upd_info'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_ap_2_upd_info'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `lockFile'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `newCAF'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_readMVarzh'
/usr/bin/ld: /usr/lib/ghc/ghc-prim-0.5.3/libHSghc-prim-0.5.3-ghc8.6.5.so: undefined reference to `stg_cloneArrayzh'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_raisezh'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `getMonotonicNSec'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `setIOManagerControlFd'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `libdwPoolTake'
/usr/bin/ld: /usr/lib/ghc/ghc-prim-0.5.3/libHSghc-prim-0.5.3-ghc8.6.5.so: undefined reference to `stg_unsafeThawSmallArrayzh'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `__hscore_set_saved_termios'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_MUT_ARR_PTRS_FROZEN_DIRTY_info'
/usr/bin/ld: ./libadd.so: undefined reference to `rts_getInt'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_ap_ppppp_info'
/usr/bin/ld: /usr/lib/ghc/ghc-prim-0.5.3/libHSghc-prim-0.5.3-ghc8.6.5.so: undefined reference to `stg_compactGetFirstBlockzh'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_waitReadzh'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_ap_ppp_info'
/usr/bin/ld: /usr/lib/ghc/integer-gmp-1.0.2.0/libHSinteger-gmp-1.0.2.0-ghc8.6.5.so: undefined reference to `stg_decodeDoublezuInt64zh'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_gc_unbx_r1'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_ap_4_upd_info'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_getSparkzh'
/usr/bin/ld: /usr/lib/ghc/ghc-prim-0.5.3/libHSghc-prim-0.5.3-ghc8.6.5.so: undefined reference to `stg_newBCOzh'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_ap_6_upd_info'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_ap_pv_fast'
/usr/bin/ld: /usr/lib/ghc/ghc-prim-0.5.3/libHSghc-prim-0.5.3-ghc8.6.5.so: undefined reference to `stg_casArrayzh'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_catchRetryzh'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_putMVarzh'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_ap_pppppp_fast'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_unsafeThawArrayzh'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `backtraceFree'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_unmaskAsyncExceptionszh'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_labelThreadzh'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `getOrSetSystemTimerThreadIOManagerThreadStore'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `setNumCapabilities'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `cmp_thread'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_bh_upd_frame_info'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `getOrSetSystemTimerThreadEventManagerStore'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_maskAsyncExceptionszh'
/usr/bin/ld: /usr/lib/ghc/ghc-prim-0.5.3/libHSghc-prim-0.5.3-ghc8.6.5.so: undefined reference to `stg_copyArrayzh'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `rts_setMainThread'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_gc_pppp'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `hs_spt_key_count'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_ap_7_upd_info'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `setTimerManagerControlFd'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `libdwPoolClear'
/usr/bin/ld: ./libadd.so: undefined reference to `stg_ap_pp_info'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_gc_f1'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `suspendThread'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_SRT_9_info'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_SRT_4_info'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_getThreadAllocationCounterzh'
/usr/bin/ld: /usr/lib/ghc/ghc-prim-0.5.3/libHSghc-prim-0.5.3-ghc8.6.5.so: undefined reference to `stg_newArrayArrayzh'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_takeMVarzh'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_SRT_10_info'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `startProfTimer'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_ap_pppp_info'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `hs_free_stable_ptr'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_sel_3_upd_info'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_SRT_6_info'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_CHARLIKE_closure'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `hs_spt_keys'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_setThreadAllocationCounterzh'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_yieldzh'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_MUT_ARR_PTRS_DIRTY_info'
/usr/bin/ld: /usr/lib/ghc/ghc-prim-0.5.3/libHSghc-prim-0.5.3-ghc8.6.5.so: undefined reference to `stg_copyMutableArrayArrayzh'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_deRefWeakzh'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `rts_disableThreadAllocationLimit'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_newMVarzh'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_waitWritezh'
/usr/bin/ld: /usr/lib/ghc/ghc-prim-0.5.3/libHSghc-prim-0.5.3-ghc8.6.5.so: undefined reference to `stg_isMutableByteArrayPinnedzh'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_SRT_1_info'
/usr/bin/ld: /usr/lib/ghc/ghc-prim-0.5.3/libHSghc-prim-0.5.3-ghc8.6.5.so: undefined reference to `stg_SMALL_MUT_ARR_PTRS_DIRTY_info'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_ap_pv_info'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `setIOManagerWakeupFd'
/usr/bin/ld: /usr/lib/ghc/ghc-prim-0.5.3/libHSghc-prim-0.5.3-ghc8.6.5.so: undefined reference to `stg_compactGetNextBlockzh'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_newArrayzh'
/usr/bin/ld: /usr/lib/ghc/ghc-prim-0.5.3/libHSghc-prim-0.5.3-ghc8.6.5.so: undefined reference to `stg_copyMutableArrayzh'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `performGC'
/usr/bin/ld: /usr/lib/ghc/ghc-prim-0.5.3/libHSghc-prim-0.5.3-ghc8.6.5.so: undefined reference to `stg_compactContainsAnyzh'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_addCFinalizzerToWeakzh'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_SRT_2_info'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_catchzh'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `getProcessElapsedTime'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `__hscore_get_saved_termios'
/usr/bin/ld: /usr/lib/ghc/ghc-prim-0.5.3/libHSghc-prim-0.5.3-ghc8.6.5.so: undefined reference to `stg_freezzeArrayzh'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_catchSTMzh'
/usr/bin/ld: /usr/lib/ghc/ghc-prim-0.5.3/libHSghc-prim-0.5.3-ghc8.6.5.so: undefined reference to `stg_compactNewzh'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_sel_0_upd_info'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `forkOS_createThread'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_SRT_16_info'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_gc_d1'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_IND_STATIC_info'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_newPinnedByteArrayzh'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_gc_noregs'
/usr/bin/ld: /usr/lib/ghc/ghc-prim-0.5.3/libHSghc-prim-0.5.3-ghc8.6.5.so: undefined reference to `stg_getApStackValzh'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `hs_spt_lookup'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_SRT_5_info'
/usr/bin/ld: ./libadd.so: undefined reference to `foreignExportStablePtr'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_SRT_8_info'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_traceMarkerzh'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_deRefStablePtrzh'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `RtsFlags'
/usr/bin/ld: /usr/lib/ghc/ghc-prim-0.5.3/libHSghc-prim-0.5.3-ghc8.6.5.so: undefined reference to `stg_freezzeSmallArrayzh'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_sel_2_upd_info'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_finalizzeWeakzh'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_forkOnzh'
/usr/bin/ld: /usr/lib/ghc/ghc-prim-0.5.3/libHSghc-prim-0.5.3-ghc8.6.5.so: undefined reference to `stg_compactResizzezh'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `getProgArgv'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `unlockFile'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `_assertFail'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_ap_p_info'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `getFullProgArgv'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_SRT_7_info'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `setProgArgv'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `shutdownHaskellAndSignal'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `shutdownHaskellAndExit'
/usr/bin/ld: /usr/lib/ghc/ghc-prim-0.5.3/libHSghc-prim-0.5.3-ghc8.6.5.so: undefined reference to `stg_compactAddWithSharingzh'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_isEmptyMVarzh'
/usr/bin/ld: /usr/lib/ghc/ghc-prim-0.5.3/libHSghc-prim-0.5.3-ghc8.6.5.so: undefined reference to `stg_isByteArrayPinnedzh'
/usr/bin/ld: ./libadd.so: undefined reference to `rts_apply'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_makeStableNamezh'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_traceEventzh'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_readTVarzh'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_mkWeakzh'
/usr/bin/ld: /usr/lib/ghc/ghc-prim-0.5.3/libHSghc-prim-0.5.3-ghc8.6.5.so: undefined reference to `stg_compactAddzh'
/usr/bin/ld: /usr/lib/ghc/ghc-prim-0.5.3/libHSghc-prim-0.5.3-ghc8.6.5.so: undefined reference to `stg_compactContainszh'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_ap_p_fast'
/usr/bin/ld: ./libadd.so: undefined reference to `rts_evalIO'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_ap_n_fast'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `libdwLookupLocation'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_sel_1_upd_info'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_sel_0_noupd_info'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_ARR_WORDS_info'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `rts_enableThreadAllocationLimit'
/usr/bin/ld: /usr/lib/ghc/ghc-prim-0.5.3/libHSghc-prim-0.5.3-ghc8.6.5.so: undefined reference to `stg_compactSizzezh'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_isCurrentThreadBoundzh'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_newTVarzh'
/usr/bin/ld: /usr/lib/ghc/ghc-prim-0.5.3/libHSghc-prim-0.5.3-ghc8.6.5.so: undefined reference to `stg_copyArrayArrayzh'
/usr/bin/ld: /usr/lib/ghc/ghc-prim-0.5.3/libHSghc-prim-0.5.3-ghc8.6.5.so: undefined reference to `stg_unpackClosurezh'
/usr/bin/ld: /usr/lib/ghc/ghc-prim-0.5.3/libHSghc-prim-0.5.3-ghc8.6.5.so: undefined reference to `stg_thawSmallArrayzh'
/usr/bin/ld: ./libadd.so: undefined reference to `rts_unlock'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_atomicallyzh'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_threadStatuszh'
/usr/bin/ld: /usr/lib/ghc/integer-gmp-1.0.2.0/libHSinteger-gmp-1.0.2.0-ghc8.6.5.so: undefined reference to `stg_shrinkMutableByteArrayzh'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_tryPutMVarzh'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_raiseIOzh'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_ap_pppv_fast'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_decodeFloatzuIntzh'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_newAlignedPinnedByteArrayzh'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_ap_v_fast'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_sel_6_upd_info'
/usr/bin/ld: /usr/lib/ghc/ghc-prim-0.5.3/libHSghc-prim-0.5.3-ghc8.6.5.so: undefined reference to `stg_copySmallArrayzh'
/usr/bin/ld: /usr/lib/ghc/ghc-prim-0.5.3/libHSghc-prim-0.5.3-ghc8.6.5.so: undefined reference to `stg_thawArrayzh'
/usr/bin/ld: /usr/lib/ghc/integer-gmp-1.0.2.0/libHSinteger-gmp-1.0.2.0-ghc8.6.5.so: undefined reference to `stg_resizzeMutableByteArrayzh'
/usr/bin/ld: ./libadd.so: undefined reference to `hs_init'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_sel_1_noupd_info'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_atomicModifyMutVarzh'
/usr/bin/ld: /usr/lib/ghc/ghc-prim-0.5.3/libHSghc-prim-0.5.3-ghc8.6.5.so: undefined reference to `stg_compactFixupPointerszh'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_numSparkszh'
/usr/bin/ld: /usr/lib/ghc/ghc-prim-0.5.3/libHSghc-prim-0.5.3-ghc8.6.5.so: undefined reference to `stg_decodeDoublezu2Intzh'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `enabled_capabilities'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `dirty_MUT_VAR'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_sig_install'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_ap_3_upd_info'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_ap_ppv_info'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `rts_mkStablePtr'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `freeHaskellFunctionPtr'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `getOrSetGHCConcSignalSignalHandlerStore'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_upd_frame_info'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_sel_5_upd_info'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_newByteArrayzh'
/usr/bin/ld: /usr/lib/ghc/ghc-prim-0.5.3/libHSghc-prim-0.5.3-ghc8.6.5.so: undefined reference to `stg_cloneSmallArrayzh'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_tryReadMVarzh'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_mkWeakNoFinalizzerzh'
/usr/bin/ld: /usr/lib/ghc/ghc-prim-0.5.3/libHSghc-prim-0.5.3-ghc8.6.5.so: undefined reference to `stg_casMutVarzh'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_ap_ppppp_fast'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_gc_ppp'
/usr/bin/ld: /usr/lib/ghc/integer-gmp-1.0.2.0/libHSinteger-gmp-1.0.2.0-ghc8.6.5.so: undefined reference to `__int_encodeDouble'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_getMaskingStatezh'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_gc_pp'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `libdwGetBacktrace'
/usr/bin/ld: /usr/lib/ghc/ghc-prim-0.5.3/libHSghc-prim-0.5.3-ghc8.6.5.so: undefined reference to `stg_copySmallMutableArrayzh'
/usr/bin/ld: /usr/lib/ghc/ghc-prim-0.5.3/libHSghc-prim-0.5.3-ghc8.6.5.so: undefined reference to `stg_cloneMutableArrayzh'
/usr/bin/ld: /usr/lib/ghc/base-4.12.0.0/libHSbase-4.12.0.0-ghc8.6.5.so: undefined reference to `stg_delayzh'
collect2: error: ld returned 1 exit status
make: *** [makefile:14: test] Error 1

当我使用 GHC 生成的存根头文件并将主程序链接到 Add.o 文件时,我没有收到任何错误。以下代码运行正常:

main.c

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

#if defined(__GLASGOW_HASKELL__)
#include "Foo_stub.h"
#endif


int add(int a, int b);


int main(int argc, char *argv[])
{
    hs_init(&argc, &argv);

    printf("1 + 2 == %d\n", add(1, 2));

    hs_exit();
    return 0;
}

工作可执行文件与以下项链接:

ghc ./obj/main.o ./obj/Add.o -no-hs-main -o main-exe

我尝试在网上搜索答案,但还没有找到任何有用的东西。 有关如何完成共享图书馆的任何帮助将不胜感激。 我在 Ubuntu 20.04 和 GHC 8.6.5 上。

我是 Haskell 的初学者......

Linux Haskell 共享库 链接器错误

评论

1赞 freestyle 8/31/2021
编译时尝试添加-lHSrts-ghc8.6.5Add.hs
0赞 arrowd 8/31/2021
如果你是一个完全的初学者,为什么不创建适当的阴谋集团包并使用节呢?这样,您就完全不必弄乱命令行标志。foreign-library

答:

5赞 varajala 8/31/2021 #1

经过更多的资源研究,我发现共享库必须显式链接到 Haskell rts 库。文件是 /usr/lib/ghc/rts/libHSrts-ghc8.6.5.so。我将构建库的命令更改为以下内容:

ghc ./obj/lib.o ./obj/Add.o -dynamic -shared -lHSrts-ghc8.6.5 -L/usr/lib/ghc/rts -o libadd.so

并使用以下命令构建了加载此库的测试程序:

gcc test.c -o ./obj/test.o -ladd -L. -Wl,-rpath,. -o test-exe

这很好用!

评论

1赞 freestyle 8/31/2021
也许你应该用标志编译你的 C 文件。-fPIC