我想从 GnuCOBOL 调用 Rust。我已经从 Can GnuCOBOL interface with Rust? 中的第一个示例中复制了代码来自 Brian Tiffin 的 GNUCobol FAQ 作为测试,但在运行时出现错误。
预期:
$ make -B
rustc --crate-type=dylib called.rs
LD_RUN_PATH=. cobc -xj caller.cob -L. -lcalled
:Hello, world:
实际:
$ make -B
rustc --crate-type=dylib called.rs
LD_RUN_PATH=. cobc -xj caller.cob -L. -lcalled
libcob: error: module 'hello_rust' not found
make: *** [makefile:5: caller] Error 1
从命令行编译这两个文件后,我得到了同样的错误,然后使用 $ ./caller
。
根据 cobc
手册页和 GnuCOBOL manual 的链接部分,语法似乎是正确的和 Rust reference .我已经按照 GnuCOBOL 手册中的说明尝试了 $ ./caller COB_LIBRARY_PATH=.
,但没有任何区别。 Rust 源代码按预期编译为库,但 COBOL 找不到它。
使用 $ cobcrun caller
显示 libcob: error: module 'caller' not found
。
This关于类似错误的问题是关于静态链接多个 COBOL 源文件,它工作正常,并且 this关于类似错误的问题似乎是 X"AF"
的问题,此处未使用。将 C 源代码与 Jay Moseley 的 C Wrapper for Calling Library Function 静态链接示例按预期工作。不支持静态链接 Rust 源。
软件版本:
- Ubuntu 22.04.1 LTS
- cobc (GnuCOBOL) 3.1.2.0
- rustc 1.64.0
最佳答案
问题似乎是 COBOL caller
可执行文件正在尝试动态加载名为 hello_rust.so
而不是 libcalled.so
的库在运行时。
不修改任何东西的简单解决方法是只创建一个符号链接(symbolic link):
$ ln -s libcalled.so hello_rust.so
或者,将
-fstatic
添加到cobc
命令应该在编译时静态链接 Rust 库,消除动态库运行时调用。示例
Makefile
可以更新为如下所示:# GnuCOBOL and Rust .RECIPEPREFIX = > caller: caller.cob libcalled.so > LD_RUN_PATH=. cobc -fstatic -xj caller.cob -L. -lcalled libcalled.so: called.rs > rustc --crate-type=dylib called.rs
作为引用,我们可以通过 strace 调查可执行文件正在做什么,以查看 COBOL 运行时正在进行哪些系统调用——在我们的例子中——它找不到哪些文件。
$ strace ./caller 2>&1 | grep hello_rust
access("./hello_rust.so", R_OK) = -1 ENOENT (No such file or directory)
access("/usr/lib64/gnucobol/hello_rust.so", R_OK) = -1 ENOENT (No such file or directory)
write(2, "module 'hello_rust' not found", 29module 'hello_rust' not found) = 29
关于rust - GnuCOBOL 调用 Rust : libcob: error: module not found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74071691/