RStan fails to build example model

Hi,
I succesfully used rstan with previous versions of R, Rtools and rstan (maybe R 4.1.2 and Rtools4, I don’t remember well).
I updated R to R 4.2.0, and rtools to Rtools42;
I removed all previous installations of rstan and configuration files, then I configured C toolchain and installed rstan following instructions (Configuring C Toolchain for Windows · stan-dev/rstan Wiki · GitHub and RStan Getting Started · stan-dev/rstan Wiki · GitHub).

I can compile packages from sources and install rstan, but when I run the example model I get a compilation error:

TRANSLATING MODEL '73fc79f8b1915e8208c736914c86d1a1' FROM Stan CODE TO C++ CODE NOW.
successful in parsing the Stan model '73fc79f8b1915e8208c736914c86d1a1'.
COMPILING THE C++ CODE FOR MODEL '73fc79f8b1915e8208c736914c86d1a1' NOW.
OS: x86_64, mingw32; rstan: 2.21.5; Rcpp: 1.0.8.3; inline: 0.3.19 
 >> setting environment variables: 
LOCAL_LIBS =  "C:/Users/Luca/R/win-library/4.2/rstan/lib/x64/libStanServices.a" -L"C:/Users/Luca/R/win-library/4.2/StanHeaders/libs/x64" -lStanHeaders -L"C:/Users/Luca/R/win-library/4.2/RcppParallel/lib/x64" -ltbb
PKG_CPPFLAGS =   -I"C:/Users/Luca/R/win-library/4.2/Rcpp/include/"  -I"C:/Users/Luca/R/win-library/4.2/RcppEigen/include/"  -I"C:/Users/Luca/R/win-library/4.2/RcppEigen/include/unsupported"  -I"C:/Users/Luca/R/win-library/4.2/BH/include" -I"C:/Users/Luca/R/win-library/4.2/StanHeaders/include/src/"  -I"C:/Users/Luca/R/win-library/4.2/StanHeaders/include/"  -I"C:/Users/Luca/R/win-library/4.2/RcppParallel/include/"  -I"C:/Users/Luca/R/win-library/4.2/rstan/include" -DEIGEN_NO_DEBUG  -DBOOST_DISABLE_ASSERTS  -DBOOST_PENDING_INTEGER_LOG2_HPP  -DSTAN_THREADS  -DBOOST_NO_AUTO_PTR  -include "C:/Users/Luca/R/win-library/4.2/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp"  -std=c++1y

[…]

make cmd is
  make -f "C:/PROGRA~1/R/R-42~1.0/etc/x64/Makeconf" -f "C:/PROGRA~1/R/R-42~1.0/share/make/winshlib.mk" -f "C:/Users/Luca/OneDrive/Documenti/.R/Makevars.win" CXX='$(CXX14) $(CXX14STD)' CXXFLAGS='$(CXX14FLAGS)' CXXPICFLAGS='$(CXX14PICFLAGS)' SHLIB_LDFLAGS='$(SHLIB_CXX14LDFLAGS)' SHLIB_LD='$(SHLIB_CXX14LD)' SHLIB="file2046d32f53.dll" WIN=64 TCLBIN= OBJECTS="file2046d32f53.o"

