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.