macosrcppgfortranrcpparmadillomacos-sonoma

RcppArmadillo : 'gfortran' not found , MAC M3, Sonoma 14.5


I was trying to brush up my RCpp skills and attempted to use sourceCpp() to use a simple hello_world.cpp program.

However, I encounter

ld: warning: search path '/opt/gfortran/lib/gcc/aarch64-apple-darwin20.0/12.2.0' not found
ld: library 'gfortran' not found

But there is an installed fortran:

GNU Fortran (GCC) 13.2.0
Copyright (C) 2023 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE

Recent Update 21/03/2025

I reinstalled R and Studio, through home-brew. Rcpp package seems to be working. But when I try RcppArmadillo. It is showing the fortran not found.

Final Update 22/03/2025

Got the location of fortran by "system("which gfortran")" and

edited FC and FLIBS to that location in the file opened by "file.edit(file.path(R.home("etc"), "Makeconf"))"


Solution

  • Poking enviornment variables (I guess not so good)

    Since your address starts with /opt/gfortran/... - I have all my fortran addresses starting with /opt/homebrew/....

    Sys.getenv():

    F77                     /opt/homebrew/Caskroom/miniconda/base/envs/R441/bin/arm64-apple-darwin20.0.0-gfortran
    F90                     /opt/homebrew/Caskroom/miniconda/base/envs/R441/bin/arm64-apple-darwin20.0.0-gfortran
    F95                     /opt/homebrew/Caskroom/miniconda/base/envs/R441/bin/arm64-apple-darwin20.0.0-gfortran
    FC                      /opt/homebrew/Caskroom/miniconda/base/envs/R441/bin/arm64-apple-darwin20.0.0-gfortran
    FC_FOR_BUILD            /opt/homebrew/Caskroom/miniconda/base/envs/R441/bin/arm64-apple-darwin20.0.0-gfortran
    FFLAGS                  -march=armv8.3-a -ftree-vectorize -fPIC
                            -fno-stack-protector -O2 -pipe -isystem
                            /opt/homebrew/Caskroom/miniconda/base/envs/R441/include
    FORTRANFLAGS            -march=armv8.3-a -ftree-vectorize -fPIC
                            -fno-stack-protector -O2 -pipe -isystem
                            /opt/homebrew/Caskroom/miniconda/base/envs/R441/include
    GFORTRAN                /opt/homebrew/Caskroom/miniconda/base/envs/R441/bin/arm64-apple-darwin20.0.0-gfortran
    

    I had installed my R in this case into a conda environment - so the gfortran is in the environment binary folder. And the library flags are to the conda environment's include folder.

    Theoretically one could the environment variables in R like this: Sys.setenv(GFORTRAN = "/path/from/your/which gfortran command", FFLAGS = "path to your include for fortran") etc.

    But I think the problem is better solved by a proper installation.

    Actually, if you install R, fortran is installed.

    probably a .libPaths() problem?

    Probably your R looks for other additional library folders than it should? Check .libPaths(). If it lists folders which should not be looked into, you can exclude folders by building a vector containing only the folders which you want. E.g. let's say it lists 3 folders and you want only 1 and 2:

    .libPaths(.libPaths()[c(1, 2)], include.site=FALSE)
    

    the include.site=FALSE makes that only those chosen paths stay in .libPaths().

    This should be done in a fresh session right at the beginning of the session.

    If you now load libraries, R looks only into those folders for the libraries you load.

    Sometimes dotfiles in your environment like .Rprofile define library paths. And you can put this code into your local environment's .Rprofile file which gets run everytime you start a session. Check also your .Renviron file which defines environment variables.