Monday, July 21, 2014

Fix the 'directory not found' error of Rcpp in Mac OSX

If you are on Max OSX Mavericks (10.9) and you happen to use R and Rcpp, you might notice that it doesn't work. Here is the error message when you try to sourceCpp something into R:

ld: warning: directory not found for option '-L/usr/local/lib/gcc/x86_64-apple-darwin13.0.0/4.8.2'
ld: library not found for -lgfortran
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [] Error 1
clang++ -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG  -I/usr/local/include -I/usr/local/include/freetype2 -I/opt/X11/include  -I"/Library/Frameworks/R.framework/Versions/3.1/Resources/library/Rcpp/include" -I"/Library/Frameworks/R.framework/Versions/3.1/Resources/library/RcppArmadillo/include" -I"/Users/kaiyin/personal_config_bin_files/workspace/rpackages/rbed/data" -I"/Users/kaiyin/personal_config_bin_files/workspace/rpackages/rbed/data/../inst/include"   -std=c++11 -fPIC  -Wall -mtune=core2 -g -O2  -c main.cpp -o main.o
clang++ -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/usr/local/lib -o main.o -L/Library/Frameworks/R.framework/Resources/lib -lRlapack -L/Library/Frameworks/R.framework/Resources/lib -lRblas -L/usr/local/lib/gcc/x86_64-apple-darwin13.0.0/4.8.2 -lgfortran -lquadmath -lm -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation
Error in sourceCpp("main.cpp") :
  Error 1 occurred building shared library.

The reason is that Rcpp is looking at a folder that used to exist but doesn't anymore, basically it's assuming you are using an older version of OSX. The fix is simple, open a terminal window and do the following:

cd /usr/local/lib/gcc/
ln -s x86_64-apple-darwin13.2.0  x86_64-apple-darwin13.0.0
cd x86_64-apple-darwin13.2.0
ln -s 4.8.3 4.8.2

Note this error only appears when you are using Rcpp inside Emacs + ESS, so some ESS configuration might be slightly out-dated.