Installing C++ Compiler for Rstan Error

I’ve tried to install Rstan for a few hours, but no success, so far. I’ve closely followed all the instructions in RStan Getting Started · stan-dev/rstan Wiki · GitHub and Configuring C Toolchain for Windows · stan-dev/rstan Wiki · GitHub, but every time when I run example(stan_model, package = “rstan”, run.dontrun = TRUE)
I get the same error:
Compilation ERROR, function(s)/method(s) not created!

Sometimes it stops at line 369 and other times at line 423.

421: SEXP file23507dfd72b5() {
422: return Rcpp::wrap(“anon_model”);
423: }

Compilation ERROR, function(s)/method(s) not created!
Error in compileCode(f, code, language = language, verbose = verbose) :
/bin/sh: line 1: C:/rtools42/usr/mingw_64/bin/g++: No such file or directorymake: *** [C:/PROGRA~1/R/R-42~1.1/etc/x64/Makeconf:260: file23507dfd72b5.o] Error 127

Could anyone help me with this? I’ve tried looking in other topics as well, but no success so far.

This is the latest output that I got when running the example:

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 ‘’ FROM Stan CODE TO C++ CODE NOW.
COMPILING THE C++ CODE FOR MODEL ‘anon_model’ NOW.
OS: x86_64, mingw32; rstan: 2.26.13; Rcpp: 1.0.9; inline: 0.3.19

setting environment variables:
PKG_LIBS = “C:/Users/sterr/AppData/Local/R/win-library/4.2/rstan/lib/x64/libStanServices.a” -L"C:/Users/sterr/AppData/Local/R/win-library/4.2/StanHeaders/libs/x64" -lStanHeaders -L"C:/Users/sterr/AppData/Local/R/win-library/4.2/RcppParallel/lib/x64" -ltbb -LC:/Users/sterr/AppData/Local/R/win-library/4.2/RcppParallel/lib/x64 -ltbb -ltbbmalloc
PKG_CPPFLAGS = -I"C:/Users/sterr/AppData/Local/R/win-library/4.2/Rcpp/include/" -I"C:/Users/sterr/AppData/Local/R/win-library/4.2/RcppEigen/include/" -I"C:/Users/sterr/AppData/Local/R/win-library/4.2/RcppEigen/include/unsupported" -I"C:/Users/sterr/AppData/Local/R/win-library/4.2/BH/include" -I"C:/Users/sterr/AppData/Local/R/win-library/4.2/StanHeaders/include/src/" -I"C:/Users/sterr/AppData/Local/R/win-library/4.2/StanHeaders/include/" -I"C:/Users/sterr/AppData/Local/R/win-library/4.2/RcppParallel/include/" -DRCPP_PARALLEL_USE_TBB=1 -I"C:/Users/sterr/AppData/Local/R/win-library/4.2/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 -DBOOST_NO_AUTO_PTR -include “C:/Users/sterr/AppData/Local/R/win-library/4.2/StanHeaders/include/stan/math/prim/fun/Eigen.hpp” -std=c++1y
Program source :

1 :
2 : // includes from the plugin
3 : // [[Rcpp::plugins(cpp14)]]
4 :
5 :
lines 6-360 excluded

