Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Instructions for compiler libraries #58

Closed
stineb opened this issue Feb 14, 2022 · 13 comments
Closed

Instructions for compiler libraries #58

stineb opened this issue Feb 14, 2022 · 13 comments

Comments

@stineb
Copy link
Collaborator

stineb commented Feb 14, 2022

I got an error when trying to install rsofun on a new machine where I had just installed gfortran using homebrew. The problem was that gfortran libraries could not be located. Here is what I did to resolve the problem. Should we include such instructions in the README?

  1. Verify gfortran installation.
which gfortran

Should return a path indicating where gfortran is installed.

  1. Locate library. homebrew installs them under /usr/local/Cellar, where you should find a subdirectory gcc. From there, locate a file named lib:
cd /usr/local/Cellar/gcc
find . -name lib

This should return the library path. In my case, this was ./11.2.0_3/lib (full path /usr/local/Cellar/gcc/11.2.0_3/lib).

  1. In that directory, locate the subdirectory where files *.dylib are located. I did
cd /usr/local/Cellar/gcc/11.2.0_3/lib
find . -name *.dylib
  1. Open the Makeconf file which contains information for R about compiler and library locations. You'll find that file by entering in your R console:
file.path(R.home("etc"), "Makeconf")

In my case, this returned "/Library/Frameworks/R.framework/Resources/etc/Makeconf". Open that file and edit the line where the variable FLIBS is specified, providing the information about the library locations determined under 2. and 3. The edited line now reads:

FLIBS =  -L/usr/local/Cellar/gcc/11.2.0_3/lib/gcc/11 -L/usr/local/Cellar/gcc/11.2.0_3/lib -lgfortran -lquadmath -lm
@khufkens
Copy link
Contributor

khufkens commented Feb 14, 2022

Install gfortran using:

brew install gcc

as gfortran is now rolled into gcc

Then:

  1. Create a file ~/.R/Makevars (if it does not exist yet)

  2. Add the following to ~/.R/Makevars

FC      =  /usr/local/opt/gcc/bin/gfortran
F77     = /usr/local/opt/gcc/bin/gfortran
FLIBS   = -L/usr/local/opt/gcc/lib        
  1. Restart R

  2. Install package

@khufkens khufkens pinned this issue Mar 10, 2022
@stineb
Copy link
Collaborator Author

stineb commented Aug 9, 2022

Note:
The solution by @khufkens is to be preferred.
On M1 processors (new Mac models), brew installs to a different directory. The following lines should be added to ~/.R/Makevars:

FC      = /opt/homebrew/bin/gfortran
F77     = /opt/homebrew/bin/gfortran
FLIBS   = -L/opt/homebrew/Cellar/gcc/12.1.0/lib

@oscarperezpriego
Copy link

I try to solve the problem on my Monterrey Mac but but I got problems with not finding the library for -lemutls_w
which path should I specify?
I my case, I modified the Makeconf with the following:
FC = /opt/homebrew/bin/gfortran -mtune=native
FLIBS = -L/opt/homebrew/Cellar/gcc/12.2.0/lib/gcc/12 -lgfortran -lemutls_w -lquadmath

@stineb
Copy link
Collaborator Author

stineb commented Aug 31, 2022

Does it work without the flags -lgfortran -lemutls_w -lquadmath?

@oscarperezpriego
Copy link

Yes, it worked without the flags!! installed!! thanks a lot!

@fgiardin
Copy link

I am trying to install rsofun on my MacBook with M1 processor and I am getting this error:

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

Exited with status 1.

I tried first with the solution by @khufkens with the paths corrected by @stineb. Then I also tried the first solution provided by @stineb but I couldn't locate a directory called "Cellar" in /usr/local (I was showing all hidden files).

@khufkens
Copy link
Contributor

@fgiardin verify the path of your brew install. Depending on your setup this might be either global or locally installed. Key is to find out where gfortran lives.

@oscarperezpriego
Copy link

oscarperezpriego commented Sep 29, 2022 via email

@fgiardin
Copy link

If I run "whic fortran" on my terminal I do get:
/opt/homebrew/bin/gfortran

But still no trace of the directory called "Cellar" in /usr/local, and I still get the error mentioned above while installing the package

@oscarperezpriego
Copy link

oscarperezpriego commented Sep 30, 2022

Have you already modified the Makeconf as suggested by @stineb with the following?

FC = /opt/homebrew/bin/gfortran -mtune=native
FLIBS = -L/opt/homebrew/Cellar/gcc/12.2.0/lib/gcc/12

Though, make sure your version.. my case 12.2.0 but you should check

@fgiardin
Copy link

fgiardin commented Nov 11, 2022

After trying several things, what worked for my M1 Mac was to first completely unistall R, Homebrew and libraries. You probably want to back up your HD before doing so.

To remove R run in terminal:
rm -rf /Applications/R.app
sudo rm -rf /Library/Frameworks/R.framework
sudo rm /usr/bin/{R,Rscript}
sudo rm -rf /usr/local/gcc4.0

You can then drag and drop R and RStudio from the applications folder to the trash. To make sure that R has been totally removed, you can manually delete the directory ~/.R/ if it's still there.

Re-install R and RStudio: https://posit.co/download/rstudio-desktop/
Install the latest version of R compatible with Apple Silicon. I installed R-4.2.2-arm64.pkg.

I then followed this guide to correctly install Xcode and gfrotran for Apple silicon Macs. Note that this installation doesn't recommend using Homebrew as it is "by definition incompatible with macOS native libraries and applications".

You can now build the rsofun package. Note that some dependencies won't be automatically installed (e.g. BayesianTools). I had to install those directly from github:
devtools::install_github(repo = "florianhartig/BayesianTools", subdir = "BayesianTools", dependencies = T, build_vignettes = T).

@khufkens
Copy link
Contributor

Thanks for the notes @fgiardin !

@fgiardin
Copy link

Another small note: I realised that ingestr wasn't running as it needs some external libraries that we normally install through Brew. So I ended up installing Brew (after everything I did in my previous post on this issue) and run on terminal:

1. brew install netcdf (installs also hdf5)
2. brew install gdal (installs also python)
3. brew install ncview
4. brew install cdo
5. brew install nco

Remember to always restart R after installing external libraries. Also: it's risky to install gfortran with Brew too, as you will have two installations at the same time. So I made sure I didn't run brew install gcc once I installed Brew.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants