Can't compile simple stan code on M1 MacOS Ventura

I just switched back to MacOS and I’m trying to get Rstan up and running. I’ve followed the instructions, but I think R is having a problem finding the right C (c++? I’m a dilettante) compiler. I can compile C++ code in my terminal, but it doesn’t work in R.

For example, if I run the simple schools example:

library(rstan)
schools_dat <- list(J = 8, 
                    y = c(28,  8, -3,  7, -1,  1, 18, 12),
                    sigma = c(15, 10, 16, 11,  9, 11, 10, 18))
fit <- stan(file = '~/Desktop/schools.stan', data = schools_dat)

I get this output (and R studio prompts me to install the command line tools that I’ve already installed):

Trying to compile a simple C file
Running /Library/Frameworks/R.framework/Resources/bin/R CMD SHLIB foo.c
using C compiler: ‘clang version 5.0.1 (tags/RELEASE_501/final)’
using SDK: ‘’
clang -arch arm64 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG   -I"/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/Rcpp/include/"  -I"/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/RcppEigen/include/"  -I"/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/RcppEigen/include/unsupported"  -I"/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/BH/include" -I"/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/StanHeaders/include/src/"  -I"/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/StanHeaders/include/"  -I"/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/RcppParallel/include/"  -I"/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/rstan/include" -DEIGEN_NO_DEBUG  -DBOOST_DISABLE_ASSERTS  -DBOOST_PENDING_INTEGER_LOG2_HPP  -DSTAN_THREADS  -DBOOST_NO_AUTO_PTR  -include '/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/StanHeaders/include/stan/math/prim/fun/Eigen.hpp'  -D_REENTRANT -DRCPP_PARALLEL_USE_TBB=1   -I/opt/R/arm64/include    -fPIC  -falign-functions=64 -Wall -g -O2  -c foo.c -o foo.o
clang-5.0: warning: optimization flag '-falign-functions=64' is not supported [-Wignored-optimization-argument]
In file included from <built-in>:1:
In file included from /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/StanHeaders/include/stan/math/prim/fun/Eigen.hpp:22:
In file included from /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/RcppEigen/include/Eigen/Dense:1:
In file included from /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/RcppEigen/include/Eigen/Core:88:
/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/RcppEigen/include/Eigen/src/Core/util/Macros.h:628:1: error: unknown type name 'namespace'
namespace Eigen {
^
/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/RcppEigen/include/Eigen/src/Core/util/Macros.h:628:16: error: expected ';' after top level declarator
namespace Eigen {
               ^
               ;
In file included from <built-in>:1:
In file included from /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/StanHeaders/include/stan/math/prim/fun/Eigen.hpp:22:
In file included from /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/RcppEigen/include/Eigen/Dense:1:
/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/RcppEigen/include/Eigen/Core:96:10: fatal error: 'complex' file not found
#include <complex>
         ^~~~~~~~~
3 errors generated.
make: *** [foo.o] Error 1
make cmd is
  make -f '/Library/Frameworks/R.framework/Resources/etc/Makeconf' -f '/Library/Frameworks/R.framework/Resources/share/make/shlib.mk' CXX='$(CXX14) $(CXX14STD)' CXXFLAGS='$(CXX14FLAGS)' CXXPICFLAGS='$(CXX14PICFLAGS)' SHLIB_LDFLAGS='$(SHLIB_CXX14LDFLAGS)' SHLIB_LD='$(SHLIB_CXX14LD)' SHLIB='fileea29267c6a80.so' OBJECTS='fileea29267c6a80.o'

make would use
clang++ -arch arm64 -std=gnu++14 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG   -I"/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/Rcpp/include/"  -I"/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/RcppEigen/include/"  -I"/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/RcppEigen/include/unsupported"  -I"/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/BH/include" -I"/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/StanHeaders/include/src/"  -I"/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/StanHeaders/include/"  -I"/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/RcppParallel/include/"  -I"/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/rstan/include" -DEIGEN_NO_DEBUG  -DBOOST_DISABLE_ASSERTS  -DBOOST_PENDING_INTEGER_LOG2_HPP  -DSTAN_THREADS  -DBOOST_NO_AUTO_PTR  -include '/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/StanHeaders/include/stan/math/prim/fun/Eigen.hpp'  -D_REENTRANT -DRCPP_PARALLEL_USE_TBB=1   -I/opt/R/arm64/include    -fPIC  -falign-functions=64 -Wall -g -O2  -c fileea29267c6a80.cpp -o fileea29267c6a80.o
if test  "zfileea29267c6a80.o" != "z"; then \
	  echo clang++ -arch arm64 -std=gnu++14 -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L"/Library/Frameworks/R.framework/Resources/lib" -L/opt/R/arm64/lib -o fileea29267c6a80.so fileea29267c6a80.o  '/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/rstan/lib//libStanServices.a' -L'/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/StanHeaders/lib/' -lStanHeaders -L'/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/RcppParallel/lib/' -ltbb  -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation; \
	  clang++ -arch arm64 -std=gnu++14 -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L"/Library/Frameworks/R.framework/Resources/lib" -L/opt/R/arm64/lib -o fileea29267c6a80.so fileea29267c6a80.o  '/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/rstan/lib//libStanServices.a' -L'/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/StanHeaders/lib/' -lStanHeaders -L'/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/RcppParallel/lib/' -ltbb  -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation; \
	fi
Error in compileCode(f, code, language = language, verbose = verbose) : 
  using C++ compiler: ‘clang version 5.0.1 (tags/RELEASE_501/final)’using C++14using SDK: ‘’clang-5.0: warning: optimization flag '-falign-functions=64' is not supported [-Wignored-optimization-argument]In file included from <built-in>:1:In file included from /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/StanHeaders/include/stan/math/prim/fun/Eigen.hpp:22:In file included from /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/RcppEigen/include/Eigen/Dense:1:/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/RcppEigen/include/Eigen/Core:82:12: fatal error: 'new' file not found  #include <new>           ^~~~~1 error generated.make: *** [fileea29267c6a80.o] Error 1
Error in sink(type = "output") : invalid connection

I’ve spent the last 3 days working on this and I’ve tried a million different combinations of things in my Makevars, but nothing seems to work. I’ve also reinstalled R, Rstudio, command line tools, and Xcode several times.

Here is the session info:

devtools::session_info("rstan")
─ Session info ───────────────────────────────────────────────────────────────
 setting  value
 version  R version 4.3.1 (2023-06-16)
 os       macOS Ventura 13.5
 system   aarch64, darwin20
 ui       RStudio
 language (EN)
 collate  en_US.UTF-8
 ctype    en_US.UTF-8
 tz       America/New_York
 date     2023-07-29
 rstudio  2023.06.1+524 Mountain Hydrangea (desktop)
 pandoc   2.16.2 @ /usr/local/bin/pandoc

─ Packages ───────────────────────────────────────────────────────────────────
 package      * version   date (UTC) lib source
 backports      1.4.1     2021-12-13 [1] CRAN (R 4.3.0)
 BH             1.81.0-1  2023-01-22 [1] CRAN (R 4.3.0)
 callr          3.7.3     2022-11-02 [1] CRAN (R 4.3.0)
 checkmate      2.2.0     2023-04-27 [1] CRAN (R 4.3.0)
 cli            3.6.1     2023-03-23 [1] CRAN (R 4.3.0)
 colorspace     2.1-0     2023-01-23 [1] CRAN (R 4.3.0)
 crayon         1.5.2     2022-09-29 [1] CRAN (R 4.3.0)
 desc           1.4.2     2022-09-08 [1] CRAN (R 4.3.0)
 fansi          1.0.4     2023-01-22 [1] CRAN (R 4.3.0)
 farver         2.1.1     2022-07-06 [1] CRAN (R 4.3.0)
 ggplot2      * 3.4.2     2023-04-03 [1] CRAN (R 4.3.0)
 glue           1.6.2     2022-02-24 [1] CRAN (R 4.3.0)
 gridExtra      2.3       2017-09-09 [1] CRAN (R 4.3.0)
 gtable         0.3.3     2023-03-21 [1] CRAN (R 4.3.0)
 inline         0.3.19    2021-05-31 [1] CRAN (R 4.3.0)
 isoband        0.2.7     2022-12-20 [1] CRAN (R 4.3.0)
 labeling       0.4.2     2020-10-20 [1] CRAN (R 4.3.0)
 lattice        0.21-8    2023-04-05 [1] CRAN (R 4.3.1)
 lifecycle      1.0.3     2022-10-07 [1] CRAN (R 4.3.0)
 loo            2.6.0     2023-03-31 [1] CRAN (R 4.3.0)
 magrittr       2.0.3     2022-03-30 [1] CRAN (R 4.3.0)
 MASS           7.3-60    2023-05-04 [1] CRAN (R 4.3.1)
 Matrix         1.5-4.1   2023-05-18 [1] CRAN (R 4.3.1)
 matrixStats    1.0.0     2023-06-02 [1] CRAN (R 4.3.0)
 mgcv           1.8-42    2023-03-02 [1] CRAN (R 4.3.1)
 munsell        0.5.0     2018-06-12 [1] CRAN (R 4.3.0)
 nlme           3.1-162   2023-01-31 [1] CRAN (R 4.3.1)
 pillar         1.9.0     2023-03-22 [1] CRAN (R 4.3.0)
 pkgbuild       1.4.2     2023-06-26 [1] CRAN (R 4.3.0)
 pkgconfig      2.0.3     2019-09-22 [1] CRAN (R 4.3.0)
 prettyunits    1.1.1     2020-01-24 [1] CRAN (R 4.3.0)
 processx       3.8.2     2023-06-30 [1] CRAN (R 4.3.0)
 ps             1.7.5     2023-04-18 [1] CRAN (R 4.3.0)
 R6             2.5.1     2021-08-19 [1] CRAN (R 4.3.0)
 RColorBrewer   1.1-3     2022-04-03 [1] CRAN (R 4.3.0)
 Rcpp           1.0.11    2023-07-06 [1] CRAN (R 4.3.0)
 RcppEigen      0.3.3.9.3 2022-11-05 [1] CRAN (R 4.3.0)
 RcppParallel   5.1.7     2023-02-27 [1] CRAN (R 4.3.0)
 rlang          1.1.1     2023-04-28 [1] CRAN (R 4.3.0)
 rprojroot      2.0.3     2022-04-02 [1] CRAN (R 4.3.0)
 rstan        * 2.21.8    2023-01-17 [1] CRAN (R 4.3.0)
 scales         1.2.1     2022-08-20 [1] CRAN (R 4.3.0)
 StanHeaders  * 2.26.27   2023-06-14 [1] CRAN (R 4.3.0)
 tibble         3.2.1     2023-03-20 [1] CRAN (R 4.3.0)
 utf8           1.2.3     2023-01-31 [1] CRAN (R 4.3.0)
 vctrs          0.6.3     2023-06-14 [1] CRAN (R 4.3.0)
 viridisLite    0.4.2     2023-05-02 [1] CRAN (R 4.3.0)
 withr          2.5.0     2022-03-03 [1] CRAN (R 4.3.0)

