Error in compileCode(f, code, language = language, verbose = verbose) : Compilation ERROR, function(s)/method(s) not created!

StanHeaders is apparently reverted on CRAN to 2.19.x but they still have 2.21.x binaries. So, I would start a clean R session and do

install.packages("https://cran.r-project.org/src/contrib/Archive/StanHeaders/StanHeaders_2.19.0.tar.gz",
                 repos = NULL)

and then things should work again.

Thank you very much for your prompt response. I re-installed StanHeaders using the command you provided and the version is now 2.19.0. I no longer get the previous errors about missing files, which is great. However, compilation still fails, this time with the following error message. My installed version of RStan is at 2.19.2.

Again, any help will be much appreciated. Thanks.

Error in compileCode(f, code, language = language, verbose = verbose) : 
  Compilation ERROR, function(s)/method(s) not created! file26c46c0d4248.cpp:66:40: error: expected template-name before '<' token
   : public stan::model::model_base_crtp<model26c4290627dd_lm> {
                                        ^
file26c46c0d4248.cpp:66:40: error: expected '{' before '<' token
file26c46c0d4248.cpp:66:40: error: expected unqualified-id before '<' token
In file included from C:/Program Files/R/R-3.6.2/library/BH/include/boost/winapi/time.hpp:13:0,
                 from C:/Program Files/R/R-3.6.2/library/BH/include/boost/date_time/microsec_time_clock.hpp:24,
                 from C:/Program Files/R/R-3.6.2/library/BH/include/boost/date_time/posix_time/posix_time_types.hpp:11,
                 from C:/Program Files/R/R-3.6.2/library/rstan/include/rstan/stan_fit.hpp:13,
                 from C:/Program Files/R/R-3.6.2/library/rstan/include/rstan/rstaninc.hpp:3,
                 from file26c46c0d4248.cpp:631:
C:/Program Files/R/R-3.6.2/library/BH/include/boos
In addition: Warning message:
In system(cmd, intern = !verbose) :
  running command 'C:/PROGRA~1/R/R-36~1.2/bin/x64/R CMD SHLIB file26c46c0d4248.cpp 2> file26c46c0d4248.cpp.err.txt' had status 1
Error in sink(type = "output") : invalid connection

Sadly that doesn’t seem to be the case. I get the same error. I now have StanHeaders at 2.19.0 and my makevars.win is as follows:

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

Is that as it should be?

You and @jhaeberl are also going to have to reinstall rstan, possibly from source.

Deleted Rstan and tried to install from source but unfortunately that failed with many errors. Here are the first few lines of output from the build process

* installing *source* package 'rstan' ...
** package 'rstan' successfully unpacked and MD5 sums checked
** using staged installation
** libs


"C:\Rtools\mingw_64\bin\g++.exe"  -I"C:/PROGRA~1/R/R-36~1.2/include" -DNDEBUG -I"../inst/include" -I"." -I"C:/Program Files/R/R-3.6.2/library/StanHeaders/include/src" -DBOOST_DISABLE_ASSERTS -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION -I"C:/Program Files/R/R-3.6.2/library/Rcpp/include" -I"C:/Program Files/R/R-3.6.2/library/RcppEigen/include" -I"C:/Program Files/R/R-3.6.2/library/BH/include" -I"C:/Program Files/R/R-3.6.2/library/StanHeaders/include"        -O3 -march=native -mtune=native -c sparse_extractors.cpp -o sparse_extractors.o


"C:\Rtools\mingw_64\bin\g++.exe"  -I"C:/PROGRA~1/R/R-36~1.2/include" -DNDEBUG -I"../inst/include" -I"." -I"C:/Program Files/R/R-3.6.2/library/StanHeaders/include/src" -DBOOST_DISABLE_ASSERTS -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION -I"C:/Program Files/R/R-3.6.2/library/Rcpp/include" -I"C:/Program Files/R/R-3.6.2/library/RcppEigen/include" -I"C:/Program Files/R/R-3.6.2/library/BH/include" -I"C:/Program Files/R/R-3.6.2/library/StanHeaders/include"        -O3 -march=native -mtune=native -c misc.cpp -o misc.o


"C:\Rtools\mingw_64\bin\g++.exe"  -I"C:/PROGRA~1/R/R-36~1.2/include" -DNDEBUG -I"../inst/include" -I"." -I"C:/Program Files/R/R-3.6.2/library/StanHeaders/include/src" -DBOOST_DISABLE_ASSERTS -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION -I"C:/Program Files/R/R-3.6.2/library/Rcpp/include" -I"C:/Program Files/R/R-3.6.2/library/RcppEigen/include" -I"C:/Program Files/R/R-3.6.2/library/BH/include" -I"C:/Program Files/R/R-3.6.2/library/StanHeaders/include"        -O3 -march=native -mtune=native -c stanc.cpp -o stanc.o


"C:\Rtools\mingw_64\bin\g++.exe"  -I"C:/PROGRA~1/R/R-36~1.2/include" -DNDEBUG -I"../inst/include" -I"." -I"C:/Program Files/R/R-3.6.2/library/StanHeaders/include/src" -DBOOST_DISABLE_ASSERTS -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION -I"C:/Program Files/R/R-3.6.2/library/Rcpp/include" -I"C:/Program Files/R/R-3.6.2/library/RcppEigen/include" -I"C:/Program Files/R/R-3.6.2/library/BH/include" -I"C:/Program Files/R/R-3.6.2/library/StanHeaders/include"        -O3 -march=native -mtune=native -c init.cpp -o init.o


"C:\Rtools\mingw_64\bin\g++.exe"  -I"C:/PROGRA~1/R/R-36~1.2/include" -DNDEBUG -I"../inst/include" -I"." -I"C:/Program Files/R/R-3.6.2/library/StanHeaders/include/src" -DBOOST_DISABLE_ASSERTS -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION -I"C:/Program Files/R/R-3.6.2/library/Rcpp/include" -I"C:/Program Files/R/R-3.6.2/library/RcppEigen/include" -I"C:/Program Files/R/R-3.6.2/library/BH/include" -I"C:/Program Files/R/R-3.6.2/library/StanHeaders/include"        -O3 -march=native -mtune=native -c pointer-tools.cpp -o pointer-tools.o


"C:\Rtools\mingw_64\bin\g++.exe"  -I"C:/PROGRA~1/R/R-36~1.2/include" -DNDEBUG -I"../inst/include" -I"." -I"C:/Program Files/R/R-3.6.2/library/StanHeaders/include/src" -DBOOST_DISABLE_ASSERTS -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION -I"C:/Program Files/R/R-3.6.2/library/Rcpp/include" -I"C:/Program Files/R/R-3.6.2/library/RcppEigen/include" -I"C:/Program Files/R/R-3.6.2/library/BH/include" -I"C:/Program Files/R/R-3.6.2/library/StanHeaders/include"        -O3 -march=native -mtune=native -c chains.cpp -o chains.o


In file included from C:/Program Files/R/R-3.6.2/library/BH/include/boost/spirit/include/phoenix_limits.hpp:11:0,
                 from C:/Program Files/R/R-3.6.2/library/BH/include/boost/spirit/home/support/meta_compiler.hpp:16,
                 from C:/Program Files/R/R-3.6.2/library/BH/include/boost/spirit/home/qi/meta_compiler.hpp:14,
                 from C:/Program Files/R/R-3.6.2/library/BH/include/boost/spirit/home/qi/action/action.hpp:14,
                 from C:/Program Files/R/R-3.6.2/library/BH/include/boost/spirit/home/qi/action.hpp:14,
                 from C:/Program Files/R/R-3.6.2/library/BH/include/boost/spirit/home/qi.hpp:14,
                 from C:/Program Files/R/R-3.6.2/library/BH/include/boost/spirit/include/qi.hpp:16,
                 from ./stan/lang/grammars/whitespace_grammar.hpp:4,
                 from ./stan/lang/grammars/program_grammar.hpp:6,
                 from C:/Program Files/R/R-3.6.2/library/StanHeaders/include/src/stan/lang/parser.hpp:5,
                 from C:/Program Files/R/R-3.6.2/library/StanHeaders/include/src/stan/lang/compiler.hpp:7,
                 from stanc.cpp:22:
C:/Program Files/R/R-3.6.2/library/BH/include/boost/phoenix/core/limits.hpp:44:0: warning: "BOOST_PHOENIX_NO_VARIADIC_EXPRESSION" redefined
 # define BOOST_PHOENIX_NO_VARIADIC_EXPRESSION
 ^
<command-line>:0:0: note: this is the location of the previous definition
"C:\Rtools\mingw_64\bin\g++.exe"  -I"C:/PROGRA~1/R/R-36~1.2/include" -DNDEBUG -I"../inst/include" -I"." -I"C:/Program Files/R/R-3.6.2/library/StanHeaders/include/src" -DBOOST_DISABLE_ASSERTS -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION -I"C:/Program Files/R/R-3.6.2/library/Rcpp/include" -I"C:/Program Files/R/R-3.6.2/library/RcppEigen/include" -I"C:/Program Files/R/R-3.6.2/library/BH/include" -I"C:/Program Files/R/R-3.6.2/library/StanHeaders/include"        -O3 -march=native -mtune=native -c stan/lang/ast_def.cpp -o stan/lang/ast_def.o


In file included from chains.cpp:19:0:
C:/Program Files/R/R-3.6.2/library/StanHeaders/include/stan/math/prim/mat/fun/mean.hpp: In function 'typename boost::math::tools::promote_args<T>::type stan::math::mean(const std::vector<T>&)':
C:/Program Files/R/R-3.6.2/library/StanHeaders/include/stan/math/prim/mat/fun/mean.hpp:24:63: error: an array reference cannot appear in a constant-expression
   Eigen::Map<const Eigen::Matrix<T, Eigen::Dynamic, 1>> m(&v[0], v.size());
                                                               ^
C:/Program Files/R/R-3.6.2/library/StanHeaders/include/stan/math/prim/mat/fun/mean.hpp:24:63: error: '&' cannot appear in a constant-expression
C:/Program Files/R/R-3.6.2/library/StanHeaders/include/stan/math/prim/mat/fun/mean.hpp:24:68: error: '.' cannot appear in a constant-expression
   Eigen::Map<const Eigen::Matrix<T, Eigen::Dynamic, 1>> m(&v[0], v.size());
                                                                    ^
C:/Program Files/R/R-3.6.2/library/StanHeaders/include/stan/math/prim/mat/fun/mean.hpp:24:73: error: a function call cannot appear in a constant-expression
   Eigen::Map<const Eigen::Matrix<T, Eigen::Dynamic, 1>> m(&v[0], v.size());
                                                                         ^
C:/Program Files/R/R-3.6.2/library/StanHeaders/include/stan/math/prim/mat/fun/mean.hpp:24:74: error: a function call cannot appear in a constant-expression
   Eigen::Map<const Eigen::Matrix<T, Eigen::Dynamic, 1>> m(&v[0], v.size());
                                                                          ^
C:/Program Files/R/R-3.6.2/library/StanHeaders/include/stan/math/prim/mat/fun/mean.hpp:24:74: error: template argument 3 is invalid
C:/Program Files/R/R-3.6.2/library/StanHeaders/include/stan/math/prim/mat/fun/mean.hpp:24:74: error: template argument 4 is invalid
C:/Program Files/R/R-3.6.2/library/StanHeaders/include/stan/math/prim/mat/fun/mean.hpp:24:74: error: template argument 6 is invalid
C:/Program Files/R/R-3.6.2/library/StanHeaders/include/stan/math/prim/mat/fun/mean.hpp:24:27: error: template argument 1 is invalid
   Eigen::Map<const Eigen::Matrix<T, Eigen::Dynamic, 1>> m(&v[0], v.size());

Then I installed Rstan from CRAN, version 2.19.2 but I got the same error as I reported above when trying to compile a model. What am I missing? Thanks.

So, those errors are from doing

install.packages("rstan", type = "source")

after installing StanHeaders 2.19.x ?

Yes, that is correct.

That should be CXX14=$(BINPREF)g++ -std=c++1y. I think the lack of the -std=c++1y might also be the cause of the errors @jhaeberl is seeing.

1 Like

Perfect. I was able to install rstan from source and that did indeed resolve the model compilation issues. So all is well.

Many thanks for your help!

I was able to install rstan from source after installing StanHeaders 2.19.0 and updating my Makevars.win to

CXX14=$(BINPREF)g++ -std=c++1y
CXX14FLAGS=-O3
CXX11FLAGS=-O3

but I sadly still get an error when trying to compile a Stan model (that works on a different machine)

Error in compileCode(f, code, language = language, verbose = verbose) : 
  Compilation ERROR, function(s)/method(s) not created!
C:\Users\maum\AppData\Local\Temp\ccErw77r.s: Assembler messages:
C:\Users\maum\AppData\Local\Temp\ccErw77r.s:7481: Error: invalid register for .seh_savexmm
C:\Users\maum\AppData\Local\Temp\ccErw77r.s:7483: Error: invalid register for .seh_savexmm
C:\Users\maum\AppData\Local\Temp\ccErw77r.s:17828: Error: invalid register for .seh_savexmm
C:\Users\maum\AppData\Local\Temp\ccErw77r.s:89165: Error: invalid register for .seh_savexmm
C:\Users\maum\AppData\Local\Temp\ccErw77r.s:89167: Error: invalid register for .seh_savexmm
C:\Users\maum\AppData\Local\Temp\ccErw77r.s:89169: Error: invalid register for .seh_savexmm
C:\Users\maum\AppData\Local\Temp\ccErw77r.s:94157: Error: invalid register for .seh_savexmm
C:\Users\maum\AppData\Local\Temp\ccErw77r.s:121179: Error: invalid register for .seh_savexmm
C:\Users\maum\AppData\Local\Temp\ccErw77r.s:167542: Error: invalid register for .seh_savexmm
C:\Users\maum\AppData\Local\Temp\ccErw77r.s
In addition: Warning message:
In system(cmd, intern = !verbose) :
  running command 'C:/PROGRA~1/R/R-36~1.2/bin/x64/R CMD SHLIB filebc4c74170.cpp 2> filebc4c74170.cpp.err.txt' had status 1
Error in sink(type = "output") : invalid connection

Is there anything else I could try?

Do you set -mtune=native -march=native at runtime? If so, don’t.

I don’t think that I’m doing that. I’ll have a look at my environment variables right before and after trying to compile this model and then report back.

The part that gets added when trying to compile the model is this:

Sys.getenv()
PKG_CPPFLAGS                                     -I"J:/Documents/R/win-library/3.6/Rcpp/include/"
                                                 -I"J:/Documents/R/win-library/3.6/RcppEigen/include/"
                                                 -I"J:/Documents/R/win-library/3.6/RcppEigen/include/unsupported"
                                                 -I"J:/Documents/R/win-library/3.6/BH/include"
                                                 -I"J:/Documents/R/win-library/3.6/StanHeaders/include/src/"
                                                 -I"J:/Documents/R/win-library/3.6/StanHeaders/include/"
                                                 -I"J:/Documents/R/win-library/3.6/rstan/include" -DEIGEN_NO_DEBUG -DBOOST_DISABLE_ASSERTS
                                             -std=c++1y
PKG_LIBS                                         -L"J:/Documents/R/win-library/3.6/StanHeaders/libs/x64" -lStanHeaders

From this I don’t get the impression that the -mtune=native -march=native gets added at any point. I also don’t see it anywhere else - is there any other place I may be missing?

Nevermind, it seems there was a setting somewhere in the script that did overwrite LOCAL_CPPFLAGS; removing that seems to have fixed the problem. Apologies for missing that!

Thanks for your help @bgoodri!

1 Like

Hi @MauritsM,
please how you did, I have the same problem.

Hi @Rani I’m not sure I can help you. In my case there was a section in my R script that manually changed LOCAL_CPPFLAGS and added some lines that shouldn’t be there. Can you be more specific about what goes wrong on your computer?

Hi,
I’m having problems installing Stan. I followed these instructions carefully :

sessionInfo()
R version 3.6.2 (2019-12-12)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18362)

when I try to install r stan I get:

install.packages(ā€œrstanā€, type = ā€œsourceā€)
trying URL ā€˜https://cran.rstudio.com/src/contrib/rstan_2.21.2.tar.gz’
Content type ā€˜application/x-gzip’ length 1152008 bytes (1.1 MB)
downloaded 1.1 MB

  • installing source package ā€˜rstan’ …
    ** package ā€˜rstan’ successfully unpacked and MD5 sums checked
    ** using staged installation
    ** libs

*** arch - i386
Error in .shlib_internal(args) :
C++14 standard requested but CXX14 is not defined

  • removing ā€˜C:/Users/ymb0814/Documents/R/R-3.6.2/library/rstan’
    Warning in install.packages :
    installation of package ā€˜rstan’ had non-zero exit status

Hi Yina,

Can you run:

dotR <- file.path(Sys.getenv("HOME"), ".R")
if (!file.exists(dotR)) dir.create(dotR)
M <- file.path(dotR, "Makevars.win")
if (!file.exists(M)) file.create(M)
cat("\n CXX14 = g++",
    file = M, sep = "\n", append = TRUE)

Then restart R and try the install again?

1 Like

Thank you so much for answering!

It installed correctly but then:

example(stan_model, package = ā€œrstanā€, run.dontrun = TRUE)
Loading required package: StanHeaders
Loading required package: ggplot2
rstan (Version 2.21.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)
Do not specify ā€˜-march=native’ in ā€˜LOCAL_CPPFLAGS’ or a Makevars file

