0

I am trying to get R and Rstudio up and running on my m1 MacBook with monterey 12.2. I have had a lot of problems with different ways of trying to install the c++ toolchain but this great answer seemed to fix almost everything. I have followed all the steps in that guide and made a few changes to the Makevars file (see below). Now when I run the test in the guide from the terminal, it only gives me one warning but otherwise runs fine. The warning is: 'ld: warning: dylib (/opt/R/arm64/gfortran/lib/libgfortran.dylib) was built for newer macOS version (12.1) than being linked (12.0)'. This seems to be no big problem since the test works.

The problem for me is when I move to Rstudio, it gives me the same warning but it cannot run the test. Instead it gives me these errors:

Error in dyn.load("/private/var/folders/tq/mmh1nq5157j8h6vvpxx3kx7c0000gn/T/RtmpxK22i4/sourceCpp-aarch64-apple-darwin20-1.0.8/sourcecpp_6130297636cb/sourceCpp_2.so") : unable to load shared object '/private/var/folders/tq/mmh1nq5157j8h6vvpxx3kx7c0000gn/T/RtmpxK22i4/sourceCpp-aarch64-apple-darwin20-1.0.8/sourcecpp_6130297636cb/sourceCpp_2.so': dlopen(/private/var/folders/tq/mmh1nq5157j8h6vvpxx3kx7c0000gn/T/RtmpxK22i4/sourceCpp-aarch64-apple-darwin20-1.0.8/sourcecpp_6130297636cb/sourceCpp_2.so, 0x0006): Library not loaded: @rpath/libgfortran.5.dylib Referenced from: /private/var/folders/tq/mmh1nq5157j8h6vvpxx3kx7c0000gn/T/RtmpxK22i4/sourceCpp-aarch64-apple-darwin20-1.0.8/sourcecpp_6130297636cb/sourceCpp_2.so Reason: tried: '/usr/lib/libgfortran.5.dylib' (no such file)

It seems to me that it looks after the gfortran in the wrong place. I have tried adding the home-brew opt folder and gfortran folder to path (and some folders willy-nilly because I wanted to try everything). This was inspired by this post.

This is my Makevars file now (almost the same as the linked answer in the beginning but with a few changes that solved some issues I had):

LLVM_DIR=/opt/homebrew/opt/llvm
LIBS_DIR=/opt/R/arm64
GFORTRAN_DIR=$(LIBS_DIR)/gfortran
SDK_DIR=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk

CC=$(LLVM_DIR)/bin/clang -isysroot $(SDK_DIR) -target arm64-apple-macos12
CXX=$(LLVM_DIR)/bin/clang++ -isysroot $(SDK_DIR) -target arm64-apple-macos12
FC=$(GFORTRAN_DIR)/bin/gfortran -mtune=native

CFLAGS=-falign-functions=64 -g -O2 -Wall -pedantic -Wno-implicit-function-declaration
CXXFLAGS=-falign-functions=64 -g -O2 -Wall -pedantic
FFLAGS=-g -O2 -Wall -pedantic

SHLIB_OPENMP_CFLAGS=-fopenmp
SHLIB_OPENMP_CXXFLAGS=-fopenmp
SHLIB_OPENMP_FFLAGS=-fopenmp

CPPFLAGS=-I$(LLVM_DIR)/include -I$(LIBS_DIR)/include
LDFLAGS=-L$(LLVM_DIR)/lib -L$(LIBS_DIR)/lib
FLIBS=-L$(GFORTRAN_DIR)/lib/gcc/aarch64-apple-darwin21/12.0.0 -L$(GFORTRAN_DIR)/lib -lgfortran -lemutls_w -lm

Thanks a lot in advance to anyone who might be able to help - I would have liked to ask the person who wrote the answer above but comments are not allowed for lurkers like me (probably for good reason). Let me know if I need to clarify anything!

Edit - this question was closed and I am not sure I understand the reasoning behind it: the given reason is that it should already be answered in the thread that I also linked in this post. I have reread that whole post a couple of times and if the answer is in there already, I cannot find it. I have gone through the whole process outlined in that answer three times and it does not seem to work for me when I move to Rstudio (which that answer does not mention). I would greatly appreciate it if somebody could point out what I am missing in the post or in my way of posing the question.

JeppeF
  • 1
  • 2
  • I've updated the instructions at the linked thread. The new instructions suggest a different `gfortran` binary, a different approach to obtaining OpenMP support, and (as a result) a much streamlined `Makevars`. Can you try them and report back? – Mikael Jagan Feb 09 '22 at 20:17
  • You should start by uninstalling your existing `gfortran` installation with `sudo rm -rf /opt/R/arm64/gfortran`. You can also uninstall the LLVM toolchain with `brew remove llvm`. – Mikael Jagan Feb 09 '22 at 20:22
  • I've verified that the current instructions work on my machine, which is running Big Sur. No compilation errors, linker warnings, etc., when running R from Terminal and from RStudio. I'd be interested in knowing if there are any glitches on Monterey. – Mikael Jagan Feb 09 '22 at 20:23
  • I'd also suggest reversing any changes that you've made to your `PATH`. Relying on `PATH` is dangerous because the value of `Sys.getenv("PATH")` can change depending on how you start R (i.e., from Terminal or from a GUI like RStudio). That may be part of your problem... – Mikael Jagan Feb 09 '22 at 21:29
  • Thanks a lot for your reply (and also for the original instructions) - I just tried your updated instructions and used them to get everything working. Now the test compiles in both R in the terminal and also in Rstudio. I still have no idea why it did not work before, but you might be correct in the fact that my path was part of the problem. Long story short; your instructions helped me a lot and got the basic c++ toolchain working on my machine running monterey 12.2. Now I can begin the next battle of getting rstan to work :) – JeppeF Feb 10 '22 at 20:49
  • That's great to hear - I'll update the answer to state that at least one person running Monterey found it helpful. And maybe I'll upgrade to Monterey myself... – Mikael Jagan Feb 10 '22 at 20:59
  • I would not recommend it - although you seem a lot more resourceful and will probably not have as hard a time as I am having :) I am still having some issues when compiling some packages so if your installation works it might be worth it to stick with Big Sur for a while! – JeppeF Feb 10 '22 at 21:37

0 Answers0