Updating to C++17 to use rstan 2.36

Title says it: I have the C++13 compiler but cannot figure out how to get the C++17 compiler to use rstan 2.36.

Relevant info (I think):

> sessionInfo()
R version 4.4.3 (2025-02-28 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 11 x64 (build 22631)

Matrix products: default


locale:
[1] LC_COLLATE=English_United States.utf8  LC_CTYPE=English_United States.utf8   
[3] LC_MONETARY=English_United States.utf8 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.utf8    

time zone: America/Los_Angeles
tzcode source: internal

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

loaded via a namespace (and not attached):
[1] processx_3.8.6    compiler_4.4.3    R6_2.6.1          tools_4.4.3       rstudioapi_0.17.1 callr_3.7.6      
[7] ps_1.9.0          pkgbuild_1.4.7

> pkgbuild::check_build_tools(debug = TRUE)
Trying to compile a simple C file
Running "C:/PROGRA~1/R/R-44~1.3/bin/x64/Rcmd.exe" SHLIB foo.c
using C compiler: 'gcc.exe (GCC) 13.3.0'
gcc  -I"C:/PROGRA~1/R/R-44~1.3/include" -DNDEBUG     -I"C:/RBuildTools/4.4/x86_64-w64-mingw32.static.posix/include"     -O2 -Wall  -mfpmath=sse -msse2 -mstackrealign  -c foo.c -o foo.o
gcc -shared -s -static-libgcc -o foo.dll tmp.def foo.o -LC:/RBuildTools/4.4/x86_64-w64-mingw32.static.posix/lib/x64 -LC:/RBuildTools/4.4/x86_64-w64-mingw32.static.posix/lib -LC:/PROGRA~1/R/R-44~1.3/bin/x64 -lR
 
Your system is ready to build packages!

Thanks for your help :)

This is definitely confusing, but gcc 13 means you have version 13 of the Gnu Compiler Collection, which includes a C++ compiler that is capable of building C++17 (and even newer versions). The version numbers aren’t necessarily the same as the C++ editions they support.

So you should be fine on that front – are you getting a specific error when using RStan?

1 Like

Thanks so much for helping me understand this.

The code compiles and runs fine with the version of rstan on cran.

Also, the code checks out as well with the newest version of R:

> rstan:::rstudio_stanc("stan-code-Poi-alt-2-2024.stan")
stan-code-Poi-alt-2-2024.stan is syntactically correct.

I am an error using the stan_model function. It is a big wall of black and red text, I’ve edited some of it out to hide some filepath info:

> stan.mod1 <- stan_model("stan-code-Poi-alt-2-2024.stan") 
make cmd is
  make -f "C:/PROGRA~1/R/R-44~1.3/etc/x64/Makeconf" -f "C:/PROGRA~1/R/R-44~1.3/share/make/winshlib.mk" CXX='$(CXX17) $(CXX17STD)' CXXFLAGS='$(CXX17FLAGS)' CXXPICFLAGS='$(CXX17PICFLAGS)' SHLIB_LDFLAGS='$(SHLIB_CXX17LDFLAGS)' SHLIB_LD='$(SHLIB_CXX17LD)' SHLIB="file2be865aa79f.dll" WIN=64 TCLBIN= OBJECTS="file2be865aa79f.o"

