Can't compile code using RStan

I updated Mac OS to Sonoma 14.0. I have M1 arm.

RStan stopped working. So I deleted R RStudio, installed XCode anew. Installed R, RStudio, Rstan.

It all works, except compilation. For example, rstanarm works, so Stan works.

However it doesn’t compile. By trying to get a basic Stan model to compile, I get:

Error in compileCode(f, code, language = language, verbose = verbose) : 
  /Applications/ error: <cstring> tried including <string.h> but didn't find libc++'s <string.h> header.           This usually means that your header search paths are not configured properly.           The header search paths should contain the C++ Standard Library headers before           any C Standard Library, and you are probably using compiler flags that make that           not be the case.#   error <cstring> tried including <string.h> but didn't find libc++'s <string.h> header. \    ^fatal error: too many errors emitted, stopping now [-ferror-limit=]20 errors generated.make: *** [file75d10bdefef.o] Error 1

I think the problem is the same as the one here: RStanArm working, but RStan is unable to find C++ compiler - #8 by bgoodri

I ran

> Sys.which("g++")

And got


So I added CXX = “/usr/bin/g++” to ~/.R/Makevars

Still doesn’t work. I know it’s not a Stan problem but I wonder maybe someone knows.

I’ll be upgrading to OS14 on my M1 mac in a few days here so I can play around and see what’s going on.

I am having the same problem on an Intel Mac Sonoma OS 14.1.1
rstanarm works fine. I get a compilation error when compiling a stan example. (see message below). Ideas appreciated.

Compilation ERROR, function(s)/method(s) not created!
Error in compileCode(f, code, language = language, verbose = verbose) :
^file45ea3e955d03.cpp:430:60: note: in instantiation of member function ‘rstan::stan_fit<model45ea527f724a_example_namespace::model45ea527f724a_example, boost::random::additive_combine_engine<boost::random::linear_congruential_engine<unsigned int, 40014, 0, 2147483563>, boost::random::linear_congruential_engine<unsigned int, 40692, 0, 2147483399>>>::standalone_gqs’ requested here boost::random::ecuyer1988>::standalone_gqs); ^102 warnings and 1 error generated.make: *** [file45ea3e955d03.o] Error 1

We have a threads in a few places that might help


What I did:I removed Command Line Tools. I updated Sonoma to 14.1.1. I deleted completely Homebrew (this was radical and I’m not sure it was needed, but I didn’t really need Homebrew). I installed the latest regular (non-beta) Xcode. I had tried before with CLT only; that didn’t work. CLT 15.1 beta didn’t work for me. Actually all CLT didn’t work for me.

So, update sonoma to latest, update Xcode to latest.

This: Gcc 13.1.0 macOS Sonoma not compiling Ada or any languages on Intel or M1 Macs - Stack Overflow

explains that there’s a problem with Sonoma and Xcode. I suspect this was the issue.

1 Like

Hi Justin,
Just to clarify. You delete CLT, (skip the macrtools C++ toolchain config - since it installs CLT), then run the rstan .dotR script to configure .R/Makevars. Then install?

Based on my reading of this: Gcc 13.1.0 macOS Sonoma not compiling Ada or any languages on Intel or M1 Macs - Stack Overflow, I figured that the problem was the interaction between CLT/XCode and Sonoma.

I did my Sonoma upgrades on Mac, upgrading to 14.1.1.
I removed CLT from terminal: sudo rm -rf /Library/Developer/CommandLineTools
I installed Xcode from the Mac App Store. So here, since it’s not CLT I didn’t do anything from command line. Just literal download and install.

I’m not sure what rstan .dotR file is. I didn’t configure .R/Makevars. I’ve used Makevars in the past but now it’s empty. I’m not using it. I installed rstan the usual way with install.packages(“rstan”).

OK thanks for your help. I’m running a 2 year old intel Mac. Upgraded to Sonoma 14.1.1, removed CLT and installed a new Xcode 15.0.1. I just tried removing the makevars, and re-installing rstan. I still get the message “ERROR(s) during compilation: source code errors or compiler configuration errors!”

I did install cmdstan and cmdstanr, and this works as expected.

Now when I attempt to build/compile a stan model (in Rstudio) I get a message to “Install Build Tools”. However, the R session pukes when I try to install them.
“Error in sink(type = “output”) : invalid connection”

Alas - I will continue to explore.