如何使用 Guile Scheme 加载国外库?

问题描述 投票:0回答:1

我想使用我用 C 编写并使用

gcc -shared -o libsum.so sum.c
编译成库的函数,但我不知道如何加载库并从 Scheme 解释器调用函数。

我成功编译了共享对象:

file libsum.so
libsum.so: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, BuildID[sha1]=11ba1cd070663d432026c6494d88d0b5839b58dd, not stripped

来自C源:

int sum(int a, int b) {
  return a + b;
}

我可以看到使用

objdump
我的共享对象文件包含我想使用的功能:

00000000000010e9 <sum>:
    10e9:       55                      push   %rbp
    10ea:       48 89 e5                mov    %rsp,%rbp
    10ed:       89 7d fc                mov    %edi,-0x4(%rbp)
    10f0:       89 75 f8                mov    %esi,-0x8(%rbp)
    10f3:       8b 55 fc                mov    -0x4(%rbp),%edx
    10f6:       8b 45 f8                mov    -0x8(%rbp),%eax
    10f9:       01 d0                   add    %edx,%eax
    10fb:       5d                      pop    %rbp
    10fc:       c3                      ret

但是当我尝试加载文件时,它说文件不存在:

guile
GNU Guile 3.0.9
Copyright (C) 1995-2023 Free Software Foundation, Inc.

Guile comes with ABSOLUTELY NO WARRANTY; for details type `,show w'.
This program is free software, and you are welcome to redistribute it
under certain conditions; type `,show c' for details.

Enter `,help' for help.
scheme@(guile-user)> (use-modules (system foreign)
                                  (system foreign-library))
scheme@(guile-user)> (load-extension "libsum" "sum_init")
ice-9/boot-9.scm:1685:16: In procedure raise-exception:
In procedure dlopen: file "libsum.so", message "libsum.so: cannot open shared object file: No such file or directory"

Entering a new prompt.  Type `,bt' for a backtrace or `,q' to continue.
scheme@(guile-user) [1]>

文件“libsum.so”在当前工作目录中,所以我不知道会发生什么。

任何帮助将不胜感激!

c scheme ffi guile
1个回答
1
投票

load-extension
使用
load-foreign-library
加载共享库,然后在目录列表中查找普通名称:

除非 library 表示绝对文件名或以其他方式包含目录分隔符(

/
,在 Windows 上也是
\
),Guile 将在 search-paths 中列出的目录中搜索库。

您当前的工作目录可能不在该搜索路径中。尝试按照文档的建议添加目录分隔符以显式使用 CWD:

(load-extension "./libsum" "sum_init")
© www.soinside.com 2019 - 2024. All rights reserved.