Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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/

In case there are other folks like me having similar trouble with 'build' and 'check' with Armadillo projects after you've install one of the recommended fortran libraries for Mac, you need to follow [the instructions here]1: https://thecoatlessprofessor.com/programming/cpp/r-compiler-tools-for-rcpp-on-macos/

I was trying to change the LDFLAGS environment variable in the shell, but it wasn't passing through. However, setting it correctly in ~/.R/Makevars seemed to work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top