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

13

29

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.

Diathesis answered 14/3, 2016 at 23:16 Comment(2)
Homebrew installations are not supported by "official R Core". There is a Fortran package at r.research.att.com but I think it might have static linking with the "authorized" version that ends up in the Frameworks tree. I'm not an expert in this area and there are some users of SO (thinking of @hrbrmstr) that think homebrew is "just fine". Do a search on "user:1457051 homebrew" and see if you find more support than I offer.Wigan
The current versions of clang and gfortran used by CRAN can be found here: cran.r-project.org/bin/macosx/toolsWigan
T
30

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)

Turnstile answered 15/3, 2016 at 2:6 Comment(4)
I have gcc installed via homebrew and I've created a Makevars file but I keep having issues, primarily because I have a newer version of gcc. I changed the last line to FLIBS=-L/usr/local/Cellar/gcc/7.3.0_1/lib/gcc/7 and then the first line to VER=-7.3.0_1 (which caused problems until I changed it to VER=-7). That might've actually worked for compiling one R package but now for some reason the default compiler remains as clang, I'm not sure why... Have any ideas??Zelazny
Need to set the implicit variables CXX11, CXX14, and CXX17 to use g++$(VER) just like CXX does. These define the compiler that should be used for each language standard. e.g. CXX11 is the C++11 standard.Turnstile
Thank you very much for your response--I really do appreciate it. Now it appears that I'm able to compile some packages with no problem and then when other packages fail to compile I end up just commenting out the Makevars file entirely so that my system uses clang instead, which seems to mostly fix the issue. Is that sort of ad hoc approach normal??Zelazny
To get this working today with GCC 10.2.0 I had to change the second line to CC=gcc.Endomorph
D
19

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.

Dawna answered 29/11, 2018 at 21:5 Comment(3)
The FLIBS lead worked in a similar case when the compiler couldn't find libs for gfortranVessel
Just for anyone else lading here with the same issue, this approach also worked for me on MacOS Big SurHeroin
Alas, for me with Big Sur on M1, it does not. As with comment on other answer, first changed second line to CC=gcc and used /opt/homebrew. But throws "checking whether g++-10 accepts -g... no configure: error: no C++ compiler found or it cannot create executables"Koger
R
12

I had this issue for a long time, working on a Mac. Following the other answers on this page and other questions, I did the following and it finally worked!

Step 1 - brew install gcc (it installed gcc-11 for me. If this is different for you in the future, replace gcc-11 below with the corresponding version. You can find the version by checking in the folder /usr/local/bin and seeing the latest gcc version file there)

Step 2 - xcode-select --install (this installs Xcode CLI)

Step 3 - ln -sf /usr/local/bin/gcc-11 /usr/local/bin/gcc (this makes a new gcc symlink under /usr/local/bin/. Source)

Step 4 - I did not have a ~/.R/Makevars file. Heck, I didn't even have the ~./R folder. Made the folder and the file. Here is what I wrote in the Makevars file (remember to change the GCC version in the first and last line according to the version you have. Also remember to not have the entire version number that's in the last line, in the first line. Just the overall version number - 11 in my case) -

VER=-11
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/11.2.0_3/lib/gcc/11

Now restart, and voila! It works.

Randi answered 8/12, 2021 at 5:59 Comment(4)
Fixed my gfortran issues just now.Burnout
This worked for me (Sep 19, 2023) on Mac OS Ventura 13.5.2. R version 4.2.3. Make sure to change the verison number VER and the FLIBS path to your latest version. Thanks, @user1993!Noncompliance
Since most of these answers are old, how do I determine what the version value should be?Thorvaldsen
@Thorvaldsen I mention that in step 1 - check binRandi
C
9

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 xjvf gfortran-4.8.2-darwin13.tar.bz2 -C /
Centric answered 5/10, 2016 at 21:30 Comment(2)
when I enter second line, I get tar: Unrecognized archive format. What am I doing wrong?Prepuce
@Prepuce The correct tar flags are xjvf for bzip2 files. I have an edit in the review queue to fix this.Turbo
D
6

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.