stn_md> stancode <- ā€˜data {real y_mean;} parameters {real y;} model {y ~ normal(y_mean,1);}’

stn_md> mod <- stan_model(model_code = stancode, verbose = TRUE)

TRANSLATING MODEL ā€˜73fc79f8b1915e8208c736914c86d1a1’ FROM Stan CODE TO C++ CODE NOW.
successful in parsing the Stan model ā€˜73fc79f8b1915e8208c736914c86d1a1’.
COMPILING THE C++ CODE FOR MODEL ā€˜73fc79f8b1915e8208c736914c86d1a1’ NOW.
OS: x86_64, mingw32; rstan: 2.21.2; Rcpp: 1.0.5; inline: 0.3.17
Error in cleanup_makevar(old) : **
** argument ā€œRMUā€ is missing, with no default

In addition: Warning messages:
1: package ā€˜rstan’ was built under R version 3.6.3
2: package ā€˜StanHeaders’ was built under R version 3.6.3
3: package ā€˜ggplot2’ was built under R version 3.6.3
4: In system(paste(CXX, ARGS), ignore.stdout = TRUE, ignore.stderr = TRUE) :
ā€˜C:/rtools40/usr/mingw_/bin/g++’ not found

Great! Steps for solving that error are covered at the bottom of this section: Configuring C Toolchain for Windows Ā· stan-dev/rstan Wiki Ā· GitHub