361 :
362 : .method(
363 : “call_sampler”,
364 : &rstan::stan_fit<stan_model, boost::random::ecuyer1988>::call_sampler)
365 : .method(
366 : “param_names”,
367 : &rstan::stan_fit<stan_model, boost::random::ecuyer1988>::param_names)
368 : .method(“param_names_oi”,
369 : &rstan::stan_fit<stan_model,
370 : boost::random::ecuyer1988>::param_names_oi)
371 : .method(“param_fnames_oi”,
372 : &rstan::stan_fit<stan_model,
373 : boost::random::ecuyer1988>::param_fnames_oi)
374 : .method(
375 : “param_dims”,
376 : &rstan::stan_fit<stan_model, boost::random::ecuyer1988>::param_dims)
377 : .method(“param_dims_oi”,
378 : &rstan::stan_fit<stan_model,
379 : boost::random::ecuyer1988>::param_dims_oi)
380 : .method(“update_param_oi”,
381 : &rstan::stan_fit<stan_model,
382 : boost::random::ecuyer1988>::update_param_oi)
383 : .method(“param_oi_tidx”,
384 : &rstan::stan_fit<stan_model,
385 : boost::random::ecuyer1988>::param_oi_tidx)
386 : .method(“grad_log_prob”,
387 : &rstan::stan_fit<stan_model,
388 : boost::random::ecuyer1988>::grad_log_prob)
389 : .method(“log_prob”,
390 : &rstan::stan_fit<stan_model, boost::random::ecuyer1988>::log_prob)
391 : .method(“unconstrain_pars”,
392 : &rstan::stan_fit<stan_model,
393 : boost::random::ecuyer1988>::unconstrain_pars)
394 : .method(“constrain_pars”,
395 : &rstan::stan_fit<stan_model,
396 : boost::random::ecuyer1988>::constrain_pars)
397 : .method(
398 : “num_pars_unconstrained”,
399 : &rstan::stan_fit<stan_model,
400 : boost::random::ecuyer1988>::num_pars_unconstrained)
401 : .method(
402 : “unconstrained_param_names”,
403 : &rstan::stan_fit<
404 : stan_model, boost::random::ecuyer1988>::unconstrained_param_names)
405 : .method(
406 : “constrained_param_names”,
407 : &rstan::stan_fit<stan_model,
408 : boost::random::ecuyer1988>::constrained_param_names)
409 : .method(“standalone_gqs”,
410 : &rstan::stan_fit<stan_model,
411 : boost::random::ecuyer1988>::standalone_gqs);
412 : }
413 :
414 :
415 : // declarations
416 : extern “C” {
417 : SEXP file235016df5c7c( ) ;
418 : }
419 :
420 : // definition
421 : SEXP file235016df5c7c() {
422 : return Rcpp::wrap(“anon_model”);
423 : }
make cmd is
make -f “C:/PROGRA~1/R/R-42~1.1/etc/x64/Makeconf” -f “C:/PROGRA~1/R/R-42~1.1/share/make/winshlib.mk” CXX=‘(CXX14) (CXX14STD)’ CXXFLAGS=‘(CXX14FLAGS)' CXXPICFLAGS='(CXX14PICFLAGS)’ SHLIB_LDFLAGS=‘(SHLIB_CXX14LDFLAGS)' SHLIB_LD='(SHLIB_CXX14LD)’ SHLIB=“file235016df5c7c.dll” WIN=64 TCLBIN= OBJECTS=“file235016df5c7c.o”

make would use
C:/rtools42/usr/mingw_64/bin/g++ -std=gnu++14 -I"C:/PROGRA~1/R/R-42~1.1/include" -DNDEBUG -I"C:/Users/sterr/AppData/Local/R/win-library/4.2/Rcpp/include/" -I"C:/Users/sterr/AppData/Local/R/win-library/4.2/RcppEigen/include/" -I"C:/Users/sterr/AppData/Local/R/win-library/4.2/RcppEigen/include/unsupported" -I"C:/Users/sterr/AppData/Local/R/win-library/4.2/BH/include" -I"C:/Users/sterr/AppData/Local/R/win-library/4.2/StanHeaders/include/src/" -I"C:/Users/sterr/AppData/Local/R/win-library/4.2/StanHeaders/include/" -I"C:/Users/sterr/AppData/Local/R/win-library/4.2/RcppParallel/include/" -DRCPP_PARALLEL_USE_TBB=1 -I"C:/Users/sterr/AppData/Local/R/win-library/4.2/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 -DBOOST_NO_AUTO_PTR -include “C:/Users/sterr/AppData/Local/R/win-library/4.2/StanHeaders/include/stan/math/prim/fun/Eigen.hpp” -std=c++1y -I"C:/rtools42/x86_64-w64-mingw32.static.posix/include" -O2 -Wall -mfpmath=sse -msse2 -mstackrealign -c file235016df5c7c.cpp -o file235016df5c7c.o
if test “zfile235016df5c7c.o” != “z”; then
if test -e “file235016df5c7c-win.def”; then
echo C:/rtools42/usr/mingw_64/bin/g++ -shared -s -static-libgcc -o file235016df5c7c.dll file235016df5c7c-win.def file235016df5c7c.o “C:/Users/sterr/AppData/Local/R/win-library/4.2/rstan/lib/x64/libStanServices.a” -L"C:/Users/sterr/AppData/Local/R/win-library/4.2/StanHeaders/libs/x64" -lStanHeaders -L"C:/Users/sterr/AppData/Local/R/win-library/4.2/RcppParallel/lib/x64" -ltbb -LC:/Users/sterr/AppData/Local/R/win-library/4.2/RcppParallel/lib/x64 -ltbb -ltbbmalloc -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.1/bin/x64" -lR ;
C:/rtools42/usr/mingw_64/bin/g++ -shared -s -static-libgcc -o file235016df5c7c.dll file235016df5c7c-win.def file235016df5c7c.o “C:/Users/sterr/AppData/Local/R/win-library/4.2/rstan/lib/x64/libStanServices.a” -L"C:/Users/sterr/AppData/Local/R/win-library/4.2/StanHeaders/libs/x64" -lStanHeaders -L"C:/Users/sterr/AppData/Local/R/win-library/4.2/RcppParallel/lib/x64" -ltbb -LC:/Users/sterr/AppData/Local/R/win-library/4.2/RcppParallel/lib/x64 -ltbb -ltbbmalloc -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.1/bin/x64" -lR ;
else
echo EXPORTS > tmp.def;
C:/rtools42/usr/mingw_64/bin/nm file235016df5c7c.o | sed -n ‘s/^.* [BCDRT] / /p’ | sed -e ‘/[.]refptr[.]/d’ -e ‘/[.]weak[.]/d’ | sed ‘s/[^ ][^ ]*/“&”/g’ >> tmp.def;
echo C:/rtools42/usr/mingw_64/bin/g++ -shared -s -static-libgcc -o file235016df5c7c.dll tmp.def file235016df5c7c.o “C:/Users/sterr/AppData/Local/R/win-library/4.2/rstan/lib/x64/libStanServices.a” -L"C:/Users/sterr/AppData/Local/R/win-library/4.2/StanHeaders/libs/x64" -lStanHeaders -L"C:/Users/sterr/AppData/Local/R/win-library/4.2/RcppParallel/lib/x64" -ltbb -LC:/Users/sterr/AppData/Local/R/win-library/4.2/RcppParallel/lib/x64 -ltbb -ltbbmalloc -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.1/bin/x64" -lR ;
C:/rtools42/usr/mingw_64/bin/g++ -shared -s -static-libgcc -o file235016df5c7c.dll tmp.def file235016df5c7c.o “C:/Users/sterr/AppData/Local/R/win-library/4.2/rstan/lib/x64/libStanServices.a” -L"C:/Users/sterr/AppData/Local/R/win-library/4.2/StanHeaders/libs/x64" -lStanHeaders -L"C:/Users/sterr/AppData/Local/R/win-library/4.2/RcppParallel/lib/x64" -ltbb -LC:/Users/sterr/AppData/Local/R/win-library/4.2/RcppParallel/lib/x64 -ltbb -ltbbmalloc -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.1/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:
5:

lines 6-360 excluded

360: .constructor<SEXP, SEXP, SEXP>()
361:
362: .method(
363: “call_sampler”,
364: &rstan::stan_fit<stan_model, boost::random::ecuyer1988>::call_sampler)
365: .method(
366: “param_names”,
367: &rstan::stan_fit<stan_model, boost::random::ecuyer1988>::param_names)
368: .method(“param_names_oi”,
369: &rstan::stan_fit<stan_model,
370: boost::random::ecuyer1988>::param_names_oi)
371: .method(“param_fnames_oi”,
372: &rstan::stan_fit<stan_model,
373: boost::random::ecuyer1988>::param_fnames_oi)
374: .method(
375: “param_dims”,
376: &rstan::stan_fit<stan_model, boost::random::ecuyer1988>::param_dims)
377: .method(“param_dims_oi”,
378: &rstan::stan_fit<stan_model,
379: boost::random::ecuyer1988>::param_dims_oi)
380: .method(“update_param_oi”,
381: &rstan::stan_fit<stan_model,
382: boost::random::ecuyer1988>::update_param_oi)
383: .method(“param_oi_tidx”,
384: &rstan::stan_fit<stan_model,
385: boost::random::ecuyer1988>::param_oi_tidx)
386: .method(“grad_log_prob”,
387: &rstan::stan_fit<stan_model,
388: boost::random::ecuyer1988>::grad_log_prob)
389: .method(“log_prob”,
390: &rstan::stan_fit<stan_model, boost::random::ecuyer1988>::log_prob)
391: .method(“unconstrain_pars”,
392: &rstan::stan_fit<stan_model,
393: boost::random::ecuyer1988>::unconstrain_pars)
394: .method(“constrain_pars”,
395: &rstan::stan_fit<stan_model,
396: boost::random::ecuyer1988>::constrain_pars)
397: .method(
398: “num_pars_unconstrained”,
399: &rstan::stan_fit<stan_model,
400: boost::random::ecuyer1988>::num_pars_unconstrained)
401: .method(
402: “unconstrained_param_names”,
403: &rstan::stan_fit<
404: stan_model, boost::random::ecuyer1988>::unconstrained_param_names)
405: .method(
406: “constrained_param_names”,
407: &rstan::stan_fit<stan_model,
408: boost::random::ecuyer1988>::constrained_param_names)
409: .method(“standalone_gqs”,
410: &rstan::stan_fit<stan_model,
411: boost::random::ecuyer1988>::standalone_gqs);
412: }
413:
414:
415: // declarations
416: extern “C” {
417: SEXP file235016df5c7c( ) ;
418: }
419:
420: // definition
421: SEXP file235016df5c7c() {
422: return Rcpp::wrap(“anon_model”);
423: }

