Can't compile a simple C file with Rstan on MacOS Sonoma 14.5

I recently updated my mac (intel) to Sonoma 14.5 and updated R to 4.4.0 and I can’t compile a stan model with Rstan anymore. I’ve been looking at different threads online but nothing seems to work.

library(rstan)

n <- 100
data <- data.frame(
    n = n,
    y = rnorm(n, 10, 2)
)

stan_model <- stan(file = "stan_programs/test.stan", data = data)

Running the code above, I obtain the following error message :

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 15.0.0 (clang-1500.3.9.4)’
using SDK: ‘’
clang -arch x86_64 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG   -I"/Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/library/Rcpp/include/"  -I"/Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/library/RcppEigen/include/"  -I"/Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/library/RcppEigen/include/unsupported"  -I"/Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/library/BH/include" -I"/Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/library/StanHeaders/include/src/"  -I"/Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/library/StanHeaders/include/"  -I"/Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/library/RcppParallel/include/"  -I"/Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/library/rstan/include" -DEIGEN_NO_DEBUG  -DBOOST_DISABLE_ASSERTS  -DBOOST_PENDING_INTEGER_LOG2_HPP  -DSTAN_THREADS  -DUSE_STANC3 -DSTRICT_R_HEADERS  -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION  -D_HAS_AUTO_PTR_ETC=0  -include '/Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/library/StanHeaders/include/stan/math/prim/fun/Eigen.hpp'  -D_REENTRANT -DRCPP_PARALLEL_USE_TBB=1  -I"/Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/library/Rcpp/include" -I/opt/R/x86_64/include    -fPIC  -falign-functions=64 -Wall -g -O2  -c foo.c -o foo.o
In file included from <built-in>:1:
In file included from /Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/library/StanHeaders/include/stan/math/prim/fun/Eigen.hpp:22:
In file included from /Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/library/RcppEigen/include/Eigen/Dense:1:
In file included from /Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/library/RcppEigen/include/Eigen/Core:19:
/Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/library/RcppEigen/include/Eigen/src/Core/util/Macros.h:679:10: fatal error: 'cmath' file not found
#include <cmath>
         ^~~~~~~
1 error 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' -f '/Users/xavierlarochelle/.R/Makevars' CXX='$(CXX17) $(CXX17STD)' CXXFLAGS='$(CXX17FLAGS)' CXXPICFLAGS='$(CXX17PICFLAGS)' SHLIB_LDFLAGS='$(SHLIB_CXX17LDFLAGS)' SHLIB_LD='$(SHLIB_CXX17LD)' SHLIB='file147af30a190f3.so' OBJECTS='file147af30a190f3.o'

make would use
clang++ -arch x86_64 -std=gnu++17 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG   -I"/Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/library/Rcpp/include/"  -I"/Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/library/RcppEigen/include/"  -I"/Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/library/RcppEigen/include/unsupported"  -I"/Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/library/BH/include" -I"/Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/library/StanHeaders/include/src/"  -I"/Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/library/StanHeaders/include/"  -I"/Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/library/RcppParallel/include/"  -I"/Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/library/rstan/include" -DEIGEN_NO_DEBUG  -DBOOST_DISABLE_ASSERTS  -DBOOST_PENDING_INTEGER_LOG2_HPP  -DSTAN_THREADS  -DUSE_STANC3 -DSTRICT_R_HEADERS  -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION  -D_HAS_AUTO_PTR_ETC=0  -include '/Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/library/StanHeaders/include/stan/math/prim/fun/Eigen.hpp'  -D_REENTRANT -DRCPP_PARALLEL_USE_TBB=1  -I"/Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/library/Rcpp/include" -I/opt/R/x86_64/include    -fPIC  -falign-functions=64 -Wall -g -O2  -O3 -mtune=native -arch x86_64 -ftemplate-depth-256  -c file147af30a190f3.cpp -o file147af30a190f3.o
if test  "zfile147af30a190f3.o" != "z"; then \
	  echo clang++ -arch x86_64 -std=gnu++17 -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -L"/Library/Frameworks/R.framework/Resources/lib" -L/opt/R/x86_64/lib -o file147af30a190f3.so file147af30a190f3.o  '/Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/library/rstan/lib//libStanServices.a' -L'/Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/library/StanHeaders/lib/' -lStanHeaders -L'/Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/library/RcppParallel/lib/' -ltbb    -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation; \
	  clang++ -arch x86_64 -std=gnu++17 -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -L"/Library/Frameworks/R.framework/Resources/lib" -L/opt/R/x86_64/lib -o file147af30a190f3.so file147af30a190f3.o  '/Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/library/rstan/lib//libStanServices.a' -L'/Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/library/StanHeaders/lib/' -lStanHeaders -L'/Library/Frameworks/R.framework/Versions/4.4-x86_64/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) : 
            ^/Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/library/StanHeaders/include/stan/math/prim/fun/inv_inc_beta.hpp:32:23: note: in instantiation of function template specialization 'boost::math::ibeta_inv<double, double, double, boost::math::policies::policy<boost::math::policies::overflow_error<boost::math::policies::errno_on_error>, boost::math::policies::pole_error<boost::math::policies::errno_on_error>, boost::math::policies::promote_double<false>, boost::math::policies::digits2<>>>' requested here  return boost::math::ibeta_inv(a, b, p, boost_policy_t<>());                      ^11 warnings and 1 error generated.make: *** [file147af30a190f3.o] Error 1
In addition: Warning message:
In readLines(file, warn = TRUE) :
  incomplete final line found on '/Users/xavierlarochelle/funtimes/stan_programs/test.stan'
Error in sink(type = "output") : invalid connection

I’m also running into some problems trying to re-install the C toolchain with macrtools. Specifically, the command macrtools::macos_rtools_install() executes normally but the diagnostics show that xcode_cli is not installed :

> macrtools::is_gfortran_installed()
[1] TRUE
> macrtools::is_xcode_app_installed()
[1] TRUE
> macrtools::is_xcode_cli_installed()
[1] FALSE

This is odd because I should have it installed.

I’m out of ideas. What’s the next step?

Have you tried reinstalling RStan from scratch? You probably need to update your C++ toolchain to go along with the new version of R. You also need to turn on the command-line interface to Xcode (CLI), which you can do from the command line, which I believe you can do with this command:

> xcode-select --install

and then verify it works with the Xcode-select command, which should return as follows:

> xcode-select -p
/Applications/Xcode.app/Contents/Developer
1 Like

After reinstalling RStan (I desinstalled it first with remove.packages("rstan")), I still get the same error messages.

Here’s what I get when I try to run the commands above in the terminal :

(base) MacBook-Pro-de-Xavier:~ xavierlarochelle$ xcode-select --install
xcode-select: note: Command line tools are already installed. Use "Software Update" in System Settings or the softwareupdate command line interface to install updates
(base) MacBook-Pro-de-Xavier:~ xavierlarochelle$ xcode-select -p
/Applications/Xcode.app/Contents/Developer

Is there anything else I need to install?

Small update on my problem : I tried installing cmdstan to see if I could get a model working with it. Turns out it worked but I had to deactivate all active conda environments. I’m not sure how conda environments work but I figured it might be a clue to someone who does.

I’d really like to go back to rstan because it’s the interface I’m the most comfortable using. If anyone has an idea what my problem might be, please let me know.