Mac OS X R error "ld: warning: directory not found for option"

25,227

Solution 1

You need to modify the ~/.R/Makevars file. For a greater overview of this see: https://cran.r-project.org/doc/manuals/r-release/R-admin.html#OS-X-packages

Alternatively, this has been answered before in a bit more depth by @kevin-ushey in Rcpp warning: "directory not found for option '-L/usr/local/Cellar/gfortran/4.8.2/gfortran'".

What is happening is your code is not being run under gcc instead it is being forwarded to clang

You will need to change your compile statements in ~/.R/Makevars/ to gcc using:

VER=-5.3.0 
CC=gcc$(VER)
CXX=g++$(VER)
CFLAGS=-mtune=native -g -O2 -Wall -pedantic -Wconversion
CXXFLAGS=-mtune=native -g -O2 -Wall -pedantic -Wconversion
FLIBS=-L/usr/local/Cellar/gcc/5.3.0/lib/gcc/5

This assumes you have already installed gcc via homebrew under:

brew install gcc

(gfortran ships with gcc in brew now)

Solution 2

Incorporating previous solutions with additional help from the comments, the following solution has worked for me on Mac OS X High Sierra.

Create/edit ~/.R/Makevars with the following contents:

VER=-8
CC=gcc$(VER)
CXX=g++$(VER)
CXX11=g++$(VER)
CXX14=g++$(VER)
CXX17=g++$(VER)
CFLAGS=-mtune=native -g -O2 -Wall -pedantic -Wconversion
CXXFLAGS=-mtune=native -g -O2 -Wall -pedantic -Wconversion
FLIBS=-L/usr/local/Cellar/gcc/8.2.0/lib/gcc/8

Note, I am using homebrew and have gcc version 8.2.0 installed.

Solution 3

From http://thecoatlessprofessor.com/programming/rcpp-rcpparmadillo-and-os-x-mavericks-lgfortran-and-lquadmath-error/ you can fix this by downloading the optional gfortran libraries from http://r.research.att.com/libs/ and extracting them. To do this on the command line

curl -O http://r.research.att.com/libs/gfortran-4.8.2-darwin13.tar.bz2
sudo tar fvxz gfortran-4.8.2-darwin13.tar.bz2 -C /

Solution 4

In my case I combined this answer with this one, to produce the following code in the ~./R/.Makevars-file.

touch ~./R/.Makevars

(because it didn't exist there)

open -a BBEdit ~./R/.Makevars

(I use BBEdit as text editor)

Added the following lines to the Makevars-file:

VER=-11
CC=gcc$(VER)
CXX=g++$(VER)
CFLAGS=-mtune=native -g -O2 -Wall -pedantic -Wconversion
CXXFLAGS=-mtune=native -g -O2 -Wall -pedantic -Wconversion
# FLIBS=-L/usr/local/Cellar/gcc/11.1.0_1/lib/gcc/11
# FLIBS = -L`gfortran -print-file-name=libgfortran.dylib | xargs dirname`
FLIBS=`gfortran -print-search-dirs | grep ^libraries: | sed 's|libraries: =||' | sed 's|:| -L|g' | sed 's|^|-L|'`

These two lines were suggested by @KevinUshy.

# FLIBS=-L/usr/local/Cellar/gcc/11.1.0_1/lib/gcc/11
# FLIBS = -L`gfortran -print-file-name=libgfortran.dylib | xargs dirname`

I commented these out, as I figure that the final line would probably work best.

Two notes.

I have brew installed and I used brew to install gcc using the command brew install gcc.

I figure out the version-naming using brew info gcc, which gave me 11.1.0_1, but it is linked as gcc-11, so VER=-11 in the .Makevars-file.

Hope this helps others.

Solution 5

I'm working on MacOS Mojave (10.14.5) and on R 4.0.0. The problem here is that "CRAN R 4.0.0 builds and higher no longer use any custom compilers" (see here), so the Makevars solution does not appear to work anymore.

The solution for me was to download and install the GNU Fortran compiler from the official R-Project website. Note that you will also need Xcode and Xcode command-line tools.

After running the installer with default settings, compilation of gfortran code worked without problems.

Share:
25,227
burger
Author by

burger

Updated on July 29, 2022

Comments

  • burger
    burger almost 2 years

    I am trying to install an R package from source, but getting an error:

    * installing *source* package ‘mclust’ ...
    ** package ‘mclust’ successfully unpacked and MD5 sums checked
    ** libs
    gfortran-4.8   -fPIC  -g -O2  -c mclust.f -o mclust.o
    gfortran-4.8   -fPIC  -g -O2  -c mclustaddson.f -o mclustaddson.o
    clang -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/Library/Frameworks/R.framework/Resources/lib -L/usr/local/lib -o mclust.so mclust.o mclustaddson.o -L/Library/Frameworks/R.framework/Resources/lib -lRlapack -L/Library/Frameworks/R.framework/Resources/lib -lRblas -L/usr/local/lib/gcc/x86_64-apple-darwin13.0.0/4.8.2 -lgfortran -lquadmath -lm -L/usr/local/lib/gcc/x86_64-apple-darwin13.0.0/4.8.2 -lgfortran -lquadmath -lm -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation
    ld: warning: directory not found for option '-L/usr/local/lib/gcc/x86_64-apple-darwin13.0.0/4.8.2'
    ld: warning: directory not found for option '-L/usr/local/lib/gcc/x86_64-apple-darwin13.0.0/4.8.2'
    ld: library not found for -lquadmath
    clang: error: linker command failed with exit code 1 (use -v to see invocation)
    make: *** [mclust.so] Error 1
    ERROR: compilation failed for package ‘mclust’
    * removing ‘/Library/Frameworks/R.framework/Versions/3.3/Resources/library/mclust’
    Warning in install.packages :
      installation of package ‘mclust’ had non-zero exit status
    

    I don't have /usr/local/lib/gcc/x86_64-apple-darwin13.0.0, so it makes sense that it can't be found. I have /usr/local/lib/gcc/i686-apple-darwin11 and /usr/local/lib/gcc/4.8 (symlink to Homebrew installation). Where is it getting x86_64-apple-darwin13.0.0 from?

    There are a lot of references to a similar error online. However, all of them are related to compiling in Xcode and resolved by updating project settings, which is not applicable here.