Error in file(con, "r") : cannot open the connection + 'C:/rtools40/usr/mingw_/bin/g++' not found

Hi everyone,

I’m trying to install Stan on Windows 10, R-4.0.2, Rtools 4.0. I’ve followed the installation instructions but when I compile a model I’m receiving theses errors:

Compilation argument:
 C:/PROGRA~1/R/R-40~1.2/bin/x64/R CMD SHLIB fileba061084f37.cpp 2> fileba061084f37.cpp.err.txt 
C:/Rtools/mingw_64/bin/g++  -std=gnu++14 -I"C:/PROGRA~1/R/R-40~1.2/include" -DNDEBUG   -I"C:/Program Files/R/R-4.0.2/library/Rcpp/include/"  -I"C:/Program Files/R/R-4.0.2/library/RcppEigen/include/"  -I"C:/Program Files/R/R-4.0.2/library/RcppEigen/include/unsupported"  -I"C:/Program Files/R/R-4.0.2/library/BH/include" -I"C:/Program Files/R/R-4.0.2/library/StanHeaders/include/src/"  -I"C:/Program Files/R/R-4.0.2/library/StanHeaders/include/"  -I"C:/Program Files/R/R-4.0.2/library/RcppParallel/include/"  -I"C:/Program Files/R/R-4.0.2/library/rstan/include" -DEIGEN_NO_DEBUG  -DBOOST_DISABLE_ASSERTS  -DBOOST_PENDING_INTEGER_LOG2_HPP  -DSTAN_THREADS  -DBOOST_NO_AUTO_PTR  -include "C:/Program Files/R/R-4.0.2/library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp"  -std=c++1y       -include C:/Program Files/R/R-4.0.2/library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp -I "C:/Program Files/R/R-4.0.2/library/StanHeaders/include" -I "C:/Program Files/R/R-4.0.2/library/RcppEigen/include"   -O2 -Wall  -mfpmath=sse -msse2 -mstackrealign -c fileba061084f37.cpp -o fileba061084f37.o
sh: C:/Rtools/mingw_64/bin/g++: No such file or directory
make: *** [C:/PROGRA~1/R/R-40~1.2/etc/x64/Makeconf:229: fileba061084f37.o] Error 127
Error in file(con, "r") : cannot open the connection
In addition: Warning messages:
1: In system(paste(CXX, ARGS), ignore.stdout = TRUE, ignore.stderr = TRUE) :
  'C:/rtools40/usr/mingw_/bin/g++' not found
2: In file(con, "r") :
  cannot open file 'fileba061084f37.cpp.err.txt': No such file or directory

I’ve read for hours and hours all the issues and potential solutions about it and nothing fixed it. I’ve reinstalled Rtools many times, reinstalled rstan many times, no way to fix the errors. I’ve also set the following paths to my environment but without success:

PATH="${RTOOLS40_HOME}\usr\bin;${PATH}"
BINPREF="C:/Rtools/mingw_$(WIN)/bin/"

Further, I’ve fixed set the makevars.win as follows:

CXX14FLAGS=-O3 -mtune=native
CXX11FLAGS=-O3 -mtune=native

Finally when I run Sys.getenv(“PATH”), I get:

[1] "C:\\rtools40\\usr\\bin;C:\\Program Files\\R\\R-4.0.2\\bin\\x64;C:\\Windows\\System32;C:\\Windows;C:\\Windows\\System32\\wbem;C:\\Windows\\System32\\WindowsPowerShell\\v1.0;C:\\Windows\\System32\\OpenSSH;C:\\Program Files\\Git\\cmd;C:\\Program Files\\Intel\\WiFi\\bin;C:\\Program Files\\Common Files\\Intel\\WirelessCommon;C:\\Program Files\\qpdf-10.0.1\\bin;C:\\Program Files\\nodejs;C:\\Program Files (x86)\\Yarn\\bin;C:\\ProgramData\\chocolatey\\bin;C:\\Users\\Administrateur\\AppData\\Local\\Microsoft\\WindowsApps;C:\\Program Files\\JetBrains\\PyCharm 2019.3.4\\bin;C:\\Users\\Administrateur\\AppData\\Roaming\\TinyTeX\\bin\\win32;C:\\rtools40\\mingw64\\bin"

