Stan_model() return compileCode errors

Beginner user of Stan here. I have problems running any stan model. Above is the error code. Any suggestions on how to solve are greatly appreciated!

FYI I’m on R version 3.6

the error message indicates that RStan isn’t finding the C++ compiler for some reason.

If you’re a beginner, I recommend using CmdStanR instead of RStan.

CmdStanR’s function “install_cmdstan” will install its own version of CmdStan and the C++ compiler for you.

Thanks for the suggestion. Unfortunately work calls for RStan. I was able to find the g++ file in the path stated above. Wondering if a path can be added somewhere to let RStan find the files?

That g++ error is a bit of red herring, and not an actual issue. Can you post the full output from the Error in compileCode section? The screenshot is cutting off the rest of the information.

And to get a better idea of how your system is configured, can you post the outputs from the following commands:

readLines("~/.Renviron")
readLines("~/.Rprofile")
readLines("~/.R/Makevars.win")
Sys.getenv("PATH")
Sys.getenv("BINPREF")
> readLines("~/.Renviron")
[1] "PATH=\"C:/RBuildTools/3.5/bin;${PATH}\""
> readLines("~/.Rprofile")
[1] "Sys.setenv(BINPREF = \"C:/RBuildTools/3.5/mingw_$(WIN)/bin/\")"
> readLines("~/.R/Makevars.win")
Error in file(con, "r") : cannot open the connection
In addition: Warning message:
In file(con, "r") :
  cannot open file 'C:/Users/sarah/Documents/.R/Makevars.win': No such file or directory
> Sys.getenv("PATH")
[1] "C:\\RBuildTools\\3.5\\bin;C:\\Program Files\\R\\R-3.6.0\\bin\\x64;C:\\ProgramData\\Anaconda3;C:\\ProgramData\\Anaconda3\\Library\\mingw-w64\\bin;C:\\ProgramData\\Anaconda3\\Library\\usr\\bin;C:\\ProgramData\\Anaconda3\\Library\\bin;C:\\ProgramData\\Anaconda3\\Scripts;C:\\Program Files (x86)\\Common Files\\Intel\\Shared Libraries\\redist\\intel64\\compiler;C:\\Program Files\\AdoptOpenJDK\\jdk-11.0.10.9-hotspot\\bin;C:\\Program Files (x86)\\Common Files\\Oracle\\Java\\javapath;C:\\Windows\\System32;C:\\Windows;C:\\Windows\\System32\\wbem;C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\;C:\\Windows\\System32\\OpenSSH\\;C:\\Program Files (x86)\\PharosSystems\\Core;C:\\Program Files\\Java\\jdk1.8.0_201\\bin;C:\\Program Files\\nodejs\\;C:\\Program Files\\Git\\cmd;C:\\Users\\sarah\\AppData\\Local\\Programs\\Python\\Python37-32\\Scripts\\;C:\\Users\\sarah\\AppData\\Local\\Programs\\Python\\Python37-32\\;C:\\Users\\sarah\\AppData\\Local\\Microsoft\\WindowsApps;C:\\Users\\sarah\\AppData\\Local\\Programs\\Microsoft VS Code\\bin;C:\\Users\\sarah\\AppData\\Roaming\\npm;C:\\Users\\sarah\\AppData\\Roaming\\TinyTeX\\bin\\win32"
> Sys.getenv("BINPREF")
[1] ""

The full error output

