创建链接到 Rust dylib 的共享 C 对象以在 R 中使用

Create shared C object linked to Rust dylib for use in R

我正在尝试创建一个可以加载到 R 中的共享对象,它通过 R 的 C API 调用 Rust 函数。要从 C 调用 Rust,我遵循这个 blog post。当我尝试为 Rust 库创建共享库和 link 时,我的问题就出现了。 linker 抱怨找不到我的 Rust 函数。我对编译语言很陌生,在转向 SO 之前已经付出了几天的努力。在那段时间里,我学到了很多关于编译器标志的知识,但并没有接近解决方案。我认为这可能是显而易见的。

我的 C++ 代码:

#include "Rinternals.h"
#include "R.h"
#include "treble.h"

// test.cpp
extern "C" {

SEXP triple(SEXP val) {

    int32_t ival = *INTEGER(val);
    Rprintf("9 tripled is %d\n", treble(ival));
    return R_NilValue;
}

}

treble.h:

#include <stdint.h>

int32_t treble(int32_t value);

我的 Rust 代码:

#![crate_type = "dylib"]

#[no_mangle]
pub extern fn treble(value: i32) -> i32 {
  value * 3
}

这就是我在命令行上所做的:

$ rustc glue.rs
$ g++ -shared test.cpp -o test.so -I/Library/Frameworks/R.framework/Headers -L/Library/Frameworks/R.framework/Libraries -L. -lR -lglue
Undefined symbols for architecture x86_64:
  "treble(int)", referenced from:
      _triple in test-dac64b.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

检查 rust 创建的目标文件:

$ nm -gU libglue.dylib
...
0000000000001750 T _treble

在 C++ 代码中,您需要将 Rust 函数(可通过 C ABI 获得)声明为 extern "C"

treble.h

#include <stdint.h>

extern "C" {
  int32_t treble(int32_t value);
}

您遇到的错误是因为 C++ 编译器在尝试 link 之前 名称修改 方法 trebleextern "C" 禁用重整。

此外,您的 Rust FFI 代码应始终使用 libc crate 中的类型;在你的情况下你想要 libc::int32_t.