When I run version I get:

           _                           
platform       x86_64-w64-mingw32          
arch           x86_64                      
os             mingw32                     
system         x86_64, mingw32             
status                                     
major          4                           
minor          0.2                         
year           2020                        
month          06                          
day            22                          
svn rev        78730                       
language       R                           
version.string R version 4.0.2 (2020-06-22)
nickname       Taking Off Again       

> sessionInfo()
R version 4.0.2 (2020-06-22)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)

Matrix products: default

locale:
[1] LC_COLLATE=French_France.1252 
[2] LC_CTYPE=French_France.1252   
[3] LC_MONETARY=French_France.1252
[4] LC_NUMERIC=C                  
[5] LC_TIME=French_France.1252    

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

other attached packages:
[1] rstan_2.21.2         ggplot2_3.3.2       
[3] StanHeaders_2.21.0-5

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.5         compiler_4.0.2     pillar_1.4.6      
 [4] prettyunits_1.1.1  tools_4.0.2        pkgbuild_1.1.0    
 [7] jsonlite_1.7.0     lifecycle_0.2.0    tibble_3.0.2      
[10] gtable_0.3.0       pkgconfig_2.0.3    rlang_0.4.7       
[13] cli_2.0.2          rstudioapi_0.11    curl_4.3          
[16] yaml_2.2.1         parallel_4.0.2     loo_2.3.0         
[19] gridExtra_2.3      dplyr_1.0.0        withr_2.2.0       
[22] generics_0.0.2     vctrs_0.3.1        tidyselect_1.1.0  
[25] stats4_4.0.2       grid_4.0.2         glue_1.4.1        
[28] inline_0.3.15      R6_2.4.1           processx_3.4.3    
[31] fansi_0.4.1        purrr_0.3.4        callr_3.4.3       
[34] magrittr_1.5       codetools_0.2-16   matrixStats_0.56.0
[37] scales_1.1.1       ps_1.3.3           ellipsis_0.3.1    
[40] assertthat_0.2.1   colorspace_1.4-1   V8_3.2.0          
[43] RcppParallel_5.0.2 munsell_0.5.0      crayon_1.3.4 

Thank you in advance for your help :)

1 Like

Welcome to the forums! The issue Is happening because you’ve set the BINPREF variable. Both because you no longer need to set that with RTools4, and because that directory doesn’t exist with RTools4.

Hope that helps!

Hi Andrew, Thank you very much for your help. Unfortunately I’m getting the same error even though I’ve configured rstan as stated in the Github page. I’ve also removed the BINPREF variable.

This is my .Renviron file:

PATH="${RTOOLS40_HOME}\usr\bin;${PATH}"
LOCAL_CPPFLAGS='-march=corei7'

This is my Makevars.win file

CXX14FLAGS=-O3 -mtune=native
CXX11FLAGS=-O3 -march=corei7 -mtune=corei7

And this is the error I’m getting when I run the schools example:

Error in file(con, "r") : cannot open the connection
In addition: Warning messages:
1: In system(paste(CXX, ARGS), ignore.stdout = TRUE, ignore.stderr = TRUE) :
  'C:/rtools40/usr/mingw_/bin/g++' not found
2: In system(cmd, intern = !verbose) :
  running command 'C:/PROGRA~1/R/R-40~1.2/bin/x64/R CMD SHLIB file2f387e1a3ff9.cpp 2> file2f387e1a3ff9.cpp.err.txt' had status 1
3: In file(con, "r") :
  cannot open file 'file2f387e1a3ff9.cpp.err.txt': No such file or directory
Error in sink(type = "output") : invalid connection

Note: I’ve removed and reinstalled Rtools, I’ve also removed and reinstalled rstan as suggested.

I really can’t see the problem.

Best.

[UPDATE] after running:

schools_dat <- list(J = 8, 
                     y = c(28,  8, -3,  7, -1,  1, 18, 12),
                     sigma = c(15, 10, 16, 11,  9, 11, 10, 18))
fit <- rstan::stan(file = 'schools.stan', data = "schools_dat")

I’m receiving this error:

Error in data_list2array(x) : 
  the dimensions for all elements (array) of the list are not same
In addition: Warning message:
In system(paste(CXX, ARGS), ignore.stdout = TRUE, ignore.stderr = TRUE) :
  'C:/RBUILD~1/4.0/usr/mingw_/bin/g++' not found
failed to preprocess the data; sampling not done

:’(

This particular error is because in your code you have

but "schools_dat" is just string. You want to pass schools_dat (no quotes) the object instead.

There might be other errors but fixing that will get rid of this particular error and then either it will run or we’ll see different errors and we’ll help you fix those.

@jonah Yes indeed, thank you very much. Still I have the g++ error which I wasn’t able to fix.

Is there a g++ in that location

Isn’t the correct location

C:/RBUILD~1/4.0/mingw64/bin/g++

Hi @ahartikainen, thanks the location of g++ is within :
C:\RBuildTools\4.0\mingw64\bin\g++ , I assume it’s same path as
C:/RBUILD~1/4.0/mingw64/bin/g++, so yes.

The warning:

In addition: Warning message:
In system(paste(CXX, ARGS), ignore.stdout = TRUE, ignore.stderr = TRUE) :
  'C:/RBUILD~1/4.0/usr/mingw_/bin/g++' not found

Is safe to ignore. Are there any other errors aside from that or does the code run and just give that error at the end?

Hi @andrjohn,

Is this output OK, so that I can ignore the ‘g++’ error ?

SAMPLING FOR MODEL 'schools' NOW (CHAIN 1).

Chain 1:
Chain 1: Gradient evaluation took 0 seconds
Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 0 seconds.
Chain 1: Adjust your expectations accordingly!
Chain 1:
Chain 1:
Chain 1: Iteration: 1 / 2000 [ 0%] (Warmup)
Chain 1: Iteration: 200 / 2000 [ 10%] (Warmup)
Chain 1: Iteration: 400 / 2000 [ 20%] (Warmup)
Chain 1: Iteration: 600 / 2000 [ 30%] (Warmup)
Chain 1: Iteration: 800 / 2000 [ 40%] (Warmup)
Chain 1: Iteration: 1000 / 2000 [ 50%] (Warmup)
Chain 1: Iteration: 1001 / 2000 [ 50%] (Sampling)
Chain 1: Iteration: 1200 / 2000 [ 60%] (Sampling)
Chain 1: Iteration: 1400 / 2000 [ 70%] (Sampling)
Chain 1: Iteration: 1600 / 2000 [ 80%] (Sampling)
Chain 1: Iteration: 1800 / 2000 [ 90%] (Sampling)
Chain 1: Iteration: 2000 / 2000 [100%] (Sampling)
Chain 1:
Chain 1: Elapsed Time: 0.062 seconds (Warm-up)
Chain 1: 0.074 seconds (Sampling)
Chain 1: 0.136 seconds (Total)
Chain 1:

SAMPLING FOR MODEL ‘schools’ NOW (CHAIN 2).
Chain 2:
Chain 2: Gradient evaluation took 0 seconds
Chain 2: 1000 transitions using 10 leapfrog steps per transition would take 0 seconds.
Chain 2: Adjust your expectations accordingly!
Chain 2:
Chain 2:
Chain 2: Iteration: 1 / 2000 [ 0%] (Warmup)
Chain 2: Iteration: 200 / 2000 [ 10%] (Warmup)
Chain 2: Iteration: 400 / 2000 [ 20%] (Warmup)
Chain 2: Iteration: 600 / 2000 [ 30%] (Warmup)
Chain 2: Iteration: 800 / 2000 [ 40%] (Warmup)
Chain 2: Iteration: 1000 / 2000 [ 50%] (Warmup)
Chain 2: Iteration: 1001 / 2000 [ 50%] (Sampling)
Chain 2: Iteration: 1200 / 2000 [ 60%] (Sampling)
Chain 2: Iteration: 1400 / 2000 [ 70%] (Sampling)
Chain 2: Iteration: 1600 / 2000 [ 80%] (Sampling)
Chain 2: Iteration: 1800 / 2000 [ 90%] (Sampling)
Chain 2: Iteration: 2000 / 2000 [100%] (Sampling)
Chain 2:
Chain 2: Elapsed Time: 0.063 seconds (Warm-up)
Chain 2: 0.061 seconds (Sampling)
Chain 2: 0.124 seconds (Total)
Chain 2:

SAMPLING FOR MODEL ‘schools’ NOW (CHAIN 3).
Chain 3:
Chain 3: Gradient evaluation took 0 seconds
Chain 3: 1000 transitions using 10 leapfrog steps per transition would take 0 seconds.
Chain 3: Adjust your expectations accordingly!
Chain 3:
Chain 3:
Chain 3: Iteration: 1 / 2000 [ 0%] (Warmup)
Chain 3: Iteration: 200 / 2000 [ 10%] (Warmup)
Chain 3: Iteration: 400 / 2000 [ 20%] (Warmup)
Chain 3: Iteration: 600 / 2000 [ 30%] (Warmup)
Chain 3: Iteration: 800 / 2000 [ 40%] (Warmup)
Chain 3: Iteration: 1000 / 2000 [ 50%] (Warmup)
Chain 3: Iteration: 1001 / 2000 [ 50%] (Sampling)
Chain 3: Iteration: 1200 / 2000 [ 60%] (Sampling)
Chain 3: Iteration: 1400 / 2000 [ 70%] (Sampling)
Chain 3: Iteration: 1600 / 2000 [ 80%] (Sampling)
Chain 3: Iteration: 1800 / 2000 [ 90%] (Sampling)
Chain 3: Iteration: 2000 / 2000 [100%] (Sampling)
Chain 3:
Chain 3: Elapsed Time: 0.069 seconds (Warm-up)
Chain 3: 0.078 seconds (Sampling)
Chain 3: 0.147 seconds (Total)
Chain 3:

SAMPLING FOR MODEL ‘schools’ NOW (CHAIN 4).
Chain 4:
Chain 4: Gradient evaluation took 0 seconds
Chain 4: 1000 transitions using 10 leapfrog steps per transition would take 0 seconds.
Chain 4: Adjust your expectations accordingly!
Chain 4:
Chain 4:
Chain 4: Iteration: 1 / 2000 [ 0%] (Warmup)
Chain 4: Iteration: 200 / 2000 [ 10%] (Warmup)
Chain 4: Iteration: 400 / 2000 [ 20%] (Warmup)
Chain 4: Iteration: 600 / 2000 [ 30%] (Warmup)
Chain 4: Iteration: 800 / 2000 [ 40%] (Warmup)
Chain 4: Iteration: 1000 / 2000 [ 50%] (Warmup)
Chain 4: Iteration: 1001 / 2000 [ 50%] (Sampling)
Chain 4: Iteration: 1200 / 2000 [ 60%] (Sampling)
Chain 4: Iteration: 1400 / 2000 [ 70%] (Sampling)
Chain 4: Iteration: 1600 / 2000 [ 80%] (Sampling)
Chain 4: Iteration: 1800 / 2000 [ 90%] (Sampling)
Chain 4: Iteration: 2000 / 2000 [100%] (Sampling)
Chain 4:
Chain 4: Elapsed Time: 0.068 seconds (Warm-up)
Chain 4: 0.057 seconds (Sampling)
Chain 4: 0.125 seconds (Total)
Chain 4:
Warning messages:
1: In readLines(file, warn = TRUE) :
incomplete final line found on ‘C:\Users\Administrateur\Desktop\rstan\schools.stan’
2: In system(paste(CXX, ARGS), ignore.stdout = TRUE, ignore.stderr = TRUE) :
‘C:/RBUILD~1/4.0/usr/mingw_/bin/g++’ not found

Yep, that means your install is working. The first warning:

1: In readLines(file, warn = TRUE) :
incomplete final line found on ‘C:\Users\Administrateur\Desktop\rstan\schools.stan’

Just means that your stan model should have an extra blank line at the very end.

The second g++ warning will be fixed in the next rstan version.

Sorry that the installation wasn’t completely painless, but it’s something that we’re working on. Welcome to Stan!

2 Likes

Ah great ! Thank you so much for your help guys :) I’m more than excited to dig into Stan now ! thanks again :)

4 Likes