Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RcppArmadillo Compile Errors on OS X Mavericks

Tags:

xcode

r

rcpp

This is a follow-up to the question at Element-Wise Matrix Multiplication in Rcpp

I have been getting a number of different kinds of errors with RcppArmadillo since upgrading to Mavericks. I have Xcode 5.0.2 and Command Line Tools installed. Also, gfortran from Homebrew. But I keep encountering the error below --

> cppFunction("arma::mat schur(arma::mat& a, arma::mat& b) 
                       { return(a % b); }", depends="RcppArmadillo")


ld: library not found for -lgfortran
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [sourceCpp_18474.so] Error 1
clang++  -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG  -I/usr/local/include  -    I"/Library/Frameworks/R.framework/Versions/3.0/Resources/library/Rcpp/include" -I"/Library/Frameworks/R.framework/Versions/3.0/Resources/library/RcppArmadillo/include"    -fPIC  "-mtune=native -g -O2 -Wall -pedantic -Wconversion" -c fileaf992bfb8f84.cpp -o fileaf992bfb8f84.o clang++ -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/usr/local/lib -L/usr/local/lib -o sourceCpp_18474.so fileaf992bfb8f84.o -L/Library/Frameworks/R.framework/Resources/lib -lRlapack -L/Library/Frameworks/R.framework/Resources/lib -lRblas -lgfortran /Library/Frameworks/R.framework/Versions/3.0/Resources/library/Rcpp/lib/libRcpp.a -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation 
Error in sourceCpp(code = code, env = env, rebuild = rebuild, showOutput = showOutput,  : 
Error 1 occurred building shared library.

WARNING: The tools required to build C++ code for R were not found.

Please install Command Line Tools for XCode (or equivalent).

# Contents of Makevars
$ cat ~/.R/Makevars 
CC=clang
CXX=clang++ 
CXXFLAGS="-mtune=native -g -O2 -Wall -pedantic -Wconversion"
FLIBS=-lgfortran

Commenting FLIBS=-lgfortran does not help and results in even more error messages --

> cppFunction("arma::mat schur(arma::mat& a, arma::mat& b) { return(a % b); }", depends="RcppArmadillo")
ld: warning: directory not found for option '-L/usr/local/lib/gcc/i686-apple-darwin8/4.2.3/x86_64'
ld: warning: directory not found for option '-L/usr/local/lib/x86_64'
ld: warning: directory not found for option '-L/usr/local/lib/gcc/i686-apple-darwin8/4.2.3'
ld: library not found for -lgfortran
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [sourceCpp_50381.so] Error 1

Thanks in advance.

Update

Following suggestions from response from Kevin and Dirk below, I re-installed Rcpp, RcppArmadillo and inline from source and updated FLIBS to point to the actual directory. This solved the issue.

# Update FLIBS in ~/.R/Makevars
FLIBS=-L/usr/local/Cellar/gfortran/4.8.2/gfortran
#Re-Install from source
install.packages(c("Rcpp","RcppArmadillo","inline"),type="source")
#Restart R
like image 756
xbsd Avatar asked Nov 12 '13 03:11

xbsd


2 Answers

EDIT: If you're a Homebrew user, you now instead need to use brew install gcc (gfortran is no longer provided separate of gcc), and you can then follow the instructions here to get set up.


You have to symlink the libraries to /usr/local/lib manually:

ln -s /usr/local/Cellar/gfortran/4.8.2/gfortran/lib/libgfortran.* /usr/local/lib/

I thought brew link gfortran would handle this, but apparently it only symlinks the gfortran program and not the actual libraries. So, unfortunately, you have to do it yourself.

(Replace 4.8.2 with whichever version of gfortran you're using from homebrew.)


Alternatively, if you want to keep from modifying /usr/local/lib, you can use

FLIBS=-L/usr/local/Cellar/gfortran/4.8.2/gfortran

in your ~/.R/Makevars file instead, so R knows where to find the gfortran libraries.

like image 187
Kevin Ushey Avatar answered Oct 09 '22 05:10

Kevin Ushey


I can only suggest you study the numerous threads on the r-sig-mac list, the different answers here on SO as well as the posts on the rcpp-devel list.

As your error comes from the failed Fortran linking, maybe also review the standard page by Simon U. as well as the tools page it points too. AFAIK you should use the (older) gfortran 4.2.* from that page with R -- but then I am not an OS X user.

Edit in late 2016: We now have more detailed instructions in section 2.16 of the Rcpp FAQ.

like image 25
Dirk Eddelbuettel Avatar answered Oct 09 '22 05:10

Dirk Eddelbuettel