2
ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-linux]
rustc 0.13.0-nightly (f168c12c5 2014-10-25 20:57:10 +0000)

我想将ffigem 与rust.

我已经阅读了这篇(相当过时的)博客文章,其中展示了如何做到这一点。

问题是:它不起作用。

这是我的代码:

测试.rs:

fn test(bla: i32) -> i32 { bla*bla }

#[no_mangle]
extern fn _test_wrapper(i: i32) -> i32 {
  test(i)
}

测试.rb:

require 'ffi'

module Test
  extend FFI::Library
  ffi_lib File.absolute_path 'libtest.so'

  attach_function :_test_wrapper, [:int32], :int32
end

我像这样编译 test.rs:

rustc --crate-type dylib test.rs

进而

ruby test.rb

输出:

/home/me/.rvm/gems/ruby-2.1.2/gems/ffi-1.9.6/lib/ffi/library.rb:261:in `attach_function': Function '_test_wrapper' not found in [/home/me/Dokumente/ruby/rust_require/specs/test/libtest.so] (FFI::NotFoundError)
    from test.rb:7:in `<module:Test>'
    from test.rb:3:in `<main>'

我做错了什么?(我已经尝试过pub extern fn......,也不起作用。)

4

1 回答 1

2

您已经很接近了,您只需要修复在编译 Rust 代码并将函数公开时收到的警告:

#[no_mangle]
pub extern fn _test_wrapper(i: i32) -> i32 {
  test(i)
}

为了帮助我调试问题,我曾经nm查看编译后的库导出了哪些符号。我在 OS X 上,所以你可能需要调整参数和文件名:

$ nm -g libtest.dylib
0000000000000e30 T __test_wrapper
0000000000001020 S _rust_metadata_test_04c178c971a6f904
                 U _rust_stack_exhausted
                 U dyld_stub_binder

在将该功能标记为公开之前,它没有出现在此列表中。

于 2014-11-02T21:29:23.233 回答