Error in compilation after rstan reinstall


I recently had to uninstall and reinstall R, and thus all packages (including “StanHeaders” and “rstan”). I am trying to run a model with a code that previously worked without any trouble, but I am getting this error when I try to compile the Stan model:

> StanModel <- stan_model(here("Stan_models/model_vaccination.stan"))
make cmd is
  make -f "C:/PROGRA~1/R/R-43~1.0/etc/x64/Makeconf" -f "C:/PROGRA~1/R/R-43~1.0/share/make/" -f "C:/Users/nd1316/Documents/.R/" CXX='$(CXX14) $(CXX14STD)' CXXFLAGS='$(CXX14FLAGS)' CXXPICFLAGS='$(CXX14PICFLAGS)' SHLIB_LDFLAGS='$(SHLIB_CXX14LDFLAGS)' SHLIB_LD='$(SHLIB_CXX14LD)' SHLIB="file5f1049c64190.dll" WIN=64 TCLBIN= OBJECTS="file5f1049c64190.o"

make would use
if test "zfile5f1049c64190.o" != "z"; then \
  if test -e "file5f1049c64190-win.def"; then \
    echo g++  -shared -s -static-libgcc -o file5f1049c64190.dll file5f1049c64190-win.def file5f1049c64190.o  -L"c:/rtools43/x86_64-w64-mingw32.static.posix/lib/x64" -L"c:/rtools43/x86_64-w64-mingw32.static.posix/lib"  -L"C:/PROGRA~1/R/R-43~1.0/bin/x64" -lR ; \
    g++  -shared -s -static-libgcc -o file5f1049c64190.dll file5f1049c64190-win.def file5f1049c64190.o  -L"c:/rtools43/x86_64-w64-mingw32.static.posix/lib/x64" -L"c:/rtools43/x86_64-w64-mingw32.static.posix/lib"  -L"C:/PROGRA~1/R/R-43~1.0/bin/x64" -lR ; \
  else \
    echo EXPORTS > tmp.def; \
    nm file5f1049c64190.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 file5f1049c64190.dll tmp.def file5f1049c64190.o  -L"c:/rtools43/x86_64-w64-mingw32.static.posix/lib/x64" -L"c:/rtools43/x86_64-w64-mingw32.static.posix/lib"  -L"C:/PROGRA~1/R/R-43~1.0/bin/x64" -lR ; \
    g++  -shared -s -static-libgcc -o file5f1049c64190.dll tmp.def file5f1049c64190.o  -L"c:/rtools43/x86_64-w64-mingw32.static.posix/lib/x64" -L"c:/rtools43/x86_64-w64-mingw32.static.posix/lib"  -L"C:/PROGRA~1/R/R-43~1.0/bin/x64" -lR ; \
    rm -f tmp.def; \
  fi \
Error in compileCode(f, code, language = language, verbose = verbose) : 
  C:/RBuildTools/4.0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: file5f1049c64190.o:file5f1049c64190.cpp:(.text$_ZN3tbb8internal26task_scheduler_observer_v3D0Ev[_ZN3tbb8internal26task_scheduler_observer_v3D0Ev]+0x1c): undefined reference to `tbb::internal::task_scheduler_observer_v3::observe(bool)'C:/RBuildTools/4.0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: file5f1049c64190.o:file5f1049c64190.cpp:(.text$_ZN3tbb10interface623task_scheduler_observerD0Ev[_ZN3tbb10interface623task_scheduler_observerD0Ev]+0x1c): undefined reference to `tbb::internal::task_scheduler_observer_v3::observe(bool)'C:/RBuildTools/4.0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: file5f1049c64190.o:file5f1049c64190.cpp:(.text$_ZN3tbb10interface623task_scheduler_observerD0Ev[_ZN3tbb10interface623task_scheduler_observerD0Ev]+0x37): undefined reference to `tbb::internal::t
Error in sink(type = "output") : invalid connection

The session info is:

> sessionInfo()
R version 4.3.0 (2023-04-21 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 11 x64 (build 22621)

Matrix products: default

[1] LC_COLLATE=English_United Kingdom.utf8  LC_CTYPE=English_United Kingdom.utf8    LC_MONETARY=English_United Kingdom.utf8
[4] LC_NUMERIC=C                            LC_TIME=English_United Kingdom.utf8    

time zone: Europe/London
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] here_1.0.1          rstan_2.21.8        ggplot2_3.4.2       StanHeaders_2.26.27

loaded via a namespace (and not attached):
 [1] crayon_1.5.2       vctrs_0.6.2        cli_3.6.1          rlang_1.1.1        processx_3.8.1     generics_0.1.3     RcppParallel_5.1.7
 [8] glue_1.6.2         prettyunits_1.1.1  colorspace_2.1-0   rprojroot_2.0.3    gridExtra_2.3      ps_1.7.5           pkgbuild_1.4.2    
[15] stats4_4.3.0       scales_1.2.1       fansi_1.0.4        grid_4.3.0         munsell_0.5.0      tibble_3.2.1       lifecycle_1.0.3   
[22] inline_0.3.19      compiler_4.3.0     codetools_0.2-19   dplyr_1.1.2        Rcpp_1.0.10        pkgconfig_2.0.3    rstudioapi_0.14   
[29] R6_2.5.1           tidyselect_1.2.0   utf8_1.2.3         parallel_4.3.0     pillar_1.9.0       callr_3.7.3        magrittr_2.0.3    
[36] loo_2.6.0

I have tried downloading StanHeaders and rstan directly from CRAN, and then from here: install.packages("rstan", repos = c("", getOption("repos"))). Same result.

Any help would be appreciated, thanks!

1 Like

Hello Nieves,

I realised that Stan on Windows does not operate well with any R software that’s version 4.3.-something (I saying this because I tested this on both my MAC and Windows machine). I highly suggests you remove the R.4.3. entirely from your system. Remove the rstan and StanHeaders packages that was installed through R.4.3, as well as remove Rtools 4.3.

Once you have removed the above - I highly suggest, for now, you have R.4.2.3. and Rtools 4.2 installed as rstan package seems to work well with R that’s version 4.2.-something. When installing the rstan through R.4.2.3 use the following code to get the 2.26. version (see here: Configuring C Toolchain for Windows · stan-dev/rstan Wiki · GitHub ) and avoid installing rstan 2.22.1:

install.packages("StanHeaders", repos = c("", getOption("repos")))
install.packages("rstan", repos = c("", getOption("repos")))

I hope this works. I am curious to know if this resolves your problem. The problem is with the version of R - from my own and student’s experience - it does not work with R.4.3. As for RStudio, its fine, you can use the latest version of RStudio.


Thanks so much for the help, this indeed solved the problem!

1 Like