2

I've been trying many different ways to install RcppArmadillo, but I don't get it to work

install.packages(c('Rcpp'))
Sys.setenv("PKG_CXXFLAGS"="-std=c++11")
install.packages(c('RcppArmadillo'),type = "source")

It gives me this error:

ld: warning: directory not found for option '-L/usr/local/gfortran/lib/gcc/x86_64-apple-darwin18/8.2.0'
ld: library not found for -lquadmath
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [RcppArmadillo.so] Error 1
ERROR: compilation failed for package ‘RcppArmadillo’
* removing ‘/Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppArmadillo’

Obviously, I don't have x86_64-apple-darwin18/8.2.0 since I installed gfortran for the apple silicon architecture.

Mac version:

macOS Big Sur 
11.2.3
Apple M1

R version:

version
               _                           
platform       x86_64-apple-darwin17.0     
arch           x86_64                      
os             darwin17.0                  
system         x86_64, darwin17.0          
status                                     
major          4                           
minor          0.4                         
year           2021                        
month          02                          
day            15                          
svn rev        80002                       
language       R                           
version.string R version 4.0.4 (2021-02-15)
nickname       Lost Library Book           
M. Beausoleil
  • 3,141
  • 6
  • 29
  • 61
  • 2
    I have no answer for you but keep hearing that the M1 setup for R is still having trouble with Fortran---that is all I know. I recommend checking with the r-sig-mac mailing list. – Dirk Eddelbuettel Mar 26 '21 at 12:54
  • ok, I found that modifying the Makeconf could be a solution. https://stackoverflow.com/questions/23916219/os-x-package-installation-depends-on-gfortran-4-8 – M. Beausoleil Mar 26 '21 at 12:55
  • I don't have the warning when changing the FLIBS to `FLIBS = -L/usr/local/gfortran/lib/gcc/aarch64-apple-darwin20.2.0/11.0.0 -L/usr/local/gfortran/lib -lgfortran -lquadmath -lm`, BUT I still do get the error that the library `-lquadmath` is not found – M. Beausoleil Mar 26 '21 at 12:58
  • 1
    This might be of some interest: https://github.com/fxcoudert/gfortran-for-macOS/issues/12 – M. Beausoleil Mar 26 '21 at 13:05
  • 1
    I had no trouble installing RcppArmadillo on native M1. I used these install instructions: https://www.r-bloggers.com/2021/02/fully-native-m1-apple-silicon-r-setup/. Rather than troubleshooting, it might be faster to re-install from scratch. `gfortran` was in `/opt/R/arm64/bin` and I only needed to add that to the path, as the instructions say. – thc Mar 26 '21 at 18:35
  • AS it says "Just remember, that if you need RStudio (or anything that links against the x86_64 R dylib) you’re going to be reverting this to get stuff done." And indeed, it crashes RStudio... just keep in mind that when trying this out! – M. Beausoleil Mar 27 '21 at 19:33
  • Just to make sure, I'm able to install RcppArmadillo with `install.packages('RcppArmadillo')` but whenever I want to compile `Rcpp::sourceCpp('~/Desktop/helloworld.cpp')` from https://thecoatlessprofessor.com/programming/cpp/r-compiler-tools-for-rcpp-on-macos-before-r-3.6.0/ I get an error `"*** C++11 compiler required; enable C++11 mode in your compiler, or use an earlier version of Armadillo"`. But even with an earlier version, I'm not able to install the package so that it compiles – M. Beausoleil Mar 27 '21 at 19:59
  • e.g. `packageurl <- "https://cran.r-project.org/src/contrib/Archive/RcppArmadillo/RcppArmadillo_0.9.900.3.0.tar.gz" install.packages(c(packageurl), repos=NULL, type="source")` – M. Beausoleil Mar 27 '21 at 19:59
  • I think it worked: 1. get https://github.com/fxcoudert/gfortran-for-macOS/releases/tag/10.2-bigsur-intel (NOT THE ONE FOR M1!!), 2. `install.packages(c('Rcpp'))`, 3. `Sys.setenv("PKG_CXXFLAGS"="-std=c++11")`, 4. `install.packages(c('RcppArmadillo'),type = "source")`, 5. test the `hello world` found here https://thecoatlessprofessor.com/programming/cpp/r-compiler-tools-for-rcpp-on-macos-before-r-3.6.0/ (`Rcpp::sourceCpp('~/Desktop/helloworld.cpp')`). 6. enjoy! – M. Beausoleil Apr 09 '21 at 23:59

2 Answers2

4

I think I found an issue. First, I follow this tutorial : R COMPILER TOOLS FOR RCPP ON MACOS
Once completed, it gaves me almost the same error as you mentioned in your post :

ld: warning: directory not found for option '-L/opt/R/arm64/gfortran/.....'
ld: library not found for -lgfortran clang: error: linker command failed
with exit code 1 (use -v to see invocation)

It seems, R is looking for gfortran in /opt/R/arm64 folder.
In the turorial it indicates that gfortran is installed in the /usr/local/gfortran folder.
I created a symbolic link in the /opt/R/arm64 which refers to /usr/local/gfortran with the command
ln -s /usr/local/gfortran /opt/R/arm64

Dharman
  • 30,962
  • 25
  • 85
  • 135
nimliug
  • 349
  • 1
  • 11
3

One can set FLIBS in ~/.R/Makevars to one of the following options

# homebrew gfortran
FLIBS=-L/opt/homebrew/opt/gfortran/lib

# gfortran included in R
FLIBS=-L/opt/R/arm64/gfortran/lib

In addition one might want to also define F77 and FC as

F77     = /opt/R/arm64/gfortran/bin/gfortran
FC      = /opt/R/arm64/gfortran/bin/gfortran

To verify, try installing the glmnet package which should now succeed.

pat-s
  • 5,992
  • 1
  • 32
  • 60