Compilation ERROR, function(s)/method(s) not created!
Error in compileCode(f, code, language = language, verbose = verbose) :
/bin/sh: line 1: C:/rtools42/usr/mingw_64/bin/g++: No such file or directorymake: *** [C:/PROGRA~1/R/R-42~1.1/etc/x64/Makeconf:260: file235016df5c7c.o] Error 127

Unless you need the log density or gradients or transforms in the API, I’d suggest moving to CmdStanR. It’s up to date with Stan and much easier to install.

It looks like you’ve previously configured your RStan installation for R3.6, and this is causing issues with finding the compiler under R4.2.

Can you try the instructions in the Getting Started guide for removing this existing configuration: Configuring C Toolchain for Windows · stan-dev/rstan Wiki · GitHub

Thank you for the reply! I’ve tried this, but unfortunately I still get the same error:

0:
361: // declarations
362: extern “C” {
363: SEXP file77828a06a6d( ) ;
364: }
365:
366: // definition
367: SEXP file77828a06a6d() {
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: file77828a06a6d.o:file77828a06a6d.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: file77828a06a6d.o:file77828a06a6d.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: file77828a06a6d.o:file77828a06a6d.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: file77828a06a6d.o:file77828a06a6d.cpp:(.text$ZN3tbb10interface623task

Thank you for the reply, unfortunately I do need those

Next you need to follow the instructions for R4.2 that are listed at the top

Thanks for the reply again. I also did this. I get the same error still, this is the end of the code:

361: // declarations
362: extern "C" {
363: SEXP file36e478961770( ) ;
364: }
365: 
366: // definition
367: SEXP file36e478961770() {
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: file36e478961770.o:file36e478961770.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: file36e478961770.o:file36e478961770.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: file36e478961770.o:file36e478961770.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: file36e478961770.o:file36e478961770.cpp:(.text$_ZN3tbb10interface

This is the beginning of the output:

Restarting R session...

> example(stan_model, package = "rstan", run.dontrun = TRUE)
Loading required package: StanHeaders
Loading required package: ggplot2
rstan (Version 2.21.5, GitRev: 2e1f913d3ca3)
For execution on a local, multicore CPU with excess RAM we recommend calling
options(mc.cores = parallel::detectCores()).
To avoid recompilation of unchanged Stan programs, we recommend calling
rstan_options(auto_write = TRUE)
Do not specify '-march=native' in 'LOCAL_CPPFLAGS' or a Makevars file

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.9; inline: 0.3.19 
 >> setting environment variables: 
LOCAL_LIBS =  "C:/Users/sterr/AppData/Local/R/win-library/4.2/rstan/lib/x64/libStanServices.a" -L"C:/Users/sterr/AppData/Local/R/win-library/4.2/StanHeaders/libs/x64" -lStanHeaders -L"C:/Users/sterr/AppData/Local/R/win-library/4.2/RcppParallel/lib/x64" -ltbb
PKG_CPPFLAGS =   -I"C:/Users/sterr/AppData/Local/R/win-library/4.2/Rcpp/include/"  -I"C:/Users/sterr/AppData/Local/R/win-library/4.2/RcppEigen/include/"  -I"C:/Users/sterr/AppData/Local/R/win-library/4.2/RcppEigen/include/unsupported"  -I"C:/Users/sterr/AppData/Local/R/win-library/4.2/BH/include" -I"C:/Users/sterr/AppData/Local/R/win-library/4.2/StanHeaders/include/src/"  -I"C:/Users/sterr/AppData/Local/R/win-library/4.2/StanHeaders/include/"  -I"C:/Users/sterr/AppData/Local/R/win-library/4.2/RcppParallel/include/"  -I"C:/Users/sterr/AppData/Local/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/sterr/AppData/Local/R/win-library/4.2/StanHeaders/include/stan/math/prim/fun/Eigen.hpp"  -std=c++1y
 >> Program source :

   1 : 
   2 : // includes from the plugin
   3 : // [[Rcpp::plugins(cpp14)]]
   4 : 
   5 : 

This is because you’re using rstan 2.21, the R4.2 instructions specified that you needed to install the preview version of 2.26 using the provided commands

Thank you for the reply, I tried so many things that I was a bit confused! I think it finally works, thank you!!!

1 Like