[R-SIG-Mac] Problems with runtime linking of R-extension

Ralf Stubner ralf.stubner at r-institute.com
Fri Feb 23 10:16:02 CET 2018

Hi all,

I am trying to port RcppArrayFire
(https://github.com/RInstitute/rcpparrayfire) to Mac OS, but I am having
some troubles with resolving linked libraries. I have tried to reduce
the issue as much as possible, i.e. no Rcpp and no package structure. It
seems to be MacOS specific since the same code works on Linux (as does
RcppArrayFire). I also asked this on stackoverflow
but that did not draw a lot of attention. Maybe someone on this list can
shed some light on this:

When I install ArrayFire (http://arrayfire.com/) on MacOS using the
binary installer, the libraries are installed in /usr/local/lib with an
install name starting with @rpath:

$ otool -L /usr/local/lib/libaf.dylib /usr/local/lib/libaf.dylib:
	@rpath/libaf.3.dylib (compatibility version 3.0.0, current version 3.5.1)
	/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current
version 1226.10.1)

 I can compile, link and run simple examples, e.g.

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

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


$ /usr/local/clang4/bin/clang -laf -o minimal minimal.c
$ otool -L minimal
	@rpath/libaf.3.dylib (compatibility version 3.0.0, current version 3.5.1)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current
version 1252.0.0)
$ ./minimal
backends: 2

To do the same from R I use

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

And compile it 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
$ otool -L minimal.so  minimal.so:
	minimal.so (compatibility version 0.0.0, current version 0.0.0)
	@rpath/libaf.3.dylib (compatibility version 3.0.0, current version 3.5.1)
(compatibility version 3.4.0, current version 3.4.3)
	/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 150.0.0, current version 1451.0.0)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current
version 1252.0.0)

If I then try to load the resulting library, I get this error message:

R> dyn.load("minimal.so")
Error in dyn.load("minimal.so") :   unable to load shared object
  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
Execution halted

Both the binary and the R-extension library refer to the external
library using the same install name, but for the R-extension this
reference cannot be resolved. Why is this the case?


PS: Used versions:
* Mac OS High Siearra
* R 3.4.3
* clang 4.0.0 from https://cran.r-project.org/bin/macosx/tools/

Ralf Stubner
Senior Software Engineer / Trainer

R Institute GmbH
Dortustraße 48
14467 Potsdam

T: +49 331 23 70 81 66
F: +49 331 23 70 81 67
M: +49 162 20 91 196

Mail: ralf.stubner at r-institute.com

Sitz: Potsdam
Register: AG Potsdam HRB 27966 P
Ust.-IdNr.: DE300072622
Geschäftsführer: Prof. Dr. Dr. Karl-Kuno Kunze

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 4008 bytes
Desc: S/MIME Cryptographic Signature
URL: <https://stat.ethz.ch/pipermail/r-sig-mac/attachments/20180223/d2283ebe/attachment.p7s>

More information about the R-SIG-Mac mailing list