Download the shared library associated with the Rust library in R

Following this question here , I am having problems using dyn.load to load a shared library related to Rust dylib. I suspect this has something to do with where R searches for Rul dylib, but I have not found a way to specify a different location than what is by default.

From R, I do the following:

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

And get 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 to load a shared library that depends on another shared library?

The documentation for dyn.load does not indicate how to do this.

Update:

Thanks to shepmaster, I was able to successfully create and import the shared library into R. The shared library was compiled in C and itself linked to the Rust library. These were my steps:

  • Compile a common Rust library.
  • Compile the common C library and the link to the Rust library with the following command (on Windows when I am this morning)

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 library in R:

 > dyn.load("test.dll") > is.loaded("triple") [1] TRUE > .Call("triple", as.integer(32)) The triple is 96 
+7
r shared-libraries rust
source share
2 answers

The problem is that your shared libraries are not in the directories that your system expects from them by default.

There are several tricks you can use, 2 of which I could do:

  • Run R from the same directory that you compiled the libraries.

  • Before starting R, set LD_LIBRARY_PATH (Linux) or DYLD_LIBRARY_PATH (OS X).

     DYLD_LIBRARY_PATH=/path/to/rust/lib/ R 
  • You should be able to set rpath when creating a shared library.

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

    However, I was not able to get this to work well on OS X, so I'm not sure what I'm doing wrong.

  • (OS X only) After creating your C ++ library, you can change the installation name, which refers to the original Rust library, and include the absolute path:

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

Basically, you'll want to see how to have shared libraries that depend on other shared libraries when several of these libraries do not exist in your default link lookup paths.

Sources

Dynamic Library Linking (libjvm.dylib) on Mac OS X (rpath issue)

Printing the path to the executable in OSX

@executable_path, @load_path and @rpath

How to change .dylib installation name during build

clang, change the dependent name of the installed library for the duration of the link

+4
source share

Although the original answer is good, I suggested an alternative approach for this here .

0
source share

All Articles