Cannot compile codes using rstan

Hi,

I cannot run the example code (see below) after installing rstan and got compilation error.

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

I found a similar post (Can't compile code using RStan) but the solution didn’t work for me, so I’m posting my own to get some help. I am using M3 Macbook pro. Xcode version is 15.1. Other information is provided at the bottom of this post.

Thanks so much for your help in advance.

Compilation ERROR, function(s)/method(s) not created!
Error in compileCode(f, code, language = language, verbose = verbose) : 
  /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/cstring:66:5: 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: *** [file110492060260e.o] Error 1

Other information:

  • Operating System: Sonoma 14.2.1
  • RStan Version: 2.32.3
  • Output of writeLines(readLines(file.path(Sys.getenv("HOME"), ".R/Makevars"))): CXX14FLAGS += -O3 -arch arm64 -ftemplate-depth-256
  • Output of devtools::session_info("rstan"):
    ─ Session info ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
    setting value
    version R version 4.3.2 (2023-10-31)
    os macOS Sonoma 14.2.1
    system aarch64, darwin20
    ui RStudio
    language (EN)
    collate en_US.UTF-8
    ctype en_US.UTF-8
    tz America/Chicago
    date 2023-12-21
    rstudio 2023.12.0+369 Ocean Storm (desktop)
    pandoc 3.1.1 @ /private/var/folders/p7/ljyl9rrn0wq48hqdtx3xjctc0000gn/T/AppTranslocation/6FB034D7-4D82-426F-A56F-25E2BACF2994/d/RStudio.app/Contents/Resources/app/quarto/bin/tools/ (via rmarkdown)

─ Packages ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
package * version date (UTC) lib source
backports 1.4.1 2021-12-13 [2] CRAN (R 4.3.0)
BH 1.81.0-1 2023-01-22 [2] CRAN (R 4.3.0)
callr 3.7.3 2022-11-02 [2] CRAN (R 4.3.0)
checkmate 2.3.1 2023-12-04 [2] CRAN (R 4.3.1)
cli 3.6.2 2023-12-11 [2] CRAN (R 4.3.1)
colorspace 2.1-0 2023-01-23 [2] CRAN (R 4.3.0)
desc 1.4.3 2023-12-10 [2] CRAN (R 4.3.1)
fansi 1.0.6 2023-12-08 [2] CRAN (R 4.3.1)
farver 2.1.1 2022-07-06 [2] CRAN (R 4.3.0)
ggplot2 3.4.4 2023-10-12 [2] CRAN (R 4.3.1)
glue 1.6.2 2022-02-24 [2] CRAN (R 4.3.0)
gridExtra 2.3 2017-09-09 [2] CRAN (R 4.3.0)
gtable 0.3.4 2023-08-21 [2] CRAN (R 4.3.0)
inline 0.3.19 2021-05-31 [2] CRAN (R 4.3.0)
isoband 0.2.7 2022-12-20 [2] CRAN (R 4.3.0)
jsonlite 1.8.8 2023-12-04 [2] CRAN (R 4.3.1)
labeling 0.4.3 2023-08-29 [2] CRAN (R 4.3.0)
lattice 0.21-9 2023-10-01 [2] CRAN (R 4.3.2)
lifecycle 1.0.4 2023-11-07 [2] CRAN (R 4.3.1)
loo 2.6.0 2023-03-31 [2] CRAN (R 4.3.0)
magrittr 2.0.3 2022-03-30 [2] CRAN (R 4.3.0)
MASS 7.3-60 2023-05-04 [2] CRAN (R 4.3.2)
Matrix 1.6-1.1 2023-09-18 [2] CRAN (R 4.3.2)
matrixStats 1.2.0 2023-12-11 [2] CRAN (R 4.3.1)
mgcv 1.9-0 2023-07-11 [2] CRAN (R 4.3.2)
munsell 0.5.0 2018-06-12 [2] CRAN (R 4.3.0)
nlme 3.1-163 2023-08-09 [2] CRAN (R 4.3.2)
pillar 1.9.0 2023-03-22 [2] CRAN (R 4.3.0)
pkgbuild 1.4.3 2023-12-10 [2] CRAN (R 4.3.1)
pkgconfig 2.0.3 2019-09-22 [2] CRAN (R 4.3.0)
processx 3.8.3 2023-12-10 [2] CRAN (R 4.3.1)
ps 1.7.5 2023-04-18 [2] CRAN (R 4.3.0)
QuickJSR 1.0.8 2023-11-24 [2] CRAN (R 4.3.1)
R6 2.5.1 2021-08-19 [2] CRAN (R 4.3.0)
RColorBrewer 1.1-3 2022-04-03 [2] CRAN (R 4.3.0)
Rcpp 1.0.11 2023-07-06 [2] CRAN (R 4.3.0)
RcppEigen 0.3.3.9.4 2023-11-02 [2] CRAN (R 4.3.1)
RcppParallel 5.1.7 2023-02-27 [2] CRAN (R 4.3.0)
rlang 1.1.2 2023-11-04 [2] CRAN (R 4.3.1)
rstan * 2.32.3 2023-10-15 [1] CRAN (R 4.3.1)
scales 1.3.0 2023-11-28 [2] CRAN (R 4.3.1)
StanHeaders * 2.26.28 2023-09-07 [2] CRAN (R 4.3.0)
tibble 3.2.1 2023-03-20 [2] CRAN (R 4.3.0)
utf8 1.2.4 2023-10-22 [2] CRAN (R 4.3.1)
vctrs 0.6.5 2023-12-01 [2] CRAN (R 4.3.1)
viridisLite 0.4.2 2023-05-02 [2] CRAN (R 4.3.0)
withr 2.5.2 2023-10-30 [2] CRAN (R 4.3.1)

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