 [1] /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library

Your output shows that your system is using the clang-5 compiler, whereas the current version in the CommandLineTools is clang-14.

In the terminal, what output do you get from the following commands:

clang -v
xcode-select -p
xcode-select -v
ls -l /Library/Developer/CommandLineTools/SDKs

Here is the output:

clang -v
clang version 5.0.1 (tags/RELEASE_501/final)
Target: x86_64-apple-darwin22.6.0
Thread model: posix
InstalledDir: /usr/local/bin
xcode-select -p
/Applications/Xcode.app/Contents/Developer
xcode-select -v
xcode-select version 2397.
total 0
lrwxr-xr-x  1 root  wheel   14 Jul 28 18:24 MacOSX.sdk -> MacOSX13.3.sdk
drwxr-xr-x  7 root  wheel  224 Jul 28 18:24 MacOSX12.3.sdk
lrwxr-xr-x  1 root  wheel   14 Jul 28 18:23 MacOSX12.sdk -> MacOSX12.3.sdk
drwxr-xr-x  7 root  wheel  224 Nov 12  2022 MacOSX13.1.sdk
drwxr-xr-x  7 root  wheel  224 Mar  9 19:58 MacOSX13.3.sdk
lrwxr-xr-x  1 root  wheel   14 Jul 28 18:22 MacOSX13.sdk -> MacOSX13.3.sdk

It looks like your system might be looking for the commandline tools in the wrong directory. Can you try running:

sudo xcode-select --switch /Library/Developer/CommandLineTools

Then restarting your terminal and running the debug commands again:

clang -v
xcode-select -p

Sure thing. Here is the output:

clang version 5.0.1 (tags/RELEASE_501/final)
Target: x86_64-apple-darwin22.6.0
Thread model: posix
InstalledDir: /usr/local/bin
/Library/Developer/CommandLineTools

Hmm still not finding the right compiler. The next issue is that I believe binaries in usr/local/bin take precedence over the commandline-tools, so your system is defaulting to this older clang version.

Let’s double-check that the commandline-tools clang version are all the right versions, can you run:

/Library/Developer/CommandLineTools/usr/bin/clang -v 
/usr/bin/clang -v

Those look like the 14.0 versions.

/Library/Developer/CommandLineTools/usr/bin/clang -v

Apple clang version 14.0.3 (clang-1403.0.22.14.1)
Target: x86_64-apple-darwin22.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
/usr/bin/clang -v

Apple clang version 14.0.3 (clang-1403.0.22.14.1)
Target: x86_64-apple-darwin22.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

Alright, so then it’s just the stray clang-5 in your /usr/local/bin.

Let’s rename the clang and clang++ binaries so that the newer system binaries will be found instead:

sudo mv /usr/local/bin/clang /usr/local/bin/clang-5
sudo mv /usr/local/bin/clang++ /usr/local/bin/clang++-5

Then if you run clang -v it should (hopefully) show the clang-14 version. If so, you can restart R and try the installation/example again

So, it’s now showing the right version in the terminal:

clang -v

Apple clang version 14.0.3 (clang-1403.0.22.14.1)
Target: x86_64-apple-darwin22.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

But the example is now throwing different a C-related error (I think):

Trying to compile a simple C file
Running /Library/Frameworks/R.framework/Resources/bin/R CMD SHLIB foo.c
using C compiler: ‘Apple clang version 14.0.3 (clang-1403.0.22.14.1)’
using SDK: ‘MacOSX13.3.sdk’
clang -arch arm64 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG   -I/opt/R/arm64/include    -fPIC  -falign-functions=64 -Wall -g -O2  -c foo.c -o foo.o
clang -arch arm64 -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/Library/Frameworks/R.framework/Resources/lib -L/opt/R/arm64/lib -o foo.so foo.o -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation
ld: in '/usr/local/lib/libunwind.dylib', building for macOS-arm64 but attempting to link with file built for macOS-x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [foo.so] Error 1
make cmd is
  make -f '/Library/Frameworks/R.framework/Resources/etc/Makeconf' -f '/Library/Frameworks/R.framework/Resources/share/make/shlib.mk' CXX='$(CXX14) $(CXX14STD)' CXXFLAGS='$(CXX14FLAGS)' CXXPICFLAGS='$(CXX14PICFLAGS)' SHLIB_LDFLAGS='$(SHLIB_CXX14LDFLAGS)' SHLIB_LD='$(SHLIB_CXX14LD)' SHLIB='file1052f7873b2dc.so' OBJECTS='file1052f7873b2dc.o'

make would use
if test  "zfile1052f7873b2dc.o" != "z"; then \
	  echo clang++ -arch arm64 -std=gnu++14 -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L"/Library/Frameworks/R.framework/Resources/lib" -L/opt/R/arm64/lib -o file1052f7873b2dc.so file1052f7873b2dc.o  '/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/rstan/lib//libStanServices.a' -L'/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/StanHeaders/lib/' -lStanHeaders -L'/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/RcppParallel/lib/' -ltbb  -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation; \
	  clang++ -arch arm64 -std=gnu++14 -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L"/Library/Frameworks/R.framework/Resources/lib" -L/opt/R/arm64/lib -o file1052f7873b2dc.so file1052f7873b2dc.o  '/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/rstan/lib//libStanServices.a' -L'/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/StanHeaders/lib/' -lStanHeaders -L'/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/RcppParallel/lib/' -ltbb  -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation; \
	fi
Error in compileCode(f, code, language = language, verbose = verbose) : 
                         ^55 warnings generated.ld: warning: ignoring file /usr/local/lib/libc++.dylib, building for macOS-arm64 but attempting to link with file built for macOS-x86_64ld: in '/usr/local/lib/libunwind.dylib', building for macOS-arm64 but attempting to link with file built for macOS-x86_64clang: error: linker command failed with exit code 1 (use -v to see invocation)make: *** [file1052f7873b2dc.so] Error 1
Error in sink(type = "output") : invalid connection

These are more stray files in your /usr/local folders. Are you using homebrew/macports?

Can you post the output from the following R commands:

Sys.getenv("PATH")
Sys.getenv("LDLIBS")
readLines("~/.R/Makevars")
readLines("~/.Renviron")
readLines("~/.Rprofile")

OK sorry for the double post, but I tried the example() model and it gave a more informative error at the bottom.

> example(stan_model, package = "rstan", run.dontrun = TRUE)

[ deleted stuff in here]

Compilation ERROR, function(s)/method(s) not created!
Error in compileCode(f, code, language = language, verbose = verbose) : 
                         ^55 warnings generated.ld: warning: ignoring file /usr/local/lib/libc++.dylib, building for macOS-arm64 but attempting to link with file built for macOS-x86_64ld: in '/usr/local/lib/libunwind.dylib', building for macOS-arm64 but attempting to link with file built for macOS-x86_64clang: error: linker command failed with exit code 1 (use -v to see invocation)make: *** [file10756488d913f.so] Error 1
> 
> Sys.getenv("PATH")
[1] "/opt/homebrew/bin:/opt/homebrew/bin:/opt/local/bin:/opt/local/sbin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin:/opt/X11/bin:/Library/Apple/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/Users/andrew/Applications/quarto/bin:/usr/texbin:/Applications/RStudio.app/Contents/Resources/app/quarto/bin:/Applications/RStudio.app/Contents/Resources/app/bin/postback:/Applications/RStudio.app/Contents/Resources/app/bin/postback"
> Sys.getenv("LDLIBS")
[1] ""
> readLines("~/.R/Makevars")
Error in file(con, "r") : cannot open the connection
In addition: Warning message:
In file(con, "r") :
  cannot open file '/Users/andrew/.R/Makevars': No such file or directory
> readLines("~/.Renviron")
[1] "" "" ""
> readLines("~/.Rprofile")
Error in file(con, "r") : cannot open the connection
In addition: Warning message:
In file(con, "r") :
  cannot open file '/Users/andrew/.Rprofile': No such file or directory

It looks like it is attempting to link against the old (x86) toolchain by default.

What output do you get from:

Sys.getenv("LDFLAGS")
readLines("~/.zshrc")
readLines("~/.bashrc")
> Sys.getenv("LDFLAGS")
[1] ""
> readLines("~/.zshrc")
 [1] ""                                                                                            
 [2] "# >>> conda initialize >>>"                                                                  
 [3] "# !! Contents within this block are managed by 'conda init' !!"                              
 [4] "__conda_setup=\"$('/Users/andrew/opt/anaconda3/bin/conda' 'shell.zsh' 'hook' 2> /dev/null)\""
 [5] "if [ $? -eq 0 ]; then"                                                                       
 [6] "    eval \"$__conda_setup\""                                                                 
 [7] "else"                                                                                        
 [8] "    if [ -f \"/Users/andrew/opt/anaconda3/etc/profile.d/conda.sh\" ]; then"                  
 [9] "        . \"/Users/andrew/opt/anaconda3/etc/profile.d/conda.sh\""                            
[10] "    else"                                                                                    
[11] "        export PATH=\"/Users/andrew/opt/anaconda3/bin:$PATH\""                               
[12] "    fi"                                                                                      
[13] "fi"                                                                                          
[14] "unset __conda_setup"                                                                         
[15] "# <<< conda initialize <<<"                                                                  
[16] "" 
> readLines("~/.bashrc")
Error in file(con, "r") : cannot open the connection
In addition: Warning message:
In file(con, "r") :
  cannot open file '/Users/andrew/.bashrc': No such file or directory

Hmm nothing obvious there, so I’m not sure where this old toolchain came from in your system or how to remove it.

We can try adding a compiler flag for R to look at the libraries provided by the commandline-tools. Can you add the following to your Makevars file:

LDFLAGS=-L/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib

Then trying the example model/installation again?

It threw a different error this time:

Error in compileCode(f, code, language = language, verbose = verbose) : 
          typedef double local_scalar_t__;                       ^55 warnings generated.ld: in '/usr/local/lib/libunwind.dylib', building for macOS-arm64 but attempting to link with file built for macOS-x86_64clang: error: linker command failed with exit code 1 (use -v to see invocation)make: *** [file1139371e1717c.so] Error 1

Do you think something weird is going on with the homebrew stuff? Here is the path and other output:

> Sys.getenv("PATH")
[1] "/opt/homebrew/bin:/opt/local/bin:/opt/local/sbin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin:/opt/X11/bin:/Library/Apple/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/Users/andrew/Applications/quarto/bin:/usr/texbin:/Applications/RStudio.app/Contents/Resources/app/quarto/bin:/Applications/RStudio.app/Contents/Resources/app/bin/postback"
> Sys.getenv("LDLIBS")
[1] ""
> readLines("~/.R/Makevars")
[1] "LDFLAGS=-L/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib"
> readLines("~/.Renviron")
[1] "" "" ""
> readLines("~/.Rprofile")
Error in file(con, "r") : cannot open the connection
In addition: Warning message:
In file(con, "r") :
  cannot open file '/Users/andrew/.Rprofile': No such file or directory
> Sys.getenv("LDFLAGS")
[1] ""
> readLines("~/.zshrc")
 [1] ""                                                                                            
 [2] "# >>> conda initialize >>>"                                                                  
 [3] "# !! Contents within this block are managed by 'conda init' !!"                              
 [4] "__conda_setup=\"$('/Users/andrew/opt/anaconda3/bin/conda' 'shell.zsh' 'hook' 2> /dev/null)\""
 [5] "if [ $? -eq 0 ]; then"                                                                       
 [6] "    eval \"$__conda_setup\""                                                                 
 [7] "else"                                                                                        
 [8] "    if [ -f \"/Users/andrew/opt/anaconda3/etc/profile.d/conda.sh\" ]; then"                  
 [9] "        . \"/Users/andrew/opt/anaconda3/etc/profile.d/conda.sh\""                            
[10] "    else"                                                                                    
[11] "        export PATH=\"/Users/andrew/opt/anaconda3/bin:$PATH\""                               
[12] "    fi"                                                                                      
[13] "fi"                                                                                          
[14] "unset __conda_setup"                                                                         
[15] "# <<< conda initialize <<<"                                                                  
[16] ""                                                                                            
> readLines("~/.bashrc")
Error in file(con, "r") : cannot open the connection
In addition: Warning message:
In file(con, "r") :
  cannot open file '/Users/andrew/.bashrc': No such file or directory```

It’s still the same issue unfortunately:

ld: in '/usr/local/lib/libunwind.dylib', building for macOS-arm64 but attempting to link with file built for macOS-x86_64

It looks like libunwind only exists for Intel Macs , which is why there is the error about the different architectures.

Can you try removing it:

brew remove libunwind

OK it’s working.

I uninstalled libunwind from /opt and from /usr/local/lib and now everything looks to be working. Thanks so much for helping me with this!

1 Like

Oh perfect, glad that was the magic bullet!

1 Like