make would use
g++  -std=gnu++17 -I"C:/PROGRA~1/R/R-44~1.3/include" -DNDEBUG   -I"C:/Users/redacted/AppData/Local/R/win-library/4.4/Rcpp/include/"  -I"C:/Users/redacted/AppData/Local/R/win-library/4.4/RcppEigen/include/"  -I"C:/Users/redacted/AppData/Local/R/win-library/4.4/RcppEigen/include/unsupported"  -I"C:/Users/redacted/AppData/Local/R/win-library/4.4/BH/include" -I"C:/Users/redacted/AppData/Local/R/win-library/4.4/StanHeaders/include/src/"  -I"C:/Users/redacted/AppData/Local/R/win-library/4.4/StanHeaders/include/"  -I"C:/Users/redacted/AppData/Local/R/win-library/4.4/RcppParallel/include/" -DRCPP_PARALLEL_USE_TBB=1 -I"C:/Users/redacted/AppData/Local/R/win-library/4.4/rstan/include" -DEIGEN_NO_DEBUG  -DBOOST_DISABLE_ASSERTS  -DBOOST_PENDING_INTEGER_LOG2_HPP  -DSTAN_THREADS  -DUSE_STANC3 -DSTRICT_R_HEADERS  -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION  -D_HAS_AUTO_PTR_ETC=0  -include "C:/Users/redacted/AppData/Local/R/win-library/4.4/StanHeaders/include/stan/math/prim/fun/Eigen.hpp"  -std=c++1y    -I"C:/RBuildTools/4.4/x86_64-w64-mingw32.static.posix/include"     -O2 -Wall  -mfpmath=sse -msse2 -mstackrealign  -c file2be865aa79f.cpp -o file2be865aa79f.o
if test "zfile2be865aa79f.o" != "z"; then \
  if test -e "file2be865aa79f-win.def"; then \
    echo g++  -shared -s -static-libgcc -o file2be865aa79f.dll file2be865aa79f-win.def file2be865aa79f.o  "C:/Users/redacted/AppData/Local/R/win-library/4.4/rstan/lib/x64/libStanServices.a" -L"C:/Users/redacted/AppData/Local/R/win-library/4.4/StanHeaders/libs/x64" -lStanHeaders -L"C:/Users/redacted/AppData/Local/R/win-library/4.4/RcppParallel/lib/x64" -ltbb -L"C:/Users/redacted/AppData/Local/R/win-library/4.4/RcppParallel/libs/x64" -lRcppParallel -LC:/Users/redacted/AppData/Local/R/win-library/4.4/RcppParallel/lib/x64 -ltbb -ltbbmalloc -L"C:/RBuildTools/4.4/x86_64-w64-mingw32.static.posix/lib/x64" -L"C:/RBuildTools/4.4/x86_64-w64-mingw32.static.posix/lib"  -L"C:/PROGRA~1/R/R-44~1.3/bin/x64" -lR ; \
    g++  -shared -s -static-libgcc -o file2be865aa79f.dll file2be865aa79f-win.def file2be865aa79f.o  "C:/Users/redacted/AppData/Local/R/win-library/4.4/rstan/lib/x64/libStanServices.a" -L"C:/Users/redacted/AppData/Local/R/win-library/4.4/StanHeaders/libs/x64" -lStanHeaders -L"C:/Users/redacted/AppData/Local/R/win-library/4.4/RcppParallel/lib/x64" -ltbb -L"C:/Users/redacted/AppData/Local/R/win-library/4.4/RcppParallel/libs/x64" -lRcppParallel -LC:/Users/redacted/AppData/Local/R/win-library/4.4/RcppParallel/lib/x64 -ltbb -ltbbmalloc -L"C:/RBuildTools/4.4/x86_64-w64-mingw32.static.posix/lib/x64" -L"C:/RBuildTools/4.4/x86_64-w64-mingw32.static.posix/lib"  -L"C:/PROGRA~1/R/R-44~1.3/bin/x64" -lR ; \
  else \
    echo EXPORTS > tmp.def; \
    nm file2be865aa79f.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 file2be865aa79f.dll tmp.def file2be865aa79f.o  "C:/Users/redacted/AppData/Local/R/win-library/4.4/rstan/lib/x64/libStanServices.a" -L"C:/Users/redacted/AppData/Local/R/win-library/4.4/StanHeaders/libs/x64" -lStanHeaders -L"C:/Users/redacted/AppData/Local/R/win-library/4.4/RcppParallel/lib/x64" -ltbb -L"C:/Users/redacted/AppData/Local/R/win-library/4.4/RcppParallel/libs/x64" -lRcppParallel -LC:/Users/redacted/AppData/Local/R/win-library/4.4/RcppParallel/lib/x64 -ltbb -ltbbmalloc -L"C:/RBuildTools/4.4/x86_64-w64-mingw32.static.posix/lib/x64" -L"C:/RBuildTools/4.4/x86_64-w64-mingw32.static.posix/lib"  -L"C:/PROGRA~1/R/R-44~1.3/bin/x64" -lR ; \
    g++  -shared -s -static-libgcc -o file2be865aa79f.dll tmp.def file2be865aa79f.o  "C:/Users/redacted/AppData/Local/R/win-library/4.4/rstan/lib/x64/libStanServices.a" -L"C:/Users/redacted/AppData/Local/R/win-library/4.4/StanHeaders/libs/x64" -lStanHeaders -L"C:/Users/redacted/AppData/Local/R/win-library/4.4/RcppParallel/lib/x64" -ltbb -L"C:/Users/redacted/AppData/Local/R/win-library/4.4/RcppParallel/libs/x64" -lRcppParallel -LC:/Users/redacted/AppData/Local/R/win-library/4.4/RcppParallel/lib/x64 -ltbb -ltbbmalloc -L"C:/RBuildTools/4.4/x86_64-w64-mingw32.static.posix/lib/x64" -L"C:/RBuildTools/4.4/x86_64-w64-mingw32.static.posix/lib"  -L"C:/PROGRA~1/R/R-44~1.3/bin/x64" -lR ; \
    rm -f tmp.def; \
  fi \
fi
Error in compileCode(f, code, language = language, verbose = verbose) : 
   Cb>&, const Eigen::Matrix<double, R1, C1>&, const Eigen::Matrix<double, Rb, Cb>&, const Eigen::Matrix<double, Cb, Cb>&) [with Ta = stan::math::var_value<double>; int Ra = -1; int Ca = -1; Tb = double; int Rb = -1; int Cb = 1]'
C:/Users/redacted/AppData/Local/R/win-library/4.4/StanHeaders/include/stan/math/rev/fun/quad_form.hpp:91:0:   required from 'void stan::math::internal::quad_form_vari<Ta, Ra, Ca, Tb, Rb, Cb>::chain() [with Ta = stan::math::var_value<double>; int Ra = -1; int Ca = -1; Tb = double; int Rb = -1; int Cb = 1]'
C:/Users/redacted/AppData/Local/R/win-library/4.4/StanHeaders/include/stan/math/rev/fun/quad_form.hpp:88:0:   required from here
C:/Users/redacted/AppData/Local/R/win-library/4.4/RcppEigen/include/Eigen/src/Core/CoreEvaluators.h:1071:54: warning: ignoring attributes on template argument 'Eigen::internal::packet_traits<double>::type' {aka '__m128d'} [-Wignored-attributes]
make: *** [C:/PROGRA~1/R/R-44~1.3/etc/x64/Makeconf:296: file2be865aa79f.o
Error in sink(type = "output") : invalid connection

At this point I’ll ask @andrjohns if he can take a look at that compiler error

1 Like

If you don’t need any of the special features of rstan that are not available in bridgestan, I would strongly recommend converting to cmdstanr, which is much easier to install than rstan and also keeping up with the roughly quarterly language releases.

1 Like

Thanks for looking into this. I am not familiar with bridgestan. I am specifically looking for the sum to zero vector functionality.

I am familiar with cmndstanr and always have issues with it. For example:

> library(cmdstanr)
This is cmdstanr version 0.9.0
- CmdStanR documentation and vignettes: mc-stan.org/cmdstanr
- CmdStan path: C:/Users/redacted/.cmdstan/cmdstan-2.36.0
- CmdStan version: 2.36.0
> stan.mod1 <- cmdstan_model("stan-code-Poi-alt-2-2024.stan")
Compiling Stan program...
INFO: Could not find files for the given pattern(s).

make[1]: /bin/sh: Permission denied

make[1]: /bin/sh: Permission denied

../tbb_2020.3/build/common.inc:81: *** Architecture not detected.  Stop.
make: *** [stan/lib/stan_math/make/libraries:179: stan/lib/stan_math/lib/tbb/tbb.def] Error 2

Error: An error occured during compilation! See the message above for more information.

It looks like the same error for both rstan and cmdstanr - you don’t have permissions to use the RTools compilers. Is this a work/university PC?

Are you able to install packages from source? What output do you get from:

install.packages("Rcpp", type="source")
1 Like

Thanks again so much. It is a work computer.

I get the following:

> install.packages("Rcpp", type="source")
Installing package into β€˜C:/Users/redcated/AppData/Local/R/win-library/4.4’
(as β€˜lib’ is unspecified)
trying URL 'https://cran.rstudio.com/src/contrib/Rcpp_1.0.14.tar.gz'
Content type 'application/x-gzip' length 3013288 bytes (2.9 MB)
downloaded 2.9 MB

* installing *source* package 'Rcpp' ...
** package 'Rcpp' successfully unpacked and MD5 sums checked
ERROR: cannot remove earlier installation, is it in use?
* removing 'C:/Users/redacted/AppData/Local/R/win-library/4.4/Rcpp'
* restoring previous 'C:/Users/redacted/AppData/Local/R/win-library/4.4/Rcpp'
Warning in file.copy(lp, dirname(pkgdir), recursive = TRUE, copy.date = TRUE) :
  problem copying C:\Users\redacted\AppData\Local\R\win-library\4.4\00LOCK-Rcpp\Rcpp\libs\x64\Rcpp.dll to C:\Users\redacted\AppData\Local\R\win-library\4.4\Rcpp\libs\x64\Rcpp.dll: Permission denied
Warning in install.packages :
  installation of package β€˜Rcpp’ had non-zero exit status

The downloaded source packages are in
	β€˜C:\Users\redacted\AppData\Local\Temp\RtmpQJs6wc\downloaded_packages’

Can you restart R/RStudio, making sure no packages/projects are loaded, and try the command again? Looks like the package was already loaded

1 Like

Exact same error. More infor for clarity:

> sessionInfo()
R version 4.4.3 (2025-02-28 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 11 x64 (build 22631)

Matrix products: default


locale:
[1] LC_COLLATE=English_United States.utf8  LC_CTYPE=English_United States.utf8   
[3] LC_MONETARY=English_United States.utf8 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.utf8    

time zone: America/Los_Angeles
tzcode source: internal

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

loaded via a namespace (and not attached):
[1] compiler_4.4.3    tools_4.4.3       rstudioapi_0.17.1

In that case, what output do you get from:

remove.packages("Rcpp")
install.packages("Rcpp", type="source")
1 Like

A nice clean install.

Edit: tried compiling my stan model again, same error as in initial post. I will reply tomorrow when I am at work.

Hi All,

Any thoughts on where to go from here?

Did you verify the installation? According to this page, the code is:

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

and it should be okay, in my case there is an error:

Compilation ERROR, function(s)/method(s) not created!
Error en compileCode(f, code, language = language, verbose = verbose): 
  tric.hpp:22:0:   required from 'double stan::mcmc::dense_e_metric<Model, BaseRNG>::T(stan::mcmc::dense_e_point&) [with Model = modele64438b1918__namespace::modele64438b1918_; BaseRNG = boost::random::mixmax_engine<17, 36, 0>]'
   22 |     return 0.5 * z.p.transpose() * z.inv_e_metric_ * z.p;
C:/Users/rosgori/AppData/Local/R/win-library/4.5/StanHeaders/include/src/stan/mcmc/hmc/hamiltonians/dense_e_metric.hpp:21:0:   required from here
   21 |   double T(dense_e_point& z) {
C:/Users/rosgori/AppData/Local/R/win-library/4.5/RcppEigen/include/Eigen/src/Core/DenseCoeffsBase.h:654:74: warning: ignoring attributes on template argument 'Eigen::internal::packet_traits<double>::type' {aka '__m128d'} [-Wignored-attributes]
  654 |   return internal::first_aligned<int(unpacket_traits<DefaultPacketType>::alignment),Derived>(m);
      |                                                                          ^~~~~~~~~
make: *** [C:/PROGRA~1/R/R-45~1.0/etc/x64/Makeconf:296: filee647de17426.o

According to this, I tried to install RStan by two methods:

  1. Windows β†’ RStan β†’ R-Universe
  2. Windows β†’ RStan β†’ Github (Source)

The first one shows that error; you could say that after the installation, there are errors within RStan and you cannot compile the Stan model. Nothing you can do here.

With the second method, there is another error!

package β€˜StanHeaders’ successfully unpacked and MD5 sums checked

The downloaded binary packages are in
	C:\Users\rosgori\AppData\Local\Temp\Rtmp4Wo5WQ\downloaded_packages
── R CMD build ──────────────────────────────────────────────────────────────────────────────────────────────────
βœ”  checking for file 'C:\Users\rosgori\AppData\Local\Temp\Rtmp4Wo5WQ\remotes308817f82f8d\stan-dev-rstan-662ac20\rstan\rstan/DESCRIPTION' (487ms)
─  preparing 'rstan': (4s)
βœ”  checking DESCRIPTION meta-information ... 
─  cleaning src
─  checking for LF line-endings in source and make files and shell scripts (854ms)
─  checking for empty or unneeded directories
─  looking to see if a 'data/datalist' file should be added
─  building 'rstan_2.36.0.9000.tar.gz' (1.5s)
   
Installing package into β€˜C:/Users/rosgori/AppData/Local/R/win-library/4.5’
(as β€˜lib’ is unspecified)
* installing *source* package 'rstan' ...
** this is package 'rstan' version '2.36.0.9000'
** using staged installation
** libs
Error: package 'StanHeaders' 2.32.10 was found, but >= 2.36.0 is required by 'rstan'
* removing 'C:/Users/rosgori/AppData/Local/R/win-library/4.5/rstan'
Aviso:
In i.p(...) :
  installation of package β€˜C:/Users/rosgori/AppData/Local/Temp/Rtmp4Wo5WQ/file3088759845d2/rstan_2.36.0.9000.tar.gz’ had non-zero exit status

The errors means the installer downloads StanHeaders 2.32.10 for RStan 2.36, but it needs StanHeaders >= 2.36.The last version of StanHeaders is 2.32.10 according to this page

I bet, just like me, you cannot use RStan because of those reasons, and unfortunately the installation for RStan is not working at this moment.

The other option is CmdStanR, but you said you were having problems with the installation.

1 Like

Thanks for looking into this!

I get different results than you with R-universe:

Nice install:


> install.packages("rstan", repos = c('https://stan-dev.r-universe.dev', getOption("repos")))
Installing package into β€˜C:/Users/redacted/AppData/Local/R/win-library/4.4’
(as β€˜lib’ is unspecified)
also installing the dependency β€˜StanHeaders’

trying URL 'https://stan-dev.r-universe.dev/bin/windows/contrib/4.4/StanHeaders_2.36.0.9000.zip'
Content type 'application/zip' length 3307320 bytes (3.2 MB)
downloaded 3.2 MB

trying URL 'https://stan-dev.r-universe.dev/bin/windows/contrib/4.4/rstan_2.36.0.9000.zip'
Content type 'application/zip' length 2779589 bytes (2.7 MB)
downloaded 2.7 MB

package β€˜StanHeaders’ successfully unpacked and MD5 sums checked
package β€˜rstan’ successfully unpacked and MD5 sums checked

The downloaded binary packages are in
	C:\Users\redacted\AppData\Local\Temp\RtmpamI3Aj\downloaded_packages
>

As for the installation cannot be verified and the error message is so long than the stan forums here won’t let me post them because my reply would be about 2x or 3x the size limit :(

For the devs, any idea when the install issues others are also encountering with rstan on Github (source) and R-universe will be resolved? I might have to wait until then to figure out this issue more.

Unfortunately, there isn’t any meaningful content in that error message. R seems to be cutting it off, which I don’t know how to ask it not to.

I believe to install RStan from source you also must install StanHeaders from source, though the RStan installation instructions seem to neglect this

Can you try with an extra install_github line:

remotes::install_github("stan-dev/rstan", ref = "develop", subdir = "StanHeaders")
remotes::install_github("stan-dev/rstan", ref = "develop", subdir = "rstan/rstan")

With those two lines, same error:

Compilation ERROR, function(s)/method(s) not created!
Error en compileCode(f, code, language = language, verbose = verbose): 
  c.hpp:22:0:   required from 'double stan::mcmc::dense_e_metric<Model, BaseRNG>::T(stan::mcmc::dense_e_point&) [with Model = model1b2411573bb9__namespace::model1b2411573bb9_; BaseRNG = boost::random::mixmax_engine<17, 36, 0>]'
   22 |     return 0.5 * z.p.transpose() * z.inv_e_metric_ * z.p;
C:/Users/rosgori/AppData/Local/R/win-library/4.5/StanHeaders/include/src/stan/mcmc/hmc/hamiltonians/dense_e_metric.hpp:21:0:   required from here
   21 |   double T(dense_e_point& z) {
C:/Users/rosgori/AppData/Local/R/win-library/4.5/RcppEigen/include/Eigen/src/Core/DenseCoeffsBase.h:654:74: warning: ignoring attributes on template argument 'Eigen::internal::packet_traits<double>::type' {aka '__m128d'} [-Wignored-attributes]
  654 |   return internal::first_aligned<int(unpacket_traits<DefaultPacketType>::alignment),Derived>(m);
      |                                                                          ^~~~~~~~~
make: *** [C:/PROGRA~1/R/R-45~1.0/etc/x64/Makeconf:296: file1b244881390e.o

Do you get any longer output if you set options(warning.length) to something larger (like ~5000)?

The output is very long and there is a limit of characters here, I pasted here: error-rstan - Pastebin.com

Unfortunately I’m still not spotting the root cause in that message. @andrjohns might know how to get a better one to show up