Running rstan with latest R (3.6.0) fails with Error in compileCode

I’ve just clean installed the latest R version, released today on Windows 10 Pro.
Details below.

fit ← stan(file = ‘blocker.stan’, data = blocker_dat)
Error in compileCode(f, code, language = language, verbose = verbose) :
Compilation ERROR, function(s)/method(s) not created! Error in .shlib_internal(commandArgs(TRUE)) :
C++14 standard requested but CXX14 is not defined
Calls: → .shlib_internal
Execution halted
In addition: Warning message:
In system(cmd, intern = !verbose) :
running command ‘C:/PROGRA~1/R/R-3.6.0/bin/x64/R CMD SHLIB file86011b24b3e.cpp 2> file86011b24b3e.cpp.err.txt’ had status 1
Error in sink(type = “output”) : invalid connection
I’ve installed rstan from CRAN following the instructions at:
RStan Getting Started · stan-dev/rstan Wiki · GitHub

The C++ toolchain test succeeds:

pkgbuild::has_build_tools(debug = TRUE)
[1] TRUE

R version 3.6.0 (2019-04-26)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 17763)
RStudio version 1.2.1511 (RStudio preview version)

  • RStan Version: 2.18.2

  • Output of writeLines(readLines(file.path(Sys.getenv("HOME"), ".R/Makevars"))) → no such file
    However,
    “.R/Makevars.win” contains:
    CXX14FLAGS=-O3 -march=native -mtune=native
    CXX11FLAGS=-O3 -march=native -mtune=native

  • Output of devtools::session_info("rstan")

devtools::session_info(“rstan”)

  • Session info --------------------------------------------------------------------------------------------------------------------------
    setting value
    version R version 3.6.0 (2019-04-26)
    os Windows 10 x64
    system x86_64, mingw32
    ui RStudio
    language (EN)
    collate English_United States.1252
    ctype English_United States.1252
    tz America/New_York
    date 2019-04-26

  • Packages ------------------------------------------------------------------------------------------------------------------------------
    package * version date lib source
    assertthat 0.2.1 2019-03-21 [2] CRAN (R 3.6.0)
    backports 1.1.4 2019-04-10 [2] CRAN (R 3.6.0)
    BH 1.69.0-1 2019-01-07 [2] CRAN (R 3.6.0)
    callr 3.2.0 2019-03-15 [2] CRAN (R 3.6.0)
    checkmate 1.9.1 2019-01-15 [1] CRAN (R 3.6.0)
    cli 1.1.0 2019-03-19 [2] CRAN (R 3.6.0)
    colorspace 1.4-1 2019-03-18 [2] CRAN (R 3.6.0)
    crayon 1.3.4 2017-09-16 [2] CRAN (R 3.6.0)
    desc 1.2.0 2018-05-01 [1] CRAN (R 3.6.0)
    digest 0.6.18 2018-10-10 [2] CRAN (R 3.6.0)
    fansi 0.4.0 2018-10-05 [2] CRAN (R 3.6.0)
    ggplot2 * 3.1.1 2019-04-07 [2] CRAN (R 3.6.0)
    glue 1.3.1 2019-03-12 [2] CRAN (R 3.6.0)
    gridExtra 2.3 2017-09-09 [1] CRAN (R 3.6.0)
    gtable 0.3.0 2019-03-25 [2] CRAN (R 3.6.0)
    inline 0.3.15 2018-05-18 [1] CRAN (R 3.6.0)
    labeling 0.3 2014-08-23 [2] CRAN (R 3.6.0)
    lattice 0.20-38 2018-11-04 [2] CRAN (R 3.6.0)
    lazyeval 0.2.2 2019-03-15 [2] CRAN (R 3.6.0)
    loo 2.1.0 2019-03-13 [1] CRAN (R 3.6.0)
    magrittr 1.5 2014-11-22 [2] CRAN (R 3.6.0)
    MASS 7.3-51.4 2019-03-31 [2] CRAN (R 3.6.0)
    Matrix 1.2-17 2019-03-22 [2] CRAN (R 3.6.0)
    matrixStats 0.54.0 2018-07-23 [1] CRAN (R 3.6.0)
    mgcv 1.8-28 2019-03-21 [2] CRAN (R 3.6.0)
    munsell 0.5.0 2018-06-12 [2] CRAN (R 3.6.0)
    nlme 3.1-139 2019-04-09 [2] CRAN (R 3.6.0)
    pillar 1.3.1 2018-12-15 [2] CRAN (R 3.6.0)
    pkgbuild 1.0.3 2019-03-20 [1] CRAN (R 3.6.0)
    pkgconfig 2.0.2 2018-08-16 [2] CRAN (R 3.6.0)
    plyr 1.8.4 2016-06-08 [2] CRAN (R 3.6.0)
    prettyunits 1.0.2 2015-07-13 [2] CRAN (R 3.6.0)
    processx 3.3.0 2019-03-10 [2] CRAN (R 3.6.0)
    ps 1.3.0 2018-12-21 [2] CRAN (R 3.6.0)
    R6 2.4.0 2019-02-14 [2] CRAN (R 3.6.0)
    RColorBrewer 1.1-2 2014-12-07 [2] CRAN (R 3.6.0)
    Rcpp 1.0.1 2019-03-17 [2] CRAN (R 3.6.0)
    RcppEigen 0.3.3.5.0 2018-11-24 [1] CRAN (R 3.6.0)
    reshape2 1.4.3 2017-12-11 [2] CRAN (R 3.6.0)
    rlang 0.3.4 2019-04-07 [2] CRAN (R 3.6.0)
    rprojroot 1.3-2 2018-01-03 [1] CRAN (R 3.6.0)
    rstan * 2.18.2 2018-11-07 [1] CRAN (R 3.6.0)
    scales 1.0.0 2018-08-09 [2] CRAN (R 3.6.0)
    StanHeaders * 2.18.1 2019-01-28 [1] CRAN (R 3.6.0)
    stringi 1.4.3 2019-03-12 [2] CRAN (R 3.6.0)
    stringr 1.4.0 2019-02-10 [2] CRAN (R 3.6.0)
    tibble 2.1.1 2019-03-16 [2] CRAN (R 3.6.0)
    utf8 1.1.4 2018-05-24 [2] CRAN (R 3.6.0)
    viridisLite 0.3.0 2018-02-01 [2] CRAN (R 3.6.0)
    withr 2.1.2 2018-03-15 [2] CRAN (R 3.6.0)