Error in compileCode(f, code, language = language, verbose = verbose) : 
                   from C:/Users/sarah/Documents/R/win-library/3.6/StanHeaders/include/src/stan/model/model_header.hpp:4,                 from file2cec210a2e17.cpp:14:C:/Users/sarah/Documents/R/win-library/3.6/StanHeaders/include/stan/math/rev/core/set_zero_all_adjoints.hpp:14:13: warning: 'void stan::math::set_zero_all_adjoints()' defined but not used [-Wunused-function] static void set_zero_all_adjoints() {             ^make: *** [C:/PROGRA~1/R/R-36~1.0/etc/x64/Makeconf:215: file2cec210a2e17.o] Error 1
In addition: Warning message:
In system(paste(CXX, ARGS), ignore.stdout = TRUE, ignore.stderr = TRUE) :
  'C:/RBUILD~1/3.5/mingw_/bin/g++' not found
Error in sink(type = "output") : invalid connection

It looks like everything is configured correctly, so there are two steps to check next. First is to check whether you can successfully compile c++ through the Rcpp package (which is what rstan delegates to), so try running this example code and seeing if it errors:

Rcpp::cppFunction(
    'int fibonacci(const int x) {
        if (x == 0) return(0);
        if (x == 1) return(1);
        return (fibonacci(x - 1)) + fibonacci(x - 2);
    }')

If that works without error, then it may be an issue with the current CRAN rstan, so try installing the preview of the next version and running the example model again:

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")))

Thanks. The first code runs fine.
I ran into some error in the process of re-installing the packages.

Error for StanHeaders

Error in .shlib_internal(args) : 
  C++14 standard requested but CXX14 is not defined
* removing 'C:/Users/sarah/Documents/R/win-library/3.6/StanHeaders'
Warning in install.packages :
  installation of package ‘StanHeaders’ had non-zero exit status

Error for rstan

C:/RBuildTools/3.5/mingw_32/bin/g++ -shared -s -static-libgcc -o Rcpp.dll tmp.def api.o attributes.o barrier.o date.o module.o rcpp_init.o C:/Users/sarah/Documents/R/win-library/3.6/rstan/lib/x64/libStanServices.a -LC:/Users/sarah/Documents/R/win-library/3.6/StanHeaders/libs/x64 -lStanHeaders -LC:/Users/sarah/Documents/R/win-library/3.6/RcppParallel/lib/x64 -ltbb -LC:/PROGRA~1/R/R-36~1.0/bin/i386 -lR
g++.exe: error: C:/Users/sarah/Documents/R/win-library/3.6/rstan/lib/x64/libStanServices.a: No such file or directory
no DLL was created
ERROR: compilation failed for package 'Rcpp'
* removing 'C:/Users/sarah/Documents/R/win-library/3.6/Rcpp'
* restoring previous 'C:/Users/sarah/Documents/R/win-library/3.6/Rcpp'
C:/RBuildTools/3.5/mingw_32/bin/g++ -shared -s -static-libgcc -o V8.dll tmp.def RcppExports.o bindings.o -L../windows/libv8-6.2.414.50-vista/lib-4.9.3/i386 -L../windows/libv8-6.2.414.50-vista/lib/i386 -lv8_base -lv8_libplatform -lv8_nosnapshot -lv8_libbase -lv8_libsampler -lv8_builtins_setup -lv8_builtins_generators -lws2_32 -lwinmm -ldbghelp -lshlwapi C:/Users/sarah/Documents/R/win-library/3.6/rstan/lib/x64/libStanServices.a -LC:/Users/sarah/Documents/R/win-library/3.6/StanHeaders/libs/x64 -lStanHeaders -LC:/Users/sarah/Documents/R/win-library/3.6/RcppParallel/lib/x64 -ltbb -LC:/PROGRA~1/R/R-36~1.0/bin/i386 -lR
g++.exe: error: C:/Users/sarah/Documents/R/win-library/3.6/rstan/lib/x64/libStanServices.a: No such file or directory
no DLL was created
ERROR: compilation failed for package 'V8'
* removing 'C:/Users/sarah/Documents/R/win-library/3.6/V8'
* restoring previous 'C:/Users/sarah/Documents/R/win-library/3.6/V8'
ERROR: compilation failed for package 'RcppEigen'
* removing 'C:/Users/sarah/Documents/R/win-library/3.6/RcppEigen'
* restoring previous 'C:/Users/sarah/Documents/R/win-library/3.6/RcppEigen'
Warning in install.packages :
  installation of package ‘RcppEigen’ had non-zero exit status
* installing *source* package 'StanHeaders' ...
** using staged installation
** libs

*** arch - i386
Error in .shlib_internal(args) : 
  C++14 standard requested but CXX14 is not defined
* removing 'C:/Users/sarah/Documents/R/win-library/3.6/StanHeaders'
Warning in install.packages :
  installation of package ‘StanHeaders’ had non-zero exit status
ERROR: dependency 'StanHeaders' is not available for package 'rstan'
* removing 'C:/Users/sarah/Documents/R/win-library/3.6/rstan'
Warning in install.packages :
  installation of package ‘rstan’ had non-zero exit status

So the first step is to let R know what compiler to use for c++14 code by creating a Makevars.win file:

dir.create("~/.R")
cat("CXX14 = $(BINPREF)g++ -m$(WIN) -std=c++1y" ,
    file = "~/.R/Makevars.win", sep = "\n", append = FALSE)

Then restart your R session and try installing rstan again

Seems like StanHeaders was installed successfully. Some errors for stan installation

C:/RBuildTools/3.5/mingw_32/bin/../lib/gcc/i686-w64-mingw32/4.9.3/../../../../i686-w64-mingw32/bin/as.exe: stan/lang/grammars/term_grammar_inst.o: section .eh_frame$_ZN5boost6spirit2qi11nary_parserINS1_11alternativeINS_6fusion4consINS1_8sequenceINS5_INS1_12literal_charINS0_13char_encoding8standardELb1ELb0EEENS5_INS1_6actionINS1_25parameterized_nonterminalINS1_4ruleINS0_17line_pos_iteratorIN9__gnu_cxx17__normal_iteratorIPKcSsEEEEFN4stan4lang10expressionENSM_5scopeEENSM_18whitespace_grammarISK_EENS0_11unused_typeESS_EENS4_6vectorIINS_7phoenix5actorINS0_9attributeILi1EEEEEEEEEENSW_INS_5proto7exprns_10basic_exprINSV_6detail3tag13function_evalENS12_7argsns_5list5INS14_INS12_6tagns_3tag8terminalENS18_4termINSM_11negate_exprEEELl0EEENSW_INSX_ILi0EEEEENSW_INS0_8argumentILi0EEEEENSW_INS14_IS1C_NS1D_INSV_8argumentILi3EEEEELl0EEEEENSW_INS14_IS1C_NS1D_INS_17reference_wrapperISt18basic_stringstreamIcSt11char_traitsIcESaIcEEEEEELl0EEEEEEELl5EEEEEEENS4_4nil_EEEEEEENS5_INS6_INS5_ISA_NS5_INSB_IS11_NSW_INS14_IS17_NS18_5list4INS14_IS1C_NS1D_INSM_19logical_negate_exprEEELl0EEES1I_S1L_S20_EELl4EEEEEEES25_EEEEEENS5_INS6_INS5_ISA_NS5_INSB_IS11_NSW_INS14_IS17_NS18_5list3INS14_IS1C_NS1D_INSM_10assign_lhsEEELl0EEES1I_S1L_EELl3EEEEEEES25_EEEEEES2S_EEEEEEEEEC2Ev: string table overflow at offset 10000355
C:\Users\sarah\AppData\Local\Temp\ccQD02ap.s: Assembler messages:
C:\Users\sarah\AppData\Local\Temp\ccQD02ap.s: Fatal error: can't close stan/lang/grammars/term_grammar_inst.o: File too big
make: *** [C:/PROGRA~1/R/R-36~1.0/etc/i386/Makeconf:215: stan/lang/grammars/term_grammar_inst.o] Error 1
ERROR: compilation failed for package 'rstan'
* removing 'C:/Users/sarah/Documents/R/win-library/3.6/rstan'
Warning in install.packages :
  installation of package ‘rstan’ had non-zero exit status

Thank you so much for your help so far!

Oh wow, that is the first time I’ve seen that particular error! Let’s try adding a few c++ optimisation flags so that we’re requesting the smallest and most optimised version of the compiled code.

Can I get you to run:

cat("\n CXX11FLAGS += -mtune=native -O3 -mmmx -msse -msse2 -msse3 -mssse3 -msse4.1 -msse4.2",
    file = "~/.R/Makevars.win", sep = "\n", append = TRUE)
cat("\n CXX14FLAGS += -mtune=native -O3 -mmmx -msse -msse2 -msse3 -mssse3 -msse4.1 -msse4.2",
    file = "~/.R/Makevars.win", sep = "\n", append = TRUE)
> cat("\n CXX11FLAGS += -mtune=native -O3 -mmmx -msse -msse2 -msse3 -mssse3 -msse4.1 -msse4.2",
+     file = "~/.R/Makevars.win", sep = "\n", append = TRUE)
> cat("\n CXX14FLAGS += -mtune=native -O3 -mmmx -msse -msse2 -msse3 -mssse3 -msse4.1 -msse4.2",
+     file = "~/.R/Makevars.win", sep = "\n", append = TRUE)

Doesn’t seem to output anything

Yep, those commands just update the Makevars.win file, which R checks for additional arguments to provide when compiling source code. Now that it’s been updated to request optimised code, you can try to install StanHeaders and rstan again

I had the same problem but it is still not resolved.



I encountered this error after deleting all R-related files and reinstalling R, Rstudio, and Rstan (before that Rstan was functioning properly).

Please see the instructions in the RStan Getting Started guide

Thanks. I’m having a new problem:


I’ve tried some methods but none of them have worked, do you have any suggestions?

Can you download the most recent rstan and StanHeaders binaries from here: r-packages/bin/windows/contrib/4.2 at gh-pages ¡ stan-dev/r-packages ¡ GitHub

Then install them through the rstudio menu?

1 Like

Wow, thank you so much for your help! my program is working fine now.

1 Like