Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error in R with Rcpp and RcppArmadillo using sourceCpp()

Tags:

r

rcpp

armadillo

I'm pretty new to C++, but have spent a lot of time with R. I'm trying to use RcppArmadillo, where the .cpp file is sourced in using the sourceCpp function. My example code is from

http://lists.r-forge.r-project.org/pipermail/rcpp-devel/2013-June/006150.html

and displayed below:

#include <RcppArmadillo.h>
using namespace Rcpp ;
// [[Rcpp::depends(RcppArmadillo)]]

// [[Rcpp::export]]
arma::colvec rowSumsRcppArmadillo(NumericMatrix x){
  arma::mat X = arma::mat(x.begin(), x.nrow(), x.ncol(), false);
  return arma::sum(X, 1);
}

I have the Rcpp and RcppArmadillo packages installed, and have successfully used Rcpp (without RcppArmadillo) to integrate C++ functions. However, for RcppArmadillo, I am getting the following error:

> sourceCpp("rowSums.cpp")
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_76327.so] Error 1

...

Error in sourceCpp("rowSums.cpp") : 
Error 1 occurred building shared library.

Any ideas? Thanks.

like image 642
Scott Avatar asked Dec 02 '25 04:12

Scott


2 Answers

See the error: ld: library not found for -lgfortran

You need to install the Fortran libraries as RcppArmadillo has this in src/Makevars:

PKG_LIBS=`$(R_HOME)/bin/Rscript -e "Rcpp:::LdFlags()"` \
          $(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS)

You get Lapack and Blas via R, but need the Fortran libs. See the OS X documentation for R about where to get this; I believe the place is Simon's page at AT&T but I am not an OS X user myself. If in doubt, ask on r-sig-mac where there are also numerous discussion threads concerning the various breakages which occurred with upgrades to OS X 10.9.

like image 136
Dirk Eddelbuettel Avatar answered Dec 03 '25 23:12

Dirk Eddelbuettel


I have encountered the same problem with you. I just go to http://cran.r-project.org/bin/macosx/tools/ and install gfortran-4.2.3.pkg on my computer and then use sourceCpp, it works then. You can try this, which is the simplest way I have found right now.

The more current page to consult is at https://mac.r-project.org/tools/

like image 27
Crystal Avatar answered Dec 03 '25 23:12

Crystal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!