Compilation Error (Please Help!)

Summary:

Cannot compile rstan.

Description:

I tried the 8schools example of rstan: [https://github.com/stan-dev/rstan/wiki/RStan-Getting-Started] and ran into problems.

Markvars.win File

CXX14=$(BINPREF)g++
CXX14FLAGS=-O3 -march=native -mtune=native
CXX11FLAGS=-O3 -march=native -mtune=native

Stan File:

// saved as 8schools.stan
data {
int<lower=0> J; // number of schools
real y[J]; // estimated treatment effects
real<lower=0> sigma[J]; // standard error of effect estimates
}
parameters {
real mu; // population treatment effect
real<lower=0> tau; // standard deviation in treatment effects
vector[J] eta; // unscaled deviation from mu by school
}
transformed parameters {
vector[J] theta = mu + tau * eta; // school treatment effects
}
model {
target += normal_lpdf(eta | 0, 1); // prior log-density
target += normal_lpdf(y | theta, sigma); // log-likelihood
}

Reproducible Steps:

library(rstan)
options(mc.cores = parallel::detectCores())
rstan_options(auto_write = TRUE)
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 <- stan(file = ‘8schools.stan’, data = schools_dat)

Current Output:

Error in compileCode(f, code, language = language, verbose = verbose) :
Compilation ERROR, function(s)/method(s) not created! In file included from C:/Users/swang/R/win-library/3.6/BH/include/boost/random/detail/integer_log2.hpp:19:0,
from C:/Users/swang/R/win-library/3.6/BH/include/boost/random/detail/int_float_pair.hpp:26,
from C:/Users/swang/R/win-library/3.6/BH/include/boost/random/exponential_distribution.hpp:27,
from C:/Users/swang/R/win-library/3.6/BH/include/boost/random/gamma_distribution.hpp:25,
from C:/Users/swang/R/win-library/3.6/StanHeaders/include/stan/math/prim/mat/prob/dirichlet_rng.hpp:5,
from C:/Users/swang/R/win-library/3.6/StanHeaders/include/stan/math/prim/mat.hpp:276,
from C:/Users/swang/R/win-library/3.6/StanHeaders/include/stan/math/rev/mat.hpp:12,
from C:/Users/swang/R/win-library/3.6/StanHeaders/include/stan/math.hpp:4,
from C:/Users/swang/R/win-library/3.6/StanHeaders/include/src/stan/
In addition: Warning message:
In system(cmd, intern = !verbose) :
running command ‘C:/PROGRA~1/R/R-36~1.0/bin/x64/R CMD SHLIB file4a3473ca4c69.cpp 2> file4a3473ca4c69.cpp.err.txt’ had status 1
Error in sink(type = “output”) : invalid connection

Expected Output:

If applicable, the output you expected from RStan.

Environment:

sessionInfo()
R version 3.6.0 (2019-04-26)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 17134)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C LC_TIME=English_United States.1252

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

other attached packages:
[1] rstan_2.18.2 StanHeaders_2.18.1 ggplot2_3.1.1

loaded via a namespace (and not attached):
[1] Rcpp_1.0.1 pillar_1.4.1 compiler_3.6.0 plyr_1.8.4 prettyunits_1.0.2 tools_3.6.0 pkgbuild_1.0.3
[8] tibble_2.1.2 gtable_0.3.0 pkgconfig_2.0.2 rlang_0.3.4 cli_1.1.0 rstudioapi_0.10 parallel_3.6.0
[15] loo_2.1.0 gridExtra_2.3 withr_2.1.2 dplyr_0.8.1 stats4_3.6.0 grid_3.6.0 tidyselect_0.2.5
[22] glue_1.3.1 inline_0.3.15 R6_2.4.0 processx_3.3.1 callr_3.2.0 purrr_0.3.2 magrittr_1.5
[29] scales_1.0.0 ps_1.3.0 matrixStats_0.54.0 assertthat_0.2.1 colorspace_1.4-1 lazyeval_0.2.2 munsell_0.5.0
[36] crayon_1.3.4

Checking the C++ Toolchain

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

Try the test from the wiki

fx <- inline::cxxfunction( signature(x = “integer”, y = “numeric” ) , ’
return ScalarReal( INTEGER(x)[0] * REAL(y)[0] ) ;’ )

make: *** [C:/PROGRA~1/R/R-36~1.0/share/make/winshlib.mk:13: file4a3425103b7b.dll] Error 127

ERROR(s) during compilation: source code errors or compiler configuration errors!

Program source:
1:
2: // includes from the plugin
3: #include <R.h>
4: #include <Rdefines.h>
5: #include <R_ext/Error.h>
6:
7:
8: // user includes
9:
10:
11: // declarations
12: extern “C” {
13: SEXP file4a3425103b7b( SEXP x, SEXP y) ;
14: }
15:
16: // definition
17:
18: SEXP file4a3425103b7b( SEXP x, SEXP y ){
19:
20: return ScalarReal( INTEGER(x)[0] * REAL(y)[0] ) ;
21: Rf_warning(“your C++ program does not return anything”);
22: return R_NilValue ;
23: }
24:
25:
Error in compileCode(f, code, language = language, verbose = verbose) :
Compilation ERROR, function(s)/method(s) not created! make: *** [C:/PROGRA~1/R/R-36~1.0/share/make/winshlib.mk:13: file4a3425103b7b.dll] Error 127
In addition: Warning message:
In system(cmd, intern = !verbose) :
running command ‘C:/PROGRA~1/R/R-36~1.0/bin/x64/R CMD SHLIB file4a3425103b7b.cpp 2> file4a3425103b7b.cpp.err.txt’ had status 1

It could not find make. What does Sys.getenv('BINPREF') return?

It is an empty string. Then I create a .Rprofile file with only one line in it as in the following:

Sys.setenv(BINPREF = “C:/Rtools/mingw_$(WIN)/bin/”)

Now Sys.getenv('BINPREF') returns the exact string I write: "C:/Rtools/mingw_$(WIN)/bin/". How can I let R to translate $(WIN)?

R should do that automatically but it is empty except on Windows.

After this fix, now when I run fit <- stan(file = ‘8schools.stan’, data = schools_dat). RStudio asks me to install additional build tools. After clicking yes, RStudio goes ahead to download and install Rtools again. Why does it happen? I have already get it installed. Furthermore, after the reinstallation of Rtools, RStudio asks me to install Rtools another time. How to solve this problem?

After this fix, the same error still exists. How to solve it?

fx <- inline::cxxfunction( signature(x = “integer”, y = “numeric” ) , ’

  •                        return ScalarReal( INTEGER(x)[0] * REAL(y)[0] ) ;' )
    

make: *** [C:/PROGRA~1/R/R-36~1.0/share/make/winshlib.mk:13: file552c5d1d4c4f.dll] Error 127

ERROR(s) during compilation: source code errors or compiler configuration errors!

Program source:
1:
2: // includes from the plugin
3: #include <R.h>
4: #include <Rdefines.h>
5: #include <R_ext/Error.h>
6:
7:
8: // user includes
9:
10:
11: // declarations
12: extern “C” {
13: SEXP file552c5d1d4c4f( SEXP x, SEXP y) ;
14: }
15:
16: // definition
17:
18: SEXP file552c5d1d4c4f( SEXP x, SEXP y ){
19:
20: return ScalarReal( INTEGER(x)[0] * REAL(y)[0] ) ;
21: Rf_warning(“your C++ program does not return anything”);
22: return R_NilValue ;
23: }
24:
25:
Error in compileCode(f, code, language = language, verbose = verbose) :
Compilation ERROR, function(s)/method(s) not created! make: *** [C:/PROGRA~1/R/R-36~1.0/share/make/winshlib.mk:13: file552c5d1d4c4f.dll] Error 127
In addition: Warning message:
In system(cmd, intern = !verbose) :
running command ‘C:/PROGRA~1/R/R-36~1.0/bin/x64/R CMD SHLIB file552c5d1d4c4f.cpp 2> file552c5d1d4c4f.cpp.err.txt’ had status 1

You can try first doing

options(buildtools.check = NULL)

Now I get the following error message:

fit <- stan(file = ‘8schools.stan’, data = schools_dat)

Error in compileCode(f, code, language = language, verbose = verbose) :
Compilation ERROR, function(s)/method(s) not created! In file included from C:/Users/swang/R/win-library/3.6/BH/include/boost/random/detail/integer_log2.hpp:19:0,
from C:/Users/swang/R/win-library/3.6/BH/include/boost/random/detail/int_float_pair.hpp:26,
from C:/Users/swang/R/win-library/3.6/BH/include/boost/random/exponential_distribution.hpp:27,
from C:/Users/swang/R/win-library/3.6/BH/include/boost/random/gamma_distribution.hpp:25,
from C:/Users/swang/R/win-library/3.6/StanHeaders/include/stan/math/prim/mat/prob/dirichlet_rng.hpp:5,
from C:/Users/swang/R/win-library/3.6/StanHeaders/include/stan/math/prim/mat.hpp:276,
from C:/Users/swang/R/win-library/3.6/StanHeaders/include/stan/math/rev/mat.hpp:12,
from C:/Users/swang/R/win-library/3.6/StanHeaders/include/stan/math.hpp:4,
from C:/Users/swang/R/win-library/3.6/StanHeaders/include/src/stan/
In addition: Warning message:
In system(cmd, intern = !verbose) :
running command ‘C:/PROGRA~1/R/R-36~1.0/bin/x64/R CMD SHLIB file61cc246763b7.cpp 2> file61cc246763b7.cpp.err.txt’ had status 1
Error in sink(type = “output”) : invalid connection

We need that call with the verbose = TRUE argument to see the underlying error message.

@bgoodri I am getting this error too and the fix others have suggested does not help. I ran it with verbose=TRUE and this is the last part of the compilation that I saw (the rest is stuff from the program source).

Compilation argument:
C:/PROGRA~1/R/R-36~1.1/bin/x64/R CMD SHLIB filea81c61942c20.cpp 2> filea81c61942c20.cpp.err.txt
C:/Rtools/mingw_64/bin/g++ -std=gnu++11 -I"C:/PROGRA~1/R/R-36~1.1/include" -DNDEBUG -I"C:/Users/jhall/Documents/R/win-library/3.6/Rcpp/include/" -I"C:/Users/jhall/Documents/R/win-library/3.6/RcppEigen/include/" -I"C:/Users/jhall/Documents/R/win-library/3.6/RcppEigen/include/unsupported" -I"C:/Users/jhall/Documents/R/win-library/3.6/BH/include" -I"C:/Users/jhall/Documents/R/win-library/3.6/StanHeaders/include/src/" -I"C:/Users/jhall/Documents/R/win-library/3.6/StanHeaders/include/" -I"C:/Users/jhall/Documents/R/win-library/3.6/rstan/include" -DEIGEN_NO_DEBUG -DBOOST_DISABLE_ASSERTS -std=c++1y -march=core2 -O3 -march=native -mtune=native -c filea81c61942c20.cpp -o filea81c61942c20.o
In file included from C:/Users/jhall/Documents/R/win-library/3.6/BH/include/boost/random/detail/integer_log2.hpp:19:0,
from C:/Users/jhall/Documents/R/win-library/3.6/BH/include/boost/random/detail/int_float_pair.hpp:26,
from C:/Users/jhall/Documents/R/win-library/3.6/BH/include/boost/random/exponential_distribution.hpp:27,
from C:/Users/jhall/Documents/R/win-library/3.6/BH/include/boost/random/gamma_distribution.hpp:25,
from C:/Users/jhall/Documents/R/win-library/3.6/StanHeaders/include/stan/math/prim/mat/prob/dirichlet_rng.hpp:5,
from C:/Users/jhall/Documents/R/win-library/3.6/StanHeaders/include/stan/math/prim/mat.hpp:292,
from C:/Users/jhall/Documents/R/win-library/3.6/StanHeaders/include/stan/math/rev/mat.hpp:12,
from C:/Users/jhall/Documents/R/win-library/3.6/StanHeaders/include/stan/math.hpp:4,
from C:/Users/jhall/Documents/R/win-library/3.6/StanHeaders/include/src/stan/model/model_header.hpp:4,
from filea81c61942c20.cpp:8:
C:/Users/jhall/Documents/R/win-library/3.6/BH/include/boost/pending/integer_log2.hpp:7:89: note: #pragma message: This header is deprecated. Use <boost/integer/integer_log2.hpp> instead.
BOOST_HEADER_DEPRECATED("<boost/integer/integer_log2.hpp>");
^

cc1plus.exe: out of memory allocating 65536 bytes
make: *** [C:/PROGRA~1/R/R-36~1.1/etc/x64/Makeconf:215: filea81c61942c20.o] Error 1

ERROR(s) during compilation: source code errors or compiler configuration errors!

You ran out of RAM.

Ah, thanks. I closed some other applications and that helped. It’s kind of crazy though, on just the 8schools example I saw that it was using 2gb of RAM in the Windows Task Manager at one point (leaving on the optimizations and using the previous version of stan).

The error about being out of memory wasn’t the only one I received while playing around with this. One thing that I noticed is that when there is an error, it prints out all that source code for some reason. I don’t really know what that happens, and I imagine in some cases there is a use for it. Regardless, it can print out so much that Rstudio is cutting off what was above it. So I can’t see what happened before that. If there were a way to optionally prevent the source code from being printed, then it might be a little easier to follow.

The dump of the source code comes from the inline package, so there’s not much we can do about it if we also want to see what the compiler error is.