1

I have installed the ArrayFire library using the binary installer for MacOS which puts the libraries into /usr/local/lib. I can compile and run simple examples, e.g.

#include <arrayfire.h>
#include <stdio.h>

int main() {
  unsigned int count;
  af_get_backend_count(&count);
  printf("backends: %d\n", count);
  return 0;
}

gives

$ /usr/local/clang4/bin/clang -laf -o minimal minimal.c
$ ./minimal
backends: 2

Now I want to do the equivalent thing in R. I have

#include <arrayfire.h>
#include <Rinternals.h>
SEXP count_backends() {
  unsigned int count;
  af_get_backend_count(&count);
  Rprintf("backends: %d\n", count);
  return R_NilValue;
}

which I compile with

$ PKG_LIBS=-laf R CMD SHLIB minimal.c
/usr/local/clang4/bin/clang -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG   -I/usr/local/include   -fPIC  -Wall -g -O2  -c minimal.c -o minimal.o
/usr/local/clang4/bin/clang -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/Library/Frameworks/R.framework/Resources/lib -L/usr/local/clang4/lib -o minimal.so minimal.o -laf -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation

However, I cannot load the resulting library. Error message:

> dyn.load("minimal.so")
Error in dyn.load("minimal.so") : 
  unable to load shared object '/Users/ralf/Documents/af-simple/minimalR/minimal.so':
  dlopen(/Users/ralf/Documents/af-simple/minimalR/minimal.so, 6): Library not loaded: @rpath/libaf.3.dylib
  Referenced from: /Users/ralf/Documents/af-simple/minimalR/minimal.so
  Reason: image not found

Why is is libaf not found while there are no such problems when building a binary? What can I do to have the library loaded?

Notes:

  • I have found one workaround by explicitly adding -rpath /usr/local/lib to PKG_LIBS. If this is the only solution I'll have a follow-up question with the problems resulting from that.
  • I am using "R version 3.4.3 (2017-11-30)" with the suggested "clang version 4.0.0" on MacOS High Sierra.
  • Setting LD_LIBRARY_PATH or DYLD_LIBRARY_PATH to /usr/local/lib does not help.
  • I have no such problems on Linux.
Ralf Stubner
  • 26,263
  • 3
  • 40
  • 75
  • Have you had any issues with setting `-rpath /usr/local/lib` as the solution, and / or do you have the follow-up question you mentioned? (I'm facing a very similar issue to yours) – SymbolixAU Mar 12 '18 at 02:49
  • @SymbolixAU The issue with `rpath` is probably ArrayFire specific, since `libaf` tries to load one of the backend libs (`libafcuda`, `libafopencl` or `libafcpu`) which fails when loaded from `R`. My [current workaround](https://github.com/RInstitute/rcpparrayfire/tree/macos) is to use `libafopencl` directly on MacOS. But that makes Test fail on Travis CI ... What issue do you have? – Ralf Stubner Mar 12 '18 at 11:35
  • I'm working on a package (currently private on github) that uses the new [MongoDB C++ drivers](http://mongodb.github.io/mongo-cxx-driver/mongocxx-v3/installation/), but R on Mac doesn't find the `lib*` files, and so like you I've had to specify `-rpath /usr/local/lib`. – SymbolixAU Mar 12 '18 at 22:28
  • @SymbolixAU Do you compile the library yourself? In that case it might also be possible to alter the install name of the library to include the full path, c.f. https://stackoverflow.com/questions/49154506/understanding-makevars-for-linking-to-external-c-library-in-r-package?noredirect=1&lq=1 – Ralf Stubner Mar 13 '18 at 14:42

0 Answers0