Rstan crashing in Windows

Hi
I’m having trouble getting RStan running in Windows. I have a brand new installation of R with the latest R RStudio and Rtools.

> sessionInfo()
R version 4.2.1 (2022-06-23 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19044)

When I try to run the test example at RStan Getting Started · stan-dev/rstan Wiki · GitHub I get the following error. Is it because I don’t have the right path to make?

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

stn_md> stancode <- 'data {real y_mean;} parameters {real y;} model {y ~ normal(y_mean,1);}'

stn_md> mod <- stan_model(model_code = stancode, verbose = TRUE)

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:/R_Packages/rstan/lib/x64/libStanServices.a" -L"C:/R_Packages/StanHeaders/libs/x64" -lStanHeaders -L"C:/R_Packages/RcppParallel/lib/x64" -ltbb
PKG_CPPFLAGS =   -I"C:/R_Packages/Rcpp/include/"  -I"C:/R_Packages/RcppEigen/include/"  -I"C:/R_Packages/RcppEigen/include/unsupported"  -I"C:/R_Packages/BH/include" -I"C:/R_Packages/StanHeaders/include/src/"  -I"C:/R_Packages/StanHeaders/include/"  -I"C:/R_Packages/RcppParallel/include/"  -I"C:/R_Packages/rstan/include" -DEIGEN_NO_DEBUG  -DBOOST_DISABLE_ASSERTS  -DBOOST_PENDING_INTEGER_LOG2_HPP  -DSTAN_THREADS  -DBOOST_NO_AUTO_PTR  -include "C:/R_Packages/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp"  -std=c++1y
 >> Program source :

   1 : 
   2 : // includes from the plugin
   3 : // [[Rcpp::plugins(cpp14)]]
....
 365 : 
 366 : // definition
 367 : SEXP file3c186c3b127a() {
 368 :  return Rcpp::wrap("73fc79f8b1915e8208c736914c86d1a1");
 369 : }
make cmd is
  make -f "C:/PROGRA~1/R/etc/x64/Makeconf" -f "C:/PROGRA~1/R/share/make/winshlib.mk" -f "C:/Users/WoodwardS/OneDrive - DairyNZ Limited/Documents/.R/Makevars.win" CXX='$(CXX14) $(CXX14STD)' CXXFLAGS='$(CXX14FLAGS)' CXXPICFLAGS='$(CXX14PICFLAGS)' SHLIB_LDFLAGS='$(SHLIB_CXX14LDFLAGS)' SHLIB_LD='$(SHLIB_CXX14LD)' SHLIB="file3c186c3b127a.dll" WIN=64 TCLBIN= OBJECTS="file3c186c3b127a.o"

make would use
g++  -std=gnu++14 -I"C:/PROGRA~1/R/include" -DNDEBUG   -I"C:/R_Packages/Rcpp/include/"  -I"C:/R_Packages/RcppEigen/include/"  -I"C:/R_Packages/RcppEigen/include/unsupported"  -I"C:/R_Packages/BH/include" -I"C:/R_Packages/StanHeaders/include/src/"  -I"C:/R_Packages/StanHeaders/include/"  -I"C:/R_Packages/RcppParallel/include/"  -I"C:/R_Packages/rstan/include" -DEIGEN_NO_DEBUG  -DBOOST_DISABLE_ASSERTS  -DBOOST_PENDING_INTEGER_LOG2_HPP  -DSTAN_THREADS  -DBOOST_NO_AUTO_PTR  -include "C:/R_Packages/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp"  -std=c++1y    -I"C:/rtools42/x86_64-w64-mingw32.static.posix/include"     -O3 -mtune=native -mmmx -msse -msse2 -msse3 -mssse3 -msse4.1 -msse4.2 -c file3c186c3b127a.cpp -o file3c186c3b127a.o
if test "zfile3c186c3b127a.o" != "z"; then \
  if test -e "file3c186c3b127a-win.def"; then \
    echo g++  -shared -s -static-libgcc -o file3c186c3b127a.dll file3c186c3b127a-win.def file3c186c3b127a.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/bin/x64" -lR ; \
    g++  -shared -s -static-libgcc -o file3c186c3b127a.dll file3c186c3b127a-win.def file3c186c3b127a.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/bin/x64" -lR ; \
  else \
    echo EXPORTS > tmp.def; \
    nm file3c186c3b127a.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 file3c186c3b127a.dll tmp.def file3c186c3b127a.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/bin/x64" -lR ; \
    g++  -shared -s -static-libgcc -o file3c186c3b127a.dll tmp.def file3c186c3b127a.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/bin/x64" -lR ; \
    rm -f tmp.def; \
  fi \
fi

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

Program source:
  1: 
  2: // includes from the plugin
  3: // [[Rcpp::plugins(cpp14)]]
  4: 
....
366: // definition
367: SEXP file3c186c3b127a() {
368:  return Rcpp::wrap("73fc79f8b1915e8208c736914c86d1a1");
369: }

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: file3c186c3b127a.o:file3c186c3b127a.cpp:(.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: file3c186c3b127a.o:file3c186c3b127a.cpp:(.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: file3c186c3b127a.o:file3c186c3b127a.cpp:(.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: file3c186c3b127a.o:file3c186c3b127a.cpp:(.text$_ZN3tbb10interface

See the Getting Started instructions for windows: Configuring C Toolchain for Windows · stan-dev/rstan Wiki · GitHub

1 Like

Thanks! This seems to solve the problem. Do I still need the compiler flags recommended forolder versions in ny .Renviron file ?:

  # https://github.com/stan-dev/rstan/wiki/RStan-Getting-Started
  dotR <- file.path(Sys.getenv("HOME"), ".R")
  if (!file.exists(dotR)) dir.create(dotR)
  M <- file.path(dotR, ifelse(.Platform$OS.type == "windows", "Makevars.win", "Makevars"))
  if (!file.exists(M)) file.create(M)
  cat(if( grepl("^darwin", R.version$os)) "\nCXX14FLAGS=-O3 -march=native -mtune=native -arch x86_64 -ftemplate-depth-256" else
    if (.Platform$OS.type == "windows") "\nCXX14FLAGS=-O3 -mtune=native -mmmx -msse -msse2 -msse3 -mssse3 -msse4.1 -msse4.2" else
      "CXX14FLAGS = -fPIC",
    file = M, sep = "\n", append = TRUE)
  # Sys.setenv(LOCAL_CPPFLAGS = '-march=native -mtune=native')

Those flags are for all R versions and will help speed up your models, so definitely keep them