make would use
g++  -std=gnu++14 -I"C:/PROGRA~1/R/R-42~1.0/include" -DNDEBUG   -I"C:/Users/Luca/R/win-library/4.2/Rcpp/include/"  -I"C:/Users/Luca/R/win-library/4.2/RcppEigen/include/"  -I"C:/Users/Luca/R/win-library/4.2/RcppEigen/include/unsupported"  -I"C:/Users/Luca/R/win-library/4.2/BH/include" -I"C:/Users/Luca/R/win-library/4.2/StanHeaders/include/src/"  -I"C:/Users/Luca/R/win-library/4.2/StanHeaders/include/"  -I"C:/Users/Luca/R/win-library/4.2/RcppParallel/include/"  -I"C:/Users/Luca/R/win-library/4.2/rstan/include" -DEIGEN_NO_DEBUG  -DBOOST_DISABLE_ASSERTS  -DBOOST_PENDING_INTEGER_LOG2_HPP  -DSTAN_THREADS  -DBOOST_NO_AUTO_PTR  -include "C:/Users/Luca/R/win-library/4.2/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp"  -std=c++1y    -I"C:/rtools42/x86_64-w64-mingw32.static.posix/include"     -O2 -Wall  -mfpmath=sse -msse2 -mstackrealign  -mtune=native -O3 -mmmx -msse -msse2 -msse3 -mssse3 -msse4.1 -msse4.2 -c file2046d32f53.cpp -o file2046d32f53.o
if test "zfile2046d32f53.o" != "z"; then \
  if test -e "file2046d32f53-win.def"; then \
    echo g++  -shared -s -static-libgcc -o file2046d32f53.dll file2046d32f53-win.def file2046d32f53.o  -L"C:/rtools42/x86_64-w64-mingw32.static.posix/lib/x64" -L"C:/rtools42/x86_64-w64-mingw32.static.posix/lib"  -L"C:/PROGRA~1/R/R-42~1.0/bin/x64" -lR ; \
    g++  -shared -s -static-libgcc -o file2046d32f53.dll file2046d32f53-win.def file2046d32f53.o  -L"C:/rtools42/x86_64-w64-mingw32.static.posix/lib/x64" -L"C:/rtools42/x86_64-w64-mingw32.static.posix/lib"  -L"C:/PROGRA~1/R/R-42~1.0/bin/x64" -lR ; \
  else \
    echo EXPORTS > tmp.def; \
    nm file2046d32f53.o | sed -n 's/^.* [BCDRT] / /p' | sed -e '/[.]refptr[.]/d' -e '/[.]weak[.]/d' | sed 's/[^ ][^ ]*/"&"/g'  >> tmp.def; \
    echo g++  -shared -s -static-libgcc -o file2046d32f53.dll tmp.def file2046d32f53.o  -L"C:/rtools42/x86_64-w64-mingw32.static.posix/lib/x64" -L"C:/rtools42/x86_64-w64-mingw32.static.posix/lib"  -L"C:/PROGRA~1/R/R-42~1.0/bin/x64" -lR ; \
    g++  -shared -s -static-libgcc -o file2046d32f53.dll tmp.def file2046d32f53.o  -L"C:/rtools42/x86_64-w64-mingw32.static.posix/lib/x64" -L"C:/rtools42/x86_64-w64-mingw32.static.posix/lib"  -L"C:/PROGRA~1/R/R-42~1.0/bin/x64" -lR ; \
    rm -f tmp.def; \
  fi \
fi

ERROR(s) during compilation: source code errors or compiler configuration errors!

[…]

