Rstan fit Error in compileCode(f, code, language = language, verbose = verbose)

hi, thx for the prompt reply.
after running {example(stan_model, run.dontrun = TRUE)}
i get a msg that is:
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.6; inline: 0.3.17

setting environment variables:
LOCAL_LIBS = “C:/R/library/rstan/lib/x64/libStanServices.a” -L"C:/R/library/StanHeaders/libs/x64" -lStanHeaders -L"C:/R/library/RcppParallel/lib/x64" -ltbb
PKG_CPPFLAGS = -I"C:/R/library/Rcpp/include/" -I"C:/R/library/RcppEigen/include/" -I"C:/R/library/RcppEigen/include/unsupported" -I"C:/R/library/BH/include" -I"C:/R/library/StanHeaders/include/src/" -I"C:/R/library/StanHeaders/include/" -I"C:/R/library/RcppParallel/include/" -I"C:/R/library/rstan/include" -DEIGEN_NO_DEBUG -DBOOST_DISABLE_ASSERTS -DBOOST_PENDING_INTEGER_LOG2_HPP -DSTAN_THREADS -DBOOST_NO_AUTO_PTR -include “C:/R/library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp” -std=c++1y


after ther program source, i get the famous {Compilation ERROR, function(s)/method(s) not created!
Error in compileCode(f, code, language = language, verbose = verbose) :
In addition: Warning message:
In system(paste(CXX, ARGS), ignore.stdout = TRUE, ignore.stderr = TRUE) :
‘-E’ not found}

also, may be of interest:

