Javier Javier - 3 months ago 20
R Question

Rcpp warning: "directory not found for option '-L/usr/local/Cellar/gfortran/4.8.2/gfortran'"

This question relates to some others out there, like RccpArmadillo or element-wise-multiplication.

However, my settings are such that I do not know what I have to edit/simlink to make Rccp run without giving me warnings.

I am on an Mac 10.9 (mavericks) using the latest R version.

At the very beginning, trying the following code I got from here RccpvsR, I got an error:

ld: library not found for -lgfortran
clang: error: linker command failed with exit code 1 (use -v to see invocation)`


Then, based on RccpArmadillo I did the following:

# Update FLIBS in ~/.R/Makevars
FLIBS=-L/usr/local/Cellar/gfortran/4.8.2/gfortran
#Re-Install from source
install.packages(c("Rcpp","RcppArmadillo","inline"),type="source")
#Restart R


this was JUST trying things out since I have NO
/usr/local/Cellar/gfortran/
directory. In fact, all my
libgfortran*
files are here (At the macports dir):

>ls /opt/local/lib/gcc48/libgfortran.*
/opt/local/lib/gcc48/libgfortran.3.dylib /opt/local/lib/gcc48/libgfortran.dylib
/opt/local/lib/gcc48/libgfortran.a /opt/local/lib/gcc48/libgfortran.spec


and here
/opt/local/lib/gcc48/gcc/x86_64-apple-darwin13/4.8.3/libgfortranbegin.a
and I have no
gfortran
file anywhere.

Then I tried the code RccpvsR again and surprisingly, it worked!. Apart from the fact that I get a warning:

ld: warning: directory not found for option '-L/usr/local/Cellar/gfortran/4.8.2/gfortran'


because of course, it does not exists, but the function created by that code,
cosineRcpp
, runs with no problems.

Therefore, all that, to ask if anyone knows if I have to simlink the
libgfortran
files at
/opt/local/lib/gcc48/
as:

ln -s /opt/local/lib/gcc48/libgfortran.* /usr/local/lib/


and then remove/edit the line:

FLIBS=-L/usr/local/Cellar/gfortran/4.8.2/gfortran


at
~/.R/Makevars


or if I have to install something new.

thanks in advance for your time!

Answer

Short Answer

Just put the path to libgfortran into FLIBS, e.g.

FLIBS=-L/opt/local/lib/gcc48/

Or, symlink the files within to /usr/local/lib/, if you're comfortable with that. This solution is, however, quite brittle as it's easy to forget to update this path if you update gfortran, or move it to a different directory.

Long Answer

Try parsing an appropriate FLIBS directly from gfortran output.

First, some background. The /usr/local/Cellar directory is the default path used by homebrew, a package manager for OS X. Think of it as an alternative to macports.

Homebrew now provides gfortran and its associated libraries as part of the gcc package, and so the paths where it installs FORTRAN libraries has now changed. However, these can (in general) be discovered using gfortran -print-search-dirs. For example, on my system,

gfortran -print-search-dirs

will give me

install: /usr/local/Cellar/gcc/4.9.2_1/lib/gcc/4.9/gcc/x86_64-apple-darwin14.0.0/4.9.2/
programs: =/usr/local/Cellar/gcc/4.9.2_1/libexec/gcc/x86_64-apple-darwin14.0.0/4.9.2/:/usr/local/Cellar/gcc/4.9.2_1/libexec/gcc/x86_64-apple-darwin14.0.0/4.9.2/:/usr/local/Cellar/gcc/4.9.2_1/libexec/gcc/x86_64-apple-darwin14.0.0/:/usr/local/Cellar/gcc/4.9.2_1/lib/gcc/4.9/gcc/x86_64-apple-darwin14.0.0/4.9.2/:/usr/local/Cellar/gcc/4.9.2_1/lib/gcc/4.9/gcc/x86_64-apple-darwin14.0.0/:/usr/local/Cellar/gcc/4.9.2_1/lib/gcc/4.9/gcc/x86_64-apple-darwin14.0.0/4.9.2/../../../../../../x86_64-apple-darwin14.0.0/bin/x86_64-apple-darwin14.0.0/4.9.2/:/usr/local/Cellar/gcc/4.9.2_1/lib/gcc/4.9/gcc/x86_64-apple-darwin14.0.0/4.9.2/../../../../../../x86_64-apple-darwin14.0.0/bin/
libraries: =/usr/local/Cellar/gcc/4.9.2_1/lib/gcc/4.9/gcc/x86_64-apple-darwin14.0.0/4.9.2/:/usr/local/Cellar/gcc/4.9.2_1/lib/gcc/4.9/gcc/x86_64-apple-darwin14.0.0/4.9.2/../../../../../../x86_64-apple-darwin14.0.0/lib/x86_64-apple-darwin14.0.0/4.9.2/:/usr/local/Cellar/gcc/4.9.2_1/lib/gcc/4.9/gcc/x86_64-apple-darwin14.0.0/4.9.2/../../../../../../x86_64-apple-darwin14.0.0/lib/:/usr/local/Cellar/gcc/4.9.2_1/lib/gcc/4.9/gcc/x86_64-apple-darwin14.0.0/4.9.2/../../../x86_64-apple-darwin14.0.0/4.9.2/:/usr/local/Cellar/gcc/4.9.2_1/lib/gcc/4.9/gcc/x86_64-apple-darwin14.0.0/4.9.2/../../../:/lib/x86_64-apple-darwin14.0.0/4.9.2/:/lib/:/usr/lib/x86_64-apple-darwin14.0.0/4.9.2/:/usr/lib/

Split, and printed with R, I see:

[[1]]
[1] "/usr/local/Cellar/gcc/4.9.2_1/lib/gcc/4.9/gcc/x86_64-apple-darwin14.0.0/4.9.2/"

[[2]]
[1] "/usr/local/Cellar/gcc/4.9.2_1/libexec/gcc/x86_64-apple-darwin14.0.0/4.9.2/"                                                                                    
[2] "/usr/local/Cellar/gcc/4.9.2_1/libexec/gcc/x86_64-apple-darwin14.0.0/4.9.2/"                                                                                    
[3] "/usr/local/Cellar/gcc/4.9.2_1/libexec/gcc/x86_64-apple-darwin14.0.0/"                                                                                          
[4] "/usr/local/Cellar/gcc/4.9.2_1/lib/gcc/4.9/gcc/x86_64-apple-darwin14.0.0/4.9.2/"                                                                                
[5] "/usr/local/Cellar/gcc/4.9.2_1/lib/gcc/4.9/gcc/x86_64-apple-darwin14.0.0/"                                                                                      
[6] "/usr/local/Cellar/gcc/4.9.2_1/lib/gcc/4.9/gcc/x86_64-apple-darwin14.0.0/4.9.2/../../../../../../x86_64-apple-darwin14.0.0/bin/x86_64-apple-darwin14.0.0/4.9.2/"
[7] "/usr/local/Cellar/gcc/4.9.2_1/lib/gcc/4.9/gcc/x86_64-apple-darwin14.0.0/4.9.2/../../../../../../x86_64-apple-darwin14.0.0/bin/"                                

[[3]]
[1] "/usr/local/Cellar/gcc/4.9.2_1/lib/gcc/4.9/gcc/x86_64-apple-darwin14.0.0/4.9.2/"                                                                                
[2] "/usr/local/Cellar/gcc/4.9.2_1/lib/gcc/4.9/gcc/x86_64-apple-darwin14.0.0/4.9.2/../../../../../../x86_64-apple-darwin14.0.0/lib/x86_64-apple-darwin14.0.0/4.9.2/"
[3] "/usr/local/Cellar/gcc/4.9.2_1/lib/gcc/4.9/gcc/x86_64-apple-darwin14.0.0/4.9.2/../../../../../../x86_64-apple-darwin14.0.0/lib/"                                
[4] "/usr/local/Cellar/gcc/4.9.2_1/lib/gcc/4.9/gcc/x86_64-apple-darwin14.0.0/4.9.2/../../../x86_64-apple-darwin14.0.0/4.9.2/"                                       
[5] "/usr/local/Cellar/gcc/4.9.2_1/lib/gcc/4.9/gcc/x86_64-apple-darwin14.0.0/4.9.2/../../../"                                                                       
[6] "/lib/x86_64-apple-darwin14.0.0/4.9.2/"                                                                                                                         
[7] "/lib/"                                                                                                                                                         
[8] "/usr/lib/x86_64-apple-darwin14.0.0/4.9.2/"                                                                                                                     
[9] "/usr/lib/"  

In my case, libgfortran actually lives here:

/usr/local/Cellar/gcc/4.9.2_1/lib/gcc/4.9/gcc/x86_64-apple-darwin14.0.0/4.9.2/../../../

And so this is the path we want to pass to FLIBS. But, pulling that out is kind of a pain, so let's just tell FLIBS to use whatever paths are normally used by gfortran:

gfortran -print-search-dirs | grep ^libraries: | sed 's|libraries: =||'

This is nice, but we want the library paths in a format suitable for the compiler; ie, with -L prepended. Let's do that with sed:

gfortran -print-search-dirs | grep ^libraries: | sed 's|libraries: =||' | sed 's|:| -L|g' | sed 's|^|-L|'

This outputs (split for readability)

-L/usr/local/Cellar/gcc/4.9.2_1/lib/gcc/4.9/gcc/x86_64-apple-darwin14.0.0/4.9.2/
-L/usr/local/Cellar/gcc/4.9.2_1/lib/gcc/4.9/gcc/x86_64-apple-darwin14.0.0/4.9.2/../../../../../../x86_64-apple-darwin14.0.0/lib/x86_64-apple-darwin14.0.0/4.9.2/
-L/usr/local/Cellar/gcc/4.9.2_1/lib/gcc/4.9/gcc/x86_64-apple-darwin14.0.0/4.9.2/../../../../../../x86_64-apple-darwin14.0.0/lib/
-L/usr/local/Cellar/gcc/4.9.2_1/lib/gcc/4.9/gcc/x86_64-apple-darwin14.0.0/4.9.2/../../../x86_64-apple-darwin14.0.0/4.9.2/
-L/usr/local/Cellar/gcc/4.9.2_1/lib/gcc/4.9/gcc/x86_64-apple-darwin14.0.0/4.9.2/../../../
-L/lib/x86_64-apple-darwin14.0.0/4.9.2/
-L/lib/
-L/usr/lib/x86_64-apple-darwin14.0.0/4.9.2/
-L/usr/lib/

All together, this implies that the following should work for you, at least on OS X, but should (in general) work on any platform with gfortran (as long as it's on the PATH):

FLIBS=`gfortran -print-search-dirs | grep ^libraries: | sed 's|libraries: =||' | sed 's|:| -L|g' | sed 's|^|-L|'`

This isn't perfect, e.g. it will fail if you have spaces in your paths -- if you do, 1) you deserve what you get and 2) it should also be a 'relatively' easy fix.