[1] C:/Users/dsteele/Documents/R/win-library/3.6
[2] C:/Program Files/R/R-3.6.0/library

If you are reporting a bug (thank you!) please use the issue tracker (Issues · stan-dev/rstan · GitHub) instead of the forums.

The existing rstan on CRAN had some logic about the version being less than 3.6 in an attempt to be future proof. However, the future (now present) ended up not including the Rtools40 toolchain, and thus it is temporarily not working unless you have

CXX14 = g++

in your configuration file, which you can do by following

1 Like

I followed the recommended directions and updated ‘Makevars.win’

Makevars.win now contains:

CXX14FLAGS=-O3 -march=native

CXX14 = g++ -m$(WIN) -std=c++1y

CXX11FLAGS=-O3 -march=native

After a restart, I still get the same errror

fit ← stan(file = ‘blocker.stan’, data = blocker_dat)

Error in compileCode(f, code, language = language, verbose = verbose) :

Compilation ERROR, function(s)/method(s) not created! sh: g++: command not found

make: *** [C:/PROGRA~1/R/R-3.6.0/etc/x64/Makeconf:215: file7ec3e8cf16.o] Error 127

In addition: Warning message:

In system(cmd, intern = !verbose) :

running command ‘C:/PROGRA~1/R/R-3.6.0/bin/x64/R CMD SHLIB file7ec3e8cf16.cpp 2> file7ec3e8cf16.cpp.err.txt’ had status 1

Error in sink(type = “output”) : invalid connection

Does

pkgbuild::has_build_tools(debug = TRUE)

work?

The following works.

pkgbuild::has_build_tools(debug = TRUE)

[1] TRUE

You may need to put a full path to g++ in the CXX14 line.

Thanks - Rtools v3.5 is installed by default to. C:\Rtools\

Unclear (to me) what path I should try adding.

What does

Sys.which("g++")

say?

Sys.which(“g++”)

g++

“c:\Rtools\mingw_32\bin\G__~1.EXE”

Are you using a 32bit machine? Or 32bit R on a 64bit machine?

I’m using a 64 bit machine.

devtools::session_info()

  • Session info -------------------------------------------------------------------------------------------
    setting value
    version R version 3.6.0 (2019-04-26)
    os Windows 10 x64
    system x86_64, mingw32
    ui RStudio

64-bit R on a 64bit machine …

sessionInfo()

R version 3.6.0 (2019-04-26)

Platform: x86_64-w64-mingw32/x64 (64-bit)

Running under: Windows 10 x64 (build 17134)

OK. I would next try making

CXX14 = "C:\Rtools\mingw_64\bin\g++"

or maybe

CXX14 = "C:\Rtools\mingw_64\bin\g++.exe"

I added the second option:

"CXX14 = “C:\Rtools\mingw_64\bin\g++.exe”

to .R\Makevars.R which currently reads as:

CXX14FLAGS=-O3 -Wno-unused-variable -Wno-unused-function

CXX14 = (BINPREF)g++ -m(WIN) -std=c++1y

CXX11FLAGS=-O3 -Wno-unused-variable -Wno-unused-function

CXX14FLAGS=-O3 -march=native

CXX14 = “C:\Rtools\mingw_64\bin\g++.exe”

CXX11FLAGS=-O3 -march=native

Now runs without error on the blocker example.

To a non-expert user, these compiler options in Makevars.R are mysterious. Thanks!

Unfortunately, I continue to (intermittently) get the previously reported error (when running more than one core):

fit ← stan(file = ‘blocker.stan’, data = blocker_dat)

Error in unserialize(socklist[[n]]) : error reading from connection

Error in serialize(data, node$con, xdr = FALSE) :

error writing to connection

Do you get the error when using multiple cores for anything besides that blocker example?

Will investigate

I had the same error on a Mac machine. The only solution that worked for me was to downgrade R to 3.5.3

@Dale_Steele, did you have any luck? I’m in the same boat as you right now. I have Windows 10, R 3.6.0, Rtools installed as RBuildTools, g++ being found in the PATH… and same error in compileCode as in your first message.

This may have been solved on GitHub: https://github.com/stan-dev/rstan/issues/633

Foogee fixed it by:
“changing my Makevars.win file to the below
CXX14=$(BINPREF)g++ -O2 -march=native -mtune=native”

Thanks – I am on to a new error message now (C:/RBUILD~1/3.5/mingw_64/bin/g++: Permission denied), I’ll start a new thread if I can’t figure it out.