Runtime linking R-extension on MacOS

吃可爱长大的小学妹 提交于 2019-12-23 01:54:41

问题


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.

来源:https://stackoverflow.com/questions/48705490/runtime-linking-r-extension-on-macos

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!