Daugava answered 3/8, 2020 at 8:32 Comment(0)
S
6

For R 4.3.0 and higher you can fix this issue by going to https://mac.r-project.org/tools/ and installing the gfortran-12.2 universal:

R 4.3.0 and higher uses universal GNU Fortran 12.2 compiler. You can download an installer package gfortran-12.2-universal.pkg (242MB) - for more details and other download options see R-macos GNU Fortran releases on GitHub.

Seeing answered 13/6, 2023 at 16:35 Comment(1)
waited only 7 years for thisDiathesis
P
4

I received the same error on MAC. All I needed to do was to install gfortran-6.1.pkg from https://cran.r-project.org/bin/macosx/tools/. Make sure that the package is installed under /usr/local/gfortran so it can be found by R.

Alternatively you can install it via homebrew typing brew cask install gfortran in the terminal (it may ask for your password).

Pianissimo answered 30/1, 2020 at 21:12 Comment(0)
P
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.

Proserpina answered 8/6, 2021 at 13:17 Comment(1)
I am on version 11 too but somehow my gcc is linked as gcc, so I removed VER=-11 and it worked.Couloir
M
2

I am having Mac OS Catalina and in my case installation of Homebrew, the newest gcc and Gfortran 8.2.0 solved the issue.

Meatiness answered 28/11, 2019 at 9:12 Comment(2)
This worked for me in Mac OS Catalina as well! I used the gfortran 8.2 Mojave installation github.com/fxcoudert/gfortran-for-macOS/releasesCrowley
On Mojave, brew update && brew reinstall r followed by brew link --overwrite r worked.Prepuce
P
1

The solution was to re-install problematic packages with Homebrew.

$ brew uninstall --ignore-dependencies --force openssl
$ brew install openssl

$ brew uninstall --ignore-dependencies --force readline  
$ brew install readline
Photocomposition answered 7/1, 2020 at 11:46 Comment(0)
I
0

Digging this topic, if you tried these and much others approaches related to this error and got no success, I strongly recommend you to check if your OSx - just in case - deleted the .zshrc file.

If that's the case, you should create again this file in the root dir. type in your terminal:

  • touch .zshrc

followed by

  • openssl_prefix=$(brew --prefix openssl) printf "CFLAGS=-I$openssl_prefix/include \nLDFLAGS=-L$openssl_prefix/lib"

example of output:

  • CFLAGS=-I/usr/local/opt/openssl@3/include LDFLAGS=-L/usr/local/opt/openssl@3/lib

add them to the zshrcfile content, run

  • source .zshrc to refresh the file and that`s all!
Iyeyasu answered 15/3, 2022 at 17:1 Comment(0)
T
0

Had this problem using R on macos 12.4. It was caused by old and stale entries in my ~/.R/Makevars file. Once I commented everything out everything compiled just fine. I have XCode and brew install gcc active.

Trichinosis answered 23/6, 2022 at 2:8 Comment(0)
T
0

For later versions of R (R 4.0 and higher) and later operating systems, the recommended fix for this problem seems to be to install gfortran via rtools (see here, already mentioned in the answer of @lks_swrx).

When following these instructions, I ran into an additional problem, since my computer uses Apple Silicon chips (not Intel anymore), for which I wanted to add a solution here:

  1. Download the appropriate gfortran tarball from rtools
  2. Run tar fxz gfortran-12.0.1-20220312-is-darwin20-arm64.tar.xz -C /
  3. Add it to your path by adding this line to your .zshrc file:
    nano .zshrc (or whatever you use as text editor)
    export PATH=$PATH:/opt/R/arm64/gfortran/bin

So far, so good; but the installation of DESeq2, which brought me here, did still not work, because R could not find the library. This is the fix:

  1. Create a symlink to the gfortran library in /usr/local, so that R can find it:
    ln -s /opt/R/arm64/gfortran /usr/local/gfortran (might require sudo)
Telemechanics answered 20/9, 2022 at 17:54 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.