Compilation ERROR, function(s)/method(s) not created!
Error in compileCode(f, code, language = language, verbose = verbose) : 
  C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: file2046d32f53.o:file2046d32f53:(.text$_ZN3tbb8internal26task_scheduler_observer_v3D0Ev[_ZN3tbb8internal26task_scheduler_observer_v3D0Ev]+0x1d): undefined reference to `tbb::internal::task_scheduler_observer_v3::observe(bool)'C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: file2046d32f53.o:file2046d32f53:(.text$_ZN3tbb10interface623task_scheduler_observerD1Ev[_ZN3tbb10interface623task_scheduler_observerD1Ev]+0x1d): undefined reference to `tbb::internal::task_scheduler_observer_v3::observe(bool)'C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: file2046d32f53.o:file2046d32f53:(.text$_ZN3tbb10interface623task_scheduler_observerD1Ev[_ZN3tbb10interface623task_scheduler_observerD1Ev]+0x3a): undefined reference to `tbb::internal::task_scheduler_observer_v3::observe(bool)'C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: file2046d32f53.o:file2046d32f53:(.text$_ZN3tbb10interface623task_scheduler_observerD0Ev[_

The error persists even if I install rstan and StanHeaders from source.

I’m on Windows 11

Those are my Makevars and session infos:

> writeLines(readLines(file.path(Sys.getenv("HOME"), ".R/Makevars")))
Error in file(con, "r") : cannot open the connection
In addition: Warning message:
In file(con, "r") :
  cannot open file 'C:/Users/Luca/OneDrive/Documenti/.R/Makevars': No such file or directory


> writeLines(readLines(file.path(Sys.getenv("HOME"), ".R/Makevars.win")))

 CXX14FLAGS += -mtune=native -O3 -mmmx -msse -msse2 -msse3 -mssse3 -msse4.1 -msse4.2

> devtools::session_info("rstan")
─ Session info ────────────────────────────────────────────────────────────────────────────────────────────────
 setting  value
 version  R version 4.2.0 (2022-04-22 ucrt)
 os       Windows 10 x64 (build 22000)
 system   x86_64, mingw32
 ui       RStudio
 language (EN)
 collate  Italian_Italy.utf8
 ctype    Italian_Italy.utf8
 tz       Europe/Berlin
 date     2022-04-25
 rstudio  2022.02.1+461 Prairie Trillium (desktop)
 pandoc   NA

─ Packages ────────────────────────────────────────────────────────────────────────────────────────────────────
 package      * version   date (UTC) lib source
 backports      1.4.1     2021-12-13 [1] CRAN (R 4.2.0)
 BH             1.78.0-0  2021-12-15 [1] CRAN (R 4.2.0)
 callr          3.7.0     2021-04-20 [1] CRAN (R 4.2.0)
 checkmate      2.1.0     2022-04-21 [1] CRAN (R 4.2.0)
 cli            3.3.0     2022-04-25 [1] CRAN (R 4.2.0)
 colorspace     2.0-3     2022-02-21 [1] CRAN (R 4.2.0)
 crayon         1.5.1     2022-03-26 [1] CRAN (R 4.2.0)
 desc           1.4.1     2022-03-06 [1] CRAN (R 4.2.0)
 digest         0.6.29    2021-12-01 [1] CRAN (R 4.2.0)
 ellipsis       0.3.2     2021-04-29 [1] CRAN (R 4.2.0)
 fansi          1.0.3     2022-03-24 [1] CRAN (R 4.2.0)
 farver         2.1.0     2021-02-28 [1] CRAN (R 4.2.0)
 ggplot2      * 3.3.5     2021-06-25 [1] CRAN (R 4.2.0)
 glue           1.6.2     2022-02-24 [1] CRAN (R 4.2.0)
 gridExtra      2.3       2017-09-09 [1] CRAN (R 4.2.0)
 gtable         0.3.0     2019-03-25 [1] CRAN (R 4.2.0)
 inline         0.3.19    2021-05-31 [1] CRAN (R 4.2.0)
 isoband        0.2.5     2021-07-13 [1] CRAN (R 4.2.0)
 labeling       0.4.2     2020-10-20 [1] CRAN (R 4.2.0)
 lattice        0.20-45   2021-09-22 [2] CRAN (R 4.2.0)
 lifecycle      1.0.1     2021-09-24 [1] CRAN (R 4.2.0)
 loo            2.5.1     2022-03-24 [1] CRAN (R 4.2.0)
 magrittr       2.0.3     2022-03-30 [1] CRAN (R 4.2.0)
 MASS           7.3-56    2022-03-23 [2] CRAN (R 4.2.0)
 Matrix         1.4-1     2022-03-23 [2] CRAN (R 4.2.0)
 matrixStats    0.62.0    2022-04-19 [1] CRAN (R 4.2.0)
 mgcv           1.8-40    2022-03-29 [2] CRAN (R 4.2.0)
 munsell        0.5.0     2018-06-12 [1] CRAN (R 4.2.0)
 nlme           3.1-157   2022-03-25 [2] CRAN (R 4.2.0)
 pillar         1.7.0     2022-02-01 [1] CRAN (R 4.2.0)
 pkgbuild       1.3.1     2021-12-20 [1] CRAN (R 4.2.0)
 pkgconfig      2.0.3     2019-09-22 [1] CRAN (R 4.2.0)
 prettyunits    1.1.1     2020-01-24 [1] CRAN (R 4.2.0)
 processx       3.5.3     2022-03-25 [1] CRAN (R 4.2.0)
 ps             1.7.0     2022-04-23 [1] CRAN (R 4.2.0)
 R6             2.5.1     2021-08-19 [1] CRAN (R 4.2.0)
 RColorBrewer   1.1-3     2022-04-03 [1] CRAN (R 4.2.0)
 Rcpp           1.0.8.3   2022-03-17 [1] CRAN (R 4.2.0)
 RcppEigen      0.3.3.9.2 2022-04-08 [1] CRAN (R 4.2.0)
 RcppParallel   5.1.5     2022-01-05 [1] CRAN (R 4.2.0)
 rlang          1.0.2     2022-03-04 [1] CRAN (R 4.2.0)
 rprojroot      2.0.3     2022-04-02 [1] CRAN (R 4.2.0)
 rstan        * 2.21.5    2022-04-11 [1] CRAN (R 4.2.0)
 scales         1.2.0     2022-04-13 [1] CRAN (R 4.2.0)
 StanHeaders  * 2.21.0-7  2020-12-17 [1] CRAN (R 4.2.0)
 tibble         3.1.6     2021-11-07 [1] CRAN (R 4.2.0)
 utf8           1.2.2     2021-07-24 [1] CRAN (R 4.2.0)
 vctrs          0.4.1     2022-04-13 [1] CRAN (R 4.2.0)
 viridisLite    0.4.0     2021-04-13 [1] CRAN (R 4.2.0)
 withr          2.5.0     2022-03-03 [1] CRAN (R 4.2.0)

 [1] C:/Users/Luca/R/win-library/4.2
 [2] C:/Program Files/R/R-4.2.0/library

Can anyone help me to make rstan working again?

Thank you

This is fixed in the development version of RStan >= 2.26.11. You can install it as follows:

remove.packages(c("StanHeaders", "rstan"))
install.packages("StanHeaders", repos = c("https://mc-stan.org/r-packages/", getOption("repos")))
install.packages("rstan", repos = c("https://mc-stan.org/r-packages/", getOption("repos")))

See R4.2 , Rstan & Windows · Issue #1006 · stan-dev/rstan · GitHub.

1 Like