devtools::session_info(“rstan”)

  • Session info --------------------------------------------------
    setting value
    version R version 4.0.4 (2021-02-15)
    os Windows 10 x64
    system x86_64, mingw32
    ui RStudio
    language (EN)
    collate English_United States.1252
    ctype English_United States.1252
    tz Europe/Berlin
    date 2021-04-27

  • Packages ------------------------------------------------------
    ! package * version date lib source
    backports 1.2.1 2020-12-09 [1] CRAN (R 4.0.3)
    BH 1.75.0-0 2021-01-11 [1] CRAN (R 4.0.3)
    callr 3.7.0 2021-04-20 [1] CRAN (R 4.0.5)
    checkmate 2.0.0 2020-02-06 [1] CRAN (R 4.0.5)
    cli 2.4.0 2021-04-05 [1] CRAN (R 4.0.5)
    colorspace 2.0-0 2020-11-11 [1] CRAN (R 4.0.5)
    crayon 1.4.1 2021-02-08 [1] CRAN (R 4.0.5)
    curl 4.3 2019-12-02 [1] CRAN (R 4.0.5)
    desc 1.3.0 2021-03-05 [1] CRAN (R 4.0.5)
    digest 0.6.27 2020-10-24 [1] CRAN (R 4.0.5)
    ellipsis 0.3.1 2020-05-15 [1] CRAN (R 4.0.5)
    fansi 0.4.2 2021-01-15 [1] CRAN (R 4.0.5)
    farver 2.1.0 2021-02-28 [1] CRAN (R 4.0.5)
    ggplot2 * 3.3.3 2020-12-30 [1] CRAN (R 4.0.5)
    glue 1.4.2 2020-08-27 [1] CRAN (R 4.0.5)
    gridExtra 2.3 2017-09-09 [1] CRAN (R 4.0.5)
    gtable 0.3.0 2019-03-25 [1] CRAN (R 4.0.5)
    inline 0.3.17 2020-12-01 [1] CRAN (R 4.0.5)
    isoband 0.2.4 2021-03-03 [1] CRAN (R 4.0.5)
    jsonlite 1.7.2 2020-12-09 [1] CRAN (R 4.0.5)
    labeling 0.4.2 2020-10-20 [1] CRAN (R 4.0.3)
    lattice 0.20-41 2020-04-02 [2] CRAN (R 4.0.4)
    lifecycle 1.0.0 2021-02-15 [1] CRAN (R 4.0.5)
    loo 2.4.1 2020-12-09 [1] CRAN (R 4.0.5)
    magrittr 2.0.1 2020-11-17 [1] CRAN (R 4.0.5)
    MASS 7.3-53 2020-09-09 [2] CRAN (R 4.0.4)
    Matrix 1.3-2 2021-01-06 [2] CRAN (R 4.0.4)
    matrixStats 0.58.0 2021-01-29 [1] CRAN (R 4.0.5)
    mgcv 1.8-33 2020-08-27 [2] CRAN (R 4.0.4)
    munsell 0.5.0 2018-06-12 [1] CRAN (R 4.0.5)
    nlme 3.1-152 2021-02-04 [2] CRAN (R 4.0.4)
    pillar 1.6.0 2021-04-13 [1] CRAN (R 4.0.5)
    pkgbuild 1.2.0 2020-12-15 [1] CRAN (R 4.0.5)
    pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.0.5)
    prettyunits 1.1.1 2020-01-24 [1] CRAN (R 4.0.5)
    processx 3.5.1 2021-04-04 [1] CRAN (R 4.0.5)
    ps 1.6.0 2021-02-28 [1] CRAN (R 4.0.5)
    R6 2.5.0 2020-10-28 [1] CRAN (R 4.0.5)
    RColorBrewer 1.1-2 2014-12-07 [1] CRAN (R 4.0.3)
    Rcpp 1.0.6 2021-01-15 [1] CRAN (R 4.0.5)
    RcppEigen 0.3.3.9.1 2020-12-17 [1] CRAN (R 4.0.5)
    D RcppParallel 5.1.2 2021-04-15 [1] CRAN (R 4.0.5)
    rlang 0.4.10 2020-12-30 [1] CRAN (R 4.0.5)
    rprojroot 2.0.2 2020-11-15 [1] CRAN (R 4.0.5)
    rstan * 2.21.2 2020-07-27 [1] CRAN (R 4.0.5)
    scales 1.1.1 2020-05-11 [1] CRAN (R 4.0.5)
    StanHeaders * 2.21.0-7 2020-12-17 [1] CRAN (R 4.0.5)
    tibble 3.1.1 2021-04-18 [1] CRAN (R 4.0.5)
    utf8 1.2.1 2021-03-12 [1] CRAN (R 4.0.5)
    V8 3.4.1 2021-04-23 [1] CRAN (R 4.0.5)
    vctrs 0.3.7 2021-03-29 [1] CRAN (R 4.0.5)
    viridisLite 0.4.0 2021-04-13 [1] CRAN (R 4.0.5)
    withr 2.4.2 2021-04-18 [1] CRAN (R 4.0.5)

[1] C:/R/library
[2] C:/R/R-4.0.4/library

D – DLL MD5 mismatch, broken installation.

I need a bit more of the error message, sorry. Can you copy as much of the output as possible above the : Compilation ERROR, function(s)/method(s) not created! section?

If it’s easier, you can also copy it into a text file and upload that here instead

here it is:
stan_err_msg.txt (31.7 KB)

ok now i c that it fails at the Rccp level and the RccpParallel library has a brocken installation.
how to remedy that? i installed everything from source with dependencies = TRUE as instructed.
thx again for your assistance

Hmm that’s odd, it doesn’t actually have an error message in there. Let’s check that your c++ toolchain is working. Can you post the output from the following:

system("touch foo.cpp")
system("R CMD SHLIB foo.cpp")

system(“touch foo.cpp”)
[1] 127
system(“R CMD SHLIB foo.cpp”)
[1] 1

Ok, that indicates some issues with your toolchain. To get a better idea of the issue, can you post the outputs from:

readLines("~/.Renviron")
readLines("~/.Rprofile")
readLines("~/.R/Makevars.win")
Sys.getenv("PATH")

readLines("~/.Renviron")
Error in file(con, “r”) : cannot open the connection
In addition: Warning message:
In file(con, “r”) :
cannot open file ‘C:/Users/naoua/OneDrive/Documents/.Renviron’: No such file or directory
readLines("~/.Rprofile")
[1] “Sys.setenv(BINPREF = “C:/rtools40/mingw$(WIN)/bin/”)”
readLines("~/.R/Makevars.win")
[1] “”
[2] " CXX14 = g++ -std=c++1y "
[3] " CXX14FLAGS += -O3 -mmmx -msse -msse2 -msse3 -mssse3 -msse4.1 -msse4.2"
Sys.getenv(“PATH”)
[1] “C:\R\R-4.0.4\bin\x64;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\bin;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\libnvvp;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.0\bin;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.0\libnvvp;C:\Program Files\Microsoft MPI\Bin;C:\Windows\System32;C:\Windows;C:\Windows\System32\wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Windows\System32\OpenSSH;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;C:\Program Files\Microsoft SQL Server\130\Tools\Binn;C:\Program Files (x86)\Wolfram Research\WolframScript;C:\Windows\System32;C:\Windows;C:\Windows\System32\wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Windows\System32\OpenSSH;C:\Program Files\Wolfram Research\WolframScript;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.0\bin\cuda;C:\Users\naoua\AppData\Roaming\Python\Python38\Scripts;C:\Program Files\NVIDIA Corporation\Nsight Compute 2019.4.0;C:\FFmpeg\bin;C:\rtools40;C:\rtools40\mingw64\bin;C:\Users\naoua\AppData\Local\Microsoft\WindowsApps;C:\Users\naoua\AppData\Local\Programs\MiKTeX 2.9\miktex\bin\x64;C:\Users\naoua\OneDrive\Documents\Python Scripts;C:\Users\naoua\AppData\Local\Programs\Microsoft VS Code\bin”

It looks like your configuration is a little off. Can you go through the steps in the RStan Getting Started Guide to get everything setup?

can you please be precise? to which config is off?
i confirm i did follow those instructions many many many times

is this config then ok? because i still get the error

system(“touch foo.cpp”)
[1] 0

system(“R CMD SHLIB foo.cpp”)
[1] 1
readLines("~/.Renviron")
[1] “PATH=”{RTOOLS40_HOME}\\usr\\bin;{PATH}""
readLines("~/.Rprofile")
Error in file(con, “r”) : cannot open the connection
In addition: Warning message:
In file(con, “r”) :
cannot open file ‘C:/Users/naoua/OneDrive/Documents/.Rprofile’: No such file or directory
readLines("~/.R/Makevars.win")
[1] “”
[2] " CXX14 = g++ -std=c++1y "
[3] " CXX14FLAGS += -O3 -mmmx -msse -msse2 -msse3 -mssse3 -msse4.1 -msse4.2"
Sys.getenv(“PATH”)
[1] “C:\rtools40\usr\bin;C:\R\R-4.0.4\bin\x64;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\bin;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\libnvvp;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.0\bin;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.0\libnvvp;C:\Program Files\Microsoft MPI\Bin;C:\Windows\System32;C:\Windows;C:\Windows\System32\wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Windows\System32\OpenSSH;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;C:\Program Files\Microsoft SQL Server\130\Tools\Binn;C:\Program Files (x86)\Wolfram Research\WolframScript;C:\Windows\System32;C:\Windows;C:\Windows\System32\wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Windows\System32\OpenSSH;C:\Program Files\Wolfram Research\WolframScript;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.0\bin\cuda;C:\Users\naoua\AppData\Roaming\Python\Python38\Scripts;C:\Program Files\NVIDIA Corporation\Nsight Compute 2019.4.0;C:\FFmpeg\bin;C:\rtools40;C:\rtools40\mingw64\bin;C:\Users\naoua\AppData\Local\Microsoft\WindowsApps;C:\Users\naoua\AppData\Local\Programs\MiKTeX 2.9\miktex\bin\x64;C:\Users\naoua\OneDrive\Documents\Python Scripts;C:\Users\naoua\AppData\Local\Programs\Microsoft VS Code\bin”

Yep, that looks better. Can you now restart R and try the rstan install from source again:

remove.packages(c("StanHeaders","rstan"))
install.packages(c("StanHeaders","rstan"),type="source")

smtg is odd here:

install.packages(c(“StanHeaders”,“rstan”),type=“source”)
Installing packages into ‘C:/R/library’
(as ‘lib’ is unspecified)
trying URL ‘https://cran.rstudio.com/src/contrib/StanHeaders_2.21.0-7.tar.gz
Content type ‘application/x-gzip’ length 1402572 bytes (1.3 MB)
downloaded 1.3 MB

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

Warning in install.packages :
installation of package ‘StanHeaders’ had non-zero exit status
Warning in install.packages :
installation of package ‘rstan’ had non-zero exit status

The downloaded source packages are in
‘C:\Users\naoua\AppData\Local\Temp\Rtmp63WjFW\downloaded_packages’

Restarting R session…

library(rstan)
Error in library(rstan) : there is no package called ‘rstan’

Are there any errors other than:

Warning in install.packages :
installation of package ‘StanHeaders’ had non-zero exit status
Warning in install.packages :
installation of package ‘rstan’ had non-zero exit status

Or is that all that is displayed?

Hi I’ have a same error too. Even if I tried

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

It says still

fit <- stan(file='model/model4-5.stan', data=data, seed=1234)
make cmd is
  make -f "C:/PROGRA~1/R/R-40~1.3/etc/x64/Makeconf" -f "C:/PROGRA~1/R/R-40~1.3/share/make/winshlib.mk" CXX='$(CXX14) $(CXX14STD)' CXXFLAGS='$(CXX14FLAGS)' CXXPICFLAGS='$(CXX14PICFLAGS)' SHLIB_LDFLAGS='$(SHLIB_CXX14LDFLAGS)' SHLIB_LD='$(SHLIB_CXX14LD)' SHLIB="file28785f8b7742.dll" WIN=64 TCLBIN=64 OBJECTS="file28785f8b7742.o"

make would use
if test "zfile28785f8b7742.o" != "z"; then \
  if test -e "file28785f8b7742-win.def"; then \
    echo "c:/rtools40/mingw64/bin/"g++  -shared -s -static-libgcc -o file28785f8b7742.dll file28785f8b7742-win.def file28785f8b7742.o   "C:/Program Files/R/R-4.0.3/library/rstan/lib/x64/libStanServices.a" -L"C:/Program Files/R/R-4.0.3/library/StanHeaders/libs/x64" -lStanHeaders -L"C:/Program Files/R/R-4.0.3/library/RcppParallel/lib/x64" -ltbb  -L"C:/PROGRA~1/R/R-40~1.3/bin/x64" -lR ; \
    "c:/rtools40/mingw64/bin/"g++  -shared -s -static-libgcc -o file28785f8b7742.dll file28785f8b7742-win.def file28785f8b7742.o   "C:/Program Files/R/R-4.0.3/library/rstan/lib/x64/libStanServices.a" -L"C:/Program Files/R/R-4.0.3/library/StanHeaders/libs/x64" -lStanHeaders -L"C:/Program Files/R/R-4.0.3/library/RcppParallel/lib/x64" -ltbb  -L"C:/PROGRA~1/R/R-40~1.3/bin/x64" -lR ; \
  else \
    echo EXPORTS > tmp.def; \
    "c:/rtools40/mingw64/bin/"nm file28785f8b7742.o | sed -n 's/^.* [BCDRT] / /p' | sed -e '/[.]refptr[.]/d' -e '/[.]weak[.]/d' | sed 's/[^ ][^ ]*/"&"/g'  >> tmp.def; \
    echo "c:/rtools40/mingw64/bin/"g++  -shared -s -static-libgcc -o file28785f8b7742.dll tmp.def file28785f8b7742.o   "C:/Program Files/R/R-4.0.3/library/rstan/lib/x64/libStanServices.a" -L"C:/Program Files/R/R-4.0.3/library/StanHeaders/libs/x64" -lStanHeaders -L"C:/Program Files/R/R-4.0.3/library/RcppParallel/lib/x64" -ltbb  -L"C:/PROGRA~1/R/R-40~1.3/bin/x64" -lR ; \
    "c:/rtools40/mingw64/bin/"g++  -shared -s -static-libgcc -o file28785f8b7742.dll tmp.def file28785f8b7742.o   "C:/Program Files/R/R-4.0.3/library/rstan/lib/x64/libStanServices.a" -L"C:/Program Files/R/R-4.0.3/library/StanHeaders/libs/x64" -lStanHeaders -L"C:/Program Files/R/R-4.0.3/library/RcppParallel/lib/x64" -ltbb  -L"C:/PROGRA~1/R/R-40~1.3/bin/x64" -lR ; \
    rm -f tmp.def; \
  fi \
fi
 compileCode(f, code, language = language, verbose = verbose) でエラー: 
  C:/rtools40/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Program Files/R/R-4.0.3/library/rstan/lib/x64/libStanServices.a(stan_fit.o):stan_fit.cpp:(.rdata$_ZZN5boost4math6detail9bessel_j0IeEET_S3_E2P1[_ZZN5boost4math6detail9bessel_j0IeEET_S3_E2P1]+0x0): multiple definition of `boost::math::detail::bessel_j0<long double>(long double)::P1'; file28785f8b7742.o:file28785f8b7742.cpp:(.data$_ZZN5boost4math6detail9bessel_j0IeEET_S3_E2P1[_ZZN5boost4math6detail9bessel_j0IeEET_S3_E2P1]+0x0): first defined hereC:/rtools40/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Program Files/R/R-4.0.3/library/rstan/lib/x64/libStanServices.a(stan_fit.o):stan_fit.cpp:(.rdata$_ZZN5boost4math6detail9bessel_j0IeEET_S3_E2QC[_ZZN5boost4math6detail9bessel_j0IeEET_S3_E2QC]+0x0): multiple definition of `boost::math::detail::bessel_j0<long double>(long double)::QC'; file28785f8b7742.o:file28785f8b7742.cpp:(.d
 追加情報:  警告メッセージ: 
 system(paste(CXX, ARGS), ignore.stdout = TRUE, ignore.stderr = TRUE) で: 
  'C:/rtools40/usr/mingw_/bin/g++' not found
 sink(type = "output") でエラー:  コネクションが不正です

I’ll be glad if you could help me.

Can you try running:

remove.packages(c("StanHeaders","rstan"))

Then closing and restarting R, then running:

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

Thank you so much for your reply. I really appreciate it.

As I proceed your advice. I see a compiling failure of Rstan as follows.

C:/Program Files/R/R-4.0.3/library/BH/include/boost/mpl/assert.hpp:194:21: warning: unnecessary parentheses in declaration of 'assert_arg' [-Wparentheses]
failed ************ (Pred::************
                         ^
                         C:/Program Files/R/R-4.0.3/library/BH/include/boost/mpl/assert.hpp:199:21: warning: unnecessary parentheses in declaration of 'assert_not_arg' [-Wparentheses]
                     failed ************ (boost::mpl::not_<Pred>::************

The whole compiling message was too long for Rstudio console and I couldn’t get it. But last part is as follows

C:/Program Files/R/R-4.0.3/library/StanHeaders/include/stan/math/rev/core/set_zero_all_adjoints.hpp: At global scope:
C:/Program Files/R/R-4.0.3/library/StanHeaders/include/stan/math/rev/core/set_zero_all_adjoints.hpp:14:13: warning: 'void stan::math::set_zero_all_adjoints()' defined but not used [-Wunused-function]
 static void set_zero_all_adjoints() {
             ^~~~~~~~~~~~~~~~~~~~~
"c:/rtools40/mingw64/bin/"ar -rs ../inst/lib//x64/libStanServices.a stan_fit.o stan_fit_base.o
C:\rtools40\mingw64\bin\ar.exe: ../inst/lib//x64/libStanServices.a を作成しています(generaing)
DLLは生成されませんでした (counldn't generate)
ERROR: compilation failed for package 'rstan'
* removing 'C:/Program Files/R/R-4.0.3/library/rstan'
Warning in install.packages :
  installation of package ‘rstan’ had non-zero exit status

Here’s my situation.

I could solve it. Thank you for your help!

By reinstalling R, it was solved. I guess this is caused by my environment.