简体   繁体   English

在R中加载链接到Rust库的共享库

[英]Load a shared library linked to Rust library in R

Following up on this question here , I am having issues using dyn.load to load a shared library that is linked to a Rust dylib. 这里跟进这个问题,我在使用dyn.load加载链接到Rust dylib的共享库时遇到问题。 I suspect it has something to do with where R is looking for the Rust dylib, but I have not found a way to specify another location than whatever the default is. 我怀疑它与R在哪里寻找Rust dylib有关,但我还没有找到一种方法来指定另一个位置,而不是默认值。

From R, I execute the following: 从R开始,我执行以下操作:

> dyn.load('src/test.so')

And receive this error message: 并收到此错误消息:

Error in dyn.load("src/test.so") : 
  unable to load shared object '/Users/Zelazny7/r-dev/rustr/src/test.so':
  dlopen(/Users/Zelazny7/r-dev/rustr/src/test.so, 6): Library not loaded: libglue.dylib
  Referenced from: /Users/Zelazny7/r-dev/rustr/src/test.so
  Reason: image not found

How do I load a shared library that depends on another shared library? 如何加载依赖于另一个共享库的共享库?

The documentation for dyn.load does not specify how to do this. dyn.load文档未指定如何执行此操作。

Update: 更新:

Thanks to shepmaster I was able to successfully build and import a shared library in R. The shared library was compiled in C and is itself linked to a Rust library. 感谢shepmaster,我能够在R中成功构建和导入共享库。共享库是用C编译的,并且本身链接到Rust库。 These were my steps: 这些是我的步骤:

  1. Compile shared Rust library 编译共享的Rust库
  2. Compile shared C library and link to the Rust library using the following command (in Windows as I'm at work this morning) 使用以下命令编译共享C库并链接到Rust库(在Windows中,因为我今天早上在工作)

My directory contents: 我的目录内容:

C:\Users\gravesee\test>ls
rglue.dll  rglue.rs  rustr.c  treble.h

Compiling the final shared library: 编译最终的共享库:

gcc -shared -m64 -I"C:\Program Files\R\R-3.2.0\include" rustr.c -L"C:\Program Files\R\R-3.2.0\bin\x64" -lR -L. -lrglue -o test.dll

Loading the library in R: 在R中加载库:

> dyn.load("test.dll")
> is.loaded("triple")
[1] TRUE
> .Call("triple", as.integer(32))
The triple is 96

The problem is going to boil down to the fact that your shared libraries are not in the directories that your system expects them to be by default. 问题将归结为这样一个事实:您的共享库不在您的系统默认的目录中。

There are a few tricks that you can use, 2 of which I was able to make work: 你可以使用一些技巧,其中2个我能够工作:

  1. Run R from the same directory you compiled the libraries. 从编译库的同一目录运行R.

  2. Set the LD_LIBRARY_PATH (Linux) or DYLD_LIBRARY_PATH (OS X) before launching R. 在启动R之前设置LD_LIBRARY_PATH (Linux)或DYLD_LIBRARY_PATH (OS X)。

     DYLD_LIBRARY_PATH=/path/to/rust/lib/ R 
  3. You should be able to set the rpath when building the shared library. 应该能够在构建共享库时设置rpath。

     g++ -shared treble.cxx -o treble.so -L/tmp/ -Wl,-rpath,/tmp -lglue 

    However, I wasn't able to get this to work nicely on OS X, so I'm not sure what I'm doing wrong. 但是,我无法在OS X上很好地工作,所以我不确定我做错了什么。

  4. (OS X only) After building your C++ library, you can change the install name that refers to the original Rust library and include the absolute path: (仅限OS X)构建C ++库后,可以更改引用原始Rust库的安装名称并包含绝对路径:

     install_name_tool -change libglue.dylib /tmp/libglue.dylib treble.so 

Basically, you will want to look up how to have shared libraries that depend on other shared libraries when multiple of those libraries do not exist in your default linker search paths. 基本上,当您的默认链接器搜索路径中不存在多个库时,您将需要查找如何使用依赖于其他共享库的共享库。

Sources 来源

Linking a dynamic library (libjvm.dylib) in Mac OS X (rpath issue) 在Mac OS X中链接动态库(libjvm.dylib)(rpath问题)

Print rpath of executable on OSX 在OSX上打印可执行文件的rpath

@executable_path, @load_path and @rpath @executable_path,@ load_path和@rpath

How do I modify the install name of a .dylib at build time 如何在构建时修改.dylib的安装名称

clang, change dependent shared library install name at link time clang,在链接时更改依赖的共享库安装名称

尽管最初的回答是不错的,我提出了另一种方法为这个在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM