Error in compileCode

What am I doing wrong here?
I have spent a lot of time going through old GitHub issues but haven’t really managed to get around this issue.


# setup
set.seed(123)
library(brms)
#> Loading required package: Rcpp
#> Loading required package: ggplot2
#> Registered S3 method overwritten by 'xts':
#>   method     from
#>   as.zoo.xts zoo
#> Loading 'brms' package (version 2.7.0). Useful instructions
#> can be found by typing help('brms'). A more detailed introduction
#> to the package is available through vignette('brms_overview').
#> Run theme_set(theme_default()) to use the default bayesplot theme.

library(rstan)
#> Loading required package: StanHeaders
#> rstan (Version 2.18.2, 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)
#> For improved execution time, we recommend calling
#> Sys.setenv(LOCAL_CPPFLAGS = '-march=native')
#> although this causes Stan to throw an error on a few processors.

# check compilers
pkgbuild::has_rtools()
#> [1] TRUE

system('where make')
#> C:\Rtools\bin\make.exe

system('g++ -v')
#> Using built-in specs.
#> COLLECT_GCC=C:\Rtools\mingw_32\bin\G__~1.EXE
#> COLLECT_LTO_WRAPPER=C:/Rtools/mingw_32/bin/../libexec/gcc/i686-w64-mingw32/4.9.3/lto-wrapper.exe
#> Target: i686-w64-mingw32
#> Configured with: ../../../src/gcc-4.9.3/configure --host=i686-w64-mingw32 --build=i686-w64-mingw32 --target=i686-w64-mingw32 --prefix=/mingw32 --with-sysroot=/home/Jeroen/mingw-gcc-4.9.3/i686-493-posix-dwarf-rt_v3-s/mingw32 --with-gxx-include-dir=/mingw32/i686-w64-mingw32/include/c++ --enable-static --disable-shared --disable-multilib --enable-languages=c,c++,fortran,lto --enable-libstdcxx-time=yes --enable-threads=posix --enable-libgomp --enable-libatomic --enable-lto --enable-graphite --enable-checking=release --enable-fully-dynamic-string --enable-version-specific-runtime-libs --disable-sjlj-exceptions --with-dwarf2 --disable-isl-version-check --disable-cloog-version-check --disable-libstdcxx-pch --disable-libstdcxx-debug --enable-bootstrap --disable-rpath --disable-win32-registry --disable-nls --disable-werror --disable-symvers --with-gnu-as --with-gnu-ld --with-arch=i686 --with-tune=generic --with-libiconv --with-system-zlib --with-gmp=/home/Jeroen/mingw-gcc-4.9.3/prerequisites/i686-w64-mingw32-static --with-mpfr=/home/Jeroen/mingw-gcc-4.9.3/prerequisites/i686-w64-mingw32-static --with-mpc=/home/Jeroen/mingw-gcc-4.9.3/prerequisites/i686-w64-mingw32-static --with-isl=/home/Jeroen/mingw-gcc-4.9.3/prerequisites/i686-w64-mingw32-static --with-cloog=/home/Jeroen/mingw-gcc-4.9.3/prerequisites/i686-w64-mingw32-static --enable-cloog-backend=isl --with-pkgversion='i686-posix-dwarf, Built by MinGW-W64 project' --with-bugurl=http://sourceforge.net/projects/mingw-w64 CFLAGS='-O2 -pipe -I/home/Jeroen/mingw-gcc-4.9.3/i686-493-posix-dwarf-rt_v3-s/mingw32/opt/include -I/home/Jeroen/mingw-gcc-4.9.3/prerequisites/i686-zlib-static/include -I/home/Jeroen/mingw-gcc-4.9.3/prerequisites/i686-w64-mingw32-static/include' CXXFLAGS='-O2 -pipe -I/home/Jeroen/mingw-gcc-4.9.3/i686-493-posix-dwarf-rt_v3-s/mingw32/opt/include -I/home/Jeroen/mingw-gcc-4.9.3/prerequisites/i686-zlib-static/include -I/home/Jeroen/mingw-gcc-4.9.3/prerequisites/i686-w64-mingw32-static/include' CPPFLAGS= LDFLAGS='-pipe -L/home/Jeroen/mingw-gcc-4.9.3/i686-493-posix-dwarf-rt_v3-s/mingw32/opt/lib -L/home/Jeroen/mingw-gcc-4.9.3/prerequisites/i686-zlib-static/lib -L/home/Jeroen/mingw-gcc-4.9.3/prerequisites/i686-w64-mingw32-static/lib -Wl,--large-address-aware'
#> Thread model: posix
#> gcc version 4.9.3 (i686-posix-dwarf, Built by MinGW-W64 project) 

# check toolchain
fx <-
  inline::cxxfunction(
    signature(x = "integer", y = "numeric") ,
    '
    
    return ScalarReal( INTEGER(x)[0] * REAL(y)[0] ) ;'
  )
fx(2L, 5) # should be 10
#> [1] 10

# running model brms model
brms_crossedRE <-
  brms::brm(
    formula = mpg ~ wt + (1 | cyl) + (1 + wt | gear),
    data = mtcars,
    iter = 100,
    chains = 2
  )
#> Compiling the C++ model
#> Error in compileCode(f, code, language = language, verbose = verbose): Compilation ERROR, function(s)/method(s) not created! In file included from C:/Users/inp099/Documents/R/win-library/3.6/BH/include/boost/random/detail/integer_log2.hpp:19:0,
#>                  from C:/Users/inp099/Documents/R/win-library/3.6/BH/include/boost/random/detail/int_float_pair.hpp:26,
#>                  from C:/Users/inp099/Documents/R/win-library/3.6/BH/include/boost/random/exponential_distribution.hpp:27,
#>                  from C:/Users/inp099/Documents/R/win-library/3.6/BH/include/boost/random/gamma_distribution.hpp:25,
#>                  from C:/Users/inp099/Documents/R/win-library/3.6/StanHeaders/include/stan/math/prim/mat/prob/dirichlet_rng.hpp:5,
#>                  from C:/Users/inp099/Documents/R/win-library/3.6/StanHeaders/include/stan/math/prim/mat.hpp:276,
#>                  from C:/Users/inp099/Documents/R/win-library/3.6/StanHeaders/include/stan/math/rev/mat.hpp:12,
#>                  from C:/Users/inp099/Documents/R/win-library/3.6/StanHeaders/include/stan/math.hpp:4,
#>                  from C:/Users/inp099/Documents/R/win-library/3.6/StanHeaders/include/src/stan/model/model_header.hpp:4,
#>                  from file29b4531a2e9b.cpp:8:
#> C:/Users/inp099/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>");
#>                                                                                          ^
#> C:/Users/inp099/Documents/R/win-library/3.6/StanHeaders/libs/x64/StanHeaders.dll: file not recognized: File format not recognized
#> collect2.exe: error: ld returned 1 exit status
#> Error in sink(type = "output"): invalid connection

Here is the traceback-

traceback()
21.
stop(paste("Compilation ERROR, function(s)/method(s) not created!", 
    paste(errmsg, collapse = "\n"))) 
20.
compileCode(f, code, language = language, verbose = verbose) 
19.
cxxfunction(sig = sig, body = body, plugin = plugin, includes = includes, 
    settings = settings, ..., verbose = verbose) 
18.
force(code) 
17.
withr::with_path(rtools_path(), code) 
16.
pkgbuild::with_build_tools(cxxfunction(sig = sig, body = body, 
    plugin = plugin, includes = includes, settings = settings, 
    ..., verbose = verbose), required = rstan_options("required") && 
    !identical(Sys.getenv("WINDOWS"), "TRUE") && !identical(Sys.getenv("R_PACKAGE_SOURCE"),  ... 
15.
cxxfunctionplus(signature(), body = paste(" return Rcpp::wrap(\"", 
    model_name, "\");", sep = ""), includes = inc, plugin = "rstan", 
    save_dso = save_dso | auto_write, module_name = paste("stan_fit4", 
        model_cppname, "_mod", sep = ""), verbose = verbose) 
14.
.fun(model_code = .x1) at <text>#1
13.
eval(expr, envir, ...) 
12.
eval(expr, envir, ...) 
11.
eval2(call, envir = args, enclos = parent.frame()) 
10.
run(rstan::stan_model, stan_model_args) 
9.
eval(expr, envir) 
8.
eval(expr, envir) 
7.
eval(expr, pf) 
6.
eval(expr, pf) 
5.
withVisible(eval(expr, pf)) 
4.
evalVis(expr) 
3.
utils::capture.output(out <- eval(expr, envir), type = type, 
    ...) 
2.
eval_silent(run(rstan::stan_model, stan_model_args), silent = silence_stan_model, 
    type = "message") 
1.
brms::brm(formula = mpg ~ wt + (1 | cyl) + (1 + wt | gear), data = mtcars, 
    iter = 500, chains = 2) 

Created on 2019-01-08 by the reprex package (v0.2.1)

Session info
devtools::session_info()
#> - Session info ----------------------------------------------------------
#>  setting  value                                             
#>  version  R Under development (unstable) (2018-11-30 r75724)
#>  os       Windows 10 x64                                    
#>  system   x86_64, mingw32                                   
#>  ui       RTerm                                             
#>  language (EN)                                              
#>  collate  English_United States.1252                        
#>  ctype    English_United States.1252                        
#>  tz       Asia/Calcutta                                     
#>  date     2019-01-08                                        
#> 
#> - Packages --------------------------------------------------------------
#>  package        * version     date       lib
#>  abind            1.4-5       2016-07-21 [1]
#>  assertthat       0.2.0       2017-04-11 [1]
#>  backports        1.1.3       2018-12-14 [1]
#>  base64enc        0.1-3       2015-07-28 [1]
#>  bayesplot        1.6.0       2018-08-02 [1]
#>  bridgesampling   0.6-0       2018-10-21 [1]
#>  brms           * 2.7.0       2018-12-17 [1]
#>  Brobdingnag      1.2-6       2018-08-13 [1]
#>  callr            3.1.1       2018-12-21 [1]
#>  cli              1.0.1.9000  2018-10-30 [1]
#>  coda             0.19-2      2018-10-08 [1]
#>  colorspace       1.3-2       2016-12-14 [1]
#>  colourpicker     1.0         2017-09-27 [1]
#>  crayon           1.3.4       2017-09-16 [1]
#>  crosstalk        1.0.0       2016-12-21 [1]
#>  desc             1.2.0       2018-10-30 [1]
#>  devtools         2.0.1       2018-10-26 [1]
#>  digest           0.6.18      2018-10-10 [1]
#>  dplyr            0.8.0       2019-01-06 [1]
#>  DT               0.5         2018-11-05 [1]
#>  dygraphs         1.1.1.6     2018-07-11 [1]
#>  evaluate         0.12        2018-10-09 [1]
#>  fs               1.2.6       2018-08-23 [1]
#>  ggplot2        * 3.1.0.9000  2018-12-15 [1]
#>  ggridges         0.5.1       2018-09-27 [1]
#>  glue             1.3.0       2018-07-17 [1]
#>  gridExtra        2.3         2017-09-09 [1]
#>  gtable           0.2.0       2016-02-26 [1]
#>  gtools           3.8.1       2018-06-26 [1]
#>  highr            0.7         2018-06-09 [1]
#>  htmltools        0.3.6       2017-04-28 [1]
#>  htmlwidgets      1.3         2018-09-30 [1]
#>  httpuv           1.4.5.1     2018-12-18 [1]
#>  igraph           1.2.2       2018-07-27 [1]
#>  inline           0.3.15      2018-05-18 [1]
#>  knitr            1.21        2018-12-10 [1]
#>  later            0.7.5       2018-09-18 [1]
#>  lattice          0.20-38     2018-11-04 [2]
#>  lazyeval         0.2.1       2017-10-29 [1]
#>  loo              2.0.0       2018-04-11 [1]
#>  magrittr         1.5         2014-11-22 [1]
#>  markdown         0.9         2018-12-07 [1]
#>  Matrix           1.2-15      2018-11-01 [1]
#>  matrixStats      0.54.0      2018-07-23 [1]
#>  memoise          1.1.0       2017-04-21 [1]
#>  mime             0.6         2018-10-05 [1]
#>  miniUI           0.1.1.1     2018-05-18 [1]
#>  munsell          0.5.0       2018-06-12 [1]
#>  mvtnorm          1.0-8       2018-05-31 [1]
#>  nlme             3.1-137     2018-04-07 [2]
#>  pillar           1.3.1       2018-12-15 [1]
#>  pkgbuild         1.0.2       2018-10-16 [1]
#>  pkgconfig        2.0.2       2018-08-16 [1]
#>  pkgload          1.0.2       2018-10-29 [1]
#>  plyr             1.8.4       2016-06-08 [1]
#>  prettyunits      1.0.2       2015-07-13 [1]
#>  processx         3.2.1       2018-12-05 [1]
#>  promises         1.0.1       2018-04-13 [1]
#>  ps               1.3.0       2018-12-21 [1]
#>  purrr            0.2.99.9000 2019-01-04 [1]
#>  R6               2.3.0       2018-10-04 [1]
#>  Rcpp           * 1.0.0       2018-11-07 [1]
#>  remotes          2.0.2       2018-10-30 [1]
#>  reshape2         1.4.3       2017-12-11 [1]
#>  rlang            0.3.0.9002  2019-01-04 [1]
#>  rmarkdown        1.11        2018-12-08 [1]
#>  rprojroot        1.3-2       2018-01-03 [1]
#>  rsconnect        0.8.12      2018-12-05 [1]
#>  rstan          * 2.18.2      2018-11-07 [1]
#>  rstantools       1.5.1       2018-08-22 [1]
#>  scales           1.0.0       2018-08-09 [1]
#>  sessioninfo      1.1.1       2018-11-05 [1]
#>  shiny            1.2.0       2018-11-02 [1]
#>  shinyjs          1.0         2018-01-08 [1]
#>  shinystan        2.5.0       2018-05-01 [1]
#>  shinythemes      1.1.2       2018-11-06 [1]
#>  StanHeaders    * 2.18.0-1    2018-12-13 [1]
#>  stringi          1.2.4       2018-07-20 [1]
#>  stringr          1.3.1       2018-05-10 [1]
#>  testthat         2.0.1       2018-10-13 [1]
#>  threejs          0.3.1       2017-08-13 [1]
#>  tibble           2.0.0       2019-01-04 [1]
#>  tidyselect       0.2.5       2018-10-11 [1]
#>  usethis          1.4.0.9000  2018-12-12 [1]
#>  withr            2.1.2       2018-03-15 [1]
#>  xfun             0.4         2018-10-23 [1]
#>  xtable           1.8-3       2018-08-29 [1]
#>  xts              0.11-2      2018-11-05 [1]
#>  yaml             2.2.0       2018-07-25 [1]
#>  zoo              1.8-4       2018-09-19 [1]
#>  source                            
#>  CRAN (R 3.5.0)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.5.0)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.6.0)                    
#>  Github (r-lib/cli@56538e3)        
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.5.1)                    
#>  Github (r-lib/desc@7c12d36)       
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.5.1)                    
#>  Github (tidyverse/dplyr@59d4600)  
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.5.1)                    
#>  Github (tidyverse/ggplot2@9292832)
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.5.0)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.5.0)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.6.0)                    
#>  Github (tidyverse/purrr@3b4a013)  
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.5.1)                    
#>  Github (r-lib/rlang@67ff440)      
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.5.1)                    
#>  Github (tidyverse/tibble@d06dd5c) 
#>  CRAN (R 3.5.1)                    
#>  Github (r-lib/usethis@923dd75)    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.5.2)                    
#> 
#> [1] C:/Users/inp099/Documents/R/win-library/3.6
#> [2] C:/Program Files/R/R-devel/library

Try just doing

library(rstan)
m <- stan_model(model_code = 'parameters {real y;} model {y ~ normal(0,1);}', 
                verbose = TRUE)

to see what the real error is.

Thanks.

The output is too long so I can’t post it in this thread, so I have posted the reprex as a GitHub gist here:

At line 413,

C:/Users/inp099/Documents/R/win-library/3.6/StanHeaders/libs/x64/StanHeaders.dll: file not recognized: File format not recognized
collect2.exe: error: ld returned 1 exit status

It looks as if your StanHeaders was compiled with different bitness or other compiler settings. You should also check that you don’t have any -flto or -ffat-lto-objects in your ~/.R/Makevars.win file.

Sorry for the late reply.

I solved this based on this thread:

The problem was that Rtools had installed both 32-bit and 64-bit versions by default. I reinstalled it just using 64-bit version and it worked!

I’ve encountered the same error after updating to R 3.6.0, and after reading both this and the thread you’ve linked to, I still have no idea what to actually do.

Would someone be so kind as to post step-by-step-for-dummies instructions?

Much appreciated!

Do

install.packages("https://win-builder.r-project.org/7J2OgVTa98P3/StanHeaders_2.19.1.zip"
                 repos = NULL)
install.packages("https://win-builder.r-project.org/8mtFzkp9SfmY/rstan_2.18.9.zip",
                 repos = NULL)
1 Like

Thank you Ben!