问题
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
toPKG_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
orDYLD_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