OSError: /usr/lib/ghc/ghc-prim-0.5.2.0/libHSghc-prim-0.5.2.0-ghc8.4.4.so: undefined symbol: stg_gc_unpt_r1

OSError: /usr/lib/ghc/ghc-prim-0.5.2.0/libHSghc-prim-0.5.2.0-ghc8.4.4.so: undefined symbol: stg_gc_unpt_r1

我正在尝试 运行 这个来自 github 的例子: https://github.com/nh2/haskell-from-python/blob/master/Makefile 我想从另一种语言中了解 运行 一种语言。我不确定 FFI 是否在这里发挥了某种作用,我只是不知道该说些什么。

我正在 运行在 WSL - debian 上编写代码。我也在 windows 上尝试 运行ning 它,但我遇到了同样的问题。 我的错误是在 运行ning 'make' 之后,然后 'python program.py' 我得到:

Traceback (most recent call last):
  File "program.py", line 5, in <module>
    lib = cdll.LoadLibrary("./libffi-example.so")
  File "/usr/lib/python2.7/ctypes/__init__.py", line 444, in LoadLibrary
    return self._dlltype(name)
  File "/usr/lib/python2.7/ctypes/__init__.py", line 366, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: /usr/lib/ghc/ghc-prim-0.5.2.0/libHSghc-prim-0.5.2.0-ghc8.4.4.so: undefined symbol: stg_gc_unpt_r1

我不知道 stg_gc_unput_r1 是什么,它不是任何代码中的变量。我还查看了一些 GHC 文档,但找不到任何内容。 我在存储库上的问题没有得到任何关注,虽然我发布它才一天,但代码已经有好几年了。

我没有使用 Haskell 和 ghc 的经验,使用 Python 的经验也很少,因此,如果您能提供帮助和耐心等待,我们将不胜感激。

检查文件 /usr/lib/ghc/rts/libHSrts-ghc8.4.4.so 确实存在后,这里是 运行ning ldd libffi-example.so 的输出:

linux-vdso.so.1 (0x00007ffd5f5ee000)
        libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fcc439c7000)
        libHSbase-4.11.1.0-ghc8.4.4.so => /usr/lib/ghc/base-4.11.1.0/libHSbase-4.11.1.0-ghc8.4.4.so (0x00007fcc4304c000)
        libHSinteger-gmp-1.0.2.0-ghc8.4.4.so => /usr/lib/ghc/integer-gmp-1.0.2.0/libHSinteger-gmp-1.0.2.0-ghc8.4.4.so (0x00007fcc43009000)
        libHSghc-prim-0.5.2.0-ghc8.4.4.so => /usr/lib/ghc/ghc-prim-0.5.2.0/libHSghc-prim-0.5.2.0-ghc8.4.4.so (0x00007fcc42b7b000)
        libgmp.so.10 => /usr/lib/x86_64-linux-gnu/libgmp.so.10 (0x00007fcc42af8000)
        libatomic.so.1 => /usr/lib/x86_64-linux-gnu/libatomic.so.1 (0x00007fcc42aec000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fcc4292b000)
        /lib64/ld-linux-x86-64.so.2 (0x00007fcc43b58000)
        libffi.so.6 => /usr/lib/x86_64-linux-gnu/libffi.so.6 (0x00007fcc42921000)
        libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fcc42900000)

我尝试过的事情:

我试图在 WSL 安装下使用全新的 Debian 来重现您的问题。我在 Windows 10 下的 WSL 1 下 运行ning Debian“靶心”。那个 Debian 版本必须比你的更新一点,因为 GHC 软件包是 版本 8.8.4 8.4.4,但这似乎是唯一的区别。

使用 Git 存储库的干净副本(提交 9c3b6315),仅更改 Makefile(完全如下,除了通常的“制表符”缩进):

GHC=ghc
GHC_RUNTIME_LINKER_FLAG=-L/usr/lib/ghc/rts -lHSrts-ghc8.8.4

libffi-example.so: Example.o wrapper.o
    $(GHC) -o $@ -shared -dynamic -fPIC $^ $(GHC_RUNTIME_LINKER_FLAG)

Example_stub.h Example.o: Example.hs
    $(GHC) -c -dynamic -fPIC Example.hs

wrapper.o: wrapper.c Example_stub.h
    $(GHC) -c -dynamic -fPIC wrapper.c

clean:
    rm -f *.hi *.o *_stub.[ch]

clean-all:
    rm -f *.hi *.o *_stub.[ch] *.so


# Runs the example Python program
example: libffi-example.so
    python program.py

我可以运行以下命令:

buhr@DESKTOP-585NUKU:~/src/haskell-from-python$ make clean-all
rm -f *.hi *.o *_stub.[ch] *.so
buhr@DESKTOP-585NUKU:~/src/haskell-from-python$ make example
ghc -c -dynamic -fPIC Example.hs
ghc -c -dynamic -fPIC wrapper.c
ghc -o libffi-example.so -shared -dynamic -fPIC Example.o wrapper.o -L/usr/lib/ghc/rts -lHSrts-ghc8.8.4
python program.py
Haskell fib(0): 0
Haskell fib(1): 1
Haskell fib(2): 1
Haskell fib(3): 2
Haskell fib(4): 3
Haskell fib(5): 5
Haskell fib(6): 8
Haskell fib(7): 13
Haskell fib(8): 21
Haskell fib(9): 34
buhr@DESKTOP-585NUKU:~/src/haskell-from-python$ ldd libffi-example.so 
    linux-vdso.so.1 (0x00007ffff6925000)
    libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f7c7b810000)
    libHSrts-ghc8.8.4.so => /usr/lib/ghc/rts/libHSrts-ghc8.8.4.so (0x00007f7c7b7a0000)
    libHSbase-4.13.0.0-ghc8.8.4.so => /usr/lib/ghc/base-4.13.0.0/libHSbase-4.13.0.0-ghc8.8.4.so (0x00007f7c7ae00000)
    libHSinteger-gmp-1.0.2.0-ghc8.8.4.so => /usr/lib/ghc/integer-gmp-1.0.2.0/libHSinteger-gmp-1.0.2.0-ghc8.8.4.so (0x00007f7c7adc0000)
    libHSghc-prim-0.5.3-ghc8.8.4.so => /usr/lib/ghc/ghc-prim-0.5.3/libHSghc-prim-0.5.3-ghc8.8.4.so (0x00007f7c7a920000)
    libgmp.so.10 => /usr/lib/x86_64-linux-gnu/libgmp.so.10 (0x00007f7c7a880000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f7c7a6b0000)
    /lib64/ld-linux-x86-64.so.2 (0x00007f7c7b973000)
    librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f7c7a6a0000)
    libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f7c7a690000)
    libffi.so.7 => /usr/lib/x86_64-linux-gnu/libffi.so.7 (0x00007f7c7a680000)
    libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f7c7a65e000)

buhr@DESKTOP-585NUKU:~/src/haskell-from-python$ 

如果我故意破坏 Makefile,比如注释掉 GHC_RUNTIME_LINKER_FLAG link 或拼写错误,这样它实际上就不会包含在 libffi-example.so link 命令,那么我可以准确地复制你的问题。请注意以下抄本中 Makefile 第二行中的 _MISTAKE,这会阻止 RTS 库实际被 linked:

buhr@DESKTOP-585NUKU:~/src/haskell-from-python$ head Makefile
GHC=ghc
GHC_RUNTIME_LINKER_FLAG_MISTAKE=-L/usr/lib/ghc/rts -lHSrts-ghc8.8.4

libffi-example.so: Example.o wrapper.o
    $(GHC) -o $@ -shared -dynamic -fPIC $^ $(GHC_RUNTIME_LINKER_FLAG)

Example_stub.h Example.o: Example.hs
    $(GHC) -c -dynamic -fPIC Example.hs

wrapper.o: wrapper.c Example_stub.h
buhr@DESKTOP-585NUKU:~/src/haskell-from-python$ make clean-all
rm -f *.hi *.o *_stub.[ch] *.so
buhr@DESKTOP-585NUKU:~/src/haskell-from-python$ make example
ghc -c -dynamic -fPIC Example.hs
ghc -c -dynamic -fPIC wrapper.c
ghc -o libffi-example.so -shared -dynamic -fPIC Example.o wrapper.o 
python program.py
Traceback (most recent call last):
  File "program.py", line 4, in <module>
    lib = cdll.LoadLibrary("./libffi-example.so")
  File "/usr/lib/python2.7/ctypes/__init__.py", line 444, in LoadLibrary
    return self._dlltype(name)
  File "/usr/lib/python2.7/ctypes/__init__.py", line 366, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: /usr/lib/ghc/ghc-prim-0.5.3/libHSghc-prim-0.5.3-ghc8.8.4.so: undefined symbol: stg_gc_unpt_r1
make: *** [Makefile:22: example] Error 1
buhr@DESKTOP-585NUKU:~/src/haskell-from-python$ ldd libffi-example.so
    linux-vdso.so.1 (0x00007fffea75d000)
    libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f83175e0000)
    libHSbase-4.13.0.0-ghc8.8.4.so => /usr/lib/ghc/base-4.13.0.0/libHSbase-4.13.0.0-ghc8.8.4.so (0x00007f8316c40000)
    libHSinteger-gmp-1.0.2.0-ghc8.8.4.so => /usr/lib/ghc/integer-gmp-1.0.2.0/libHSinteger-gmp-1.0.2.0-ghc8.8.4.so (0x00007f8316c00000)
    libHSghc-prim-0.5.3-ghc8.8.4.so => /usr/lib/ghc/ghc-prim-0.5.3/libHSghc-prim-0.5.3-ghc8.8.4.so (0x00007f8316760000)
    libgmp.so.10 => /usr/lib/x86_64-linux-gnu/libgmp.so.10 (0x00007f83166d0000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f83164f0000)
    /lib64/ld-linux-x86-64.so.2 (0x00007f8317741000)
    libffi.so.7 => /usr/lib/x86_64-linux-gnu/libffi.so.7 (0x00007f83164e0000)
    libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f83164be000)
buhr@DESKTOP-585NUKU:~/src/haskell-from-python$ 

在我看来,在进行实验时,您以某种方式破坏了 Makefile,所以它实际上并不是 link 对抗 /usr/lib/ghc/libHSrts-ghc8.4.4.so