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

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.

Hi, face the same problem, I get this error "Error in compileCode(f, code, language = language, verbose = verbose) when I try to run this line of the code :

f_svslagr_incorp ← brm(
formula = m_svslagr_incorp,
prior = prior_svslagr_incorp,
data = d_incl_svslagr_noNAs,
chains = 4,
cores = 4,
warmup = 2000,
iter = 20000,
control = list(adapt_delta = 0.99),
save_pars = save_pars(all = TRUE)
)

p ← pp_check(f_svslagr_incorp, type = β€œbars”, ndraws = 100)
(p + labs(title = β€œf_svslagr_incorp”))

The error mssg I get is:

Error in compileCode(f, code, language = language, verbose = verbose) :
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: file653019f55aed.o:file653019f55aed.cpp:(.text$_ZN3tbb8internal26task_scheduler_observer_v3D0Ev[_ZN3tbb8internal26task_scheduler_observer_v3D0Ev]+0x1d): undefined reference to tbb::internal::task_scheduler_observer_v3::observe(bool)'C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: file653019f55aed.o:file653019f55aed.cpp:(.text$_ZN3tbb10interface623task_scheduler_observerD1Ev[_ZN3tbb10interface623task_scheduler_observerD1Ev]+0x1d): undefined reference to tbb::internal::task_scheduler_observer_v3::observe(bool)'C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: file653019f55aed.o:file653019f55aed.cpp:(.text$_ZN3tbb10interface623task_scheduler_observerD1Ev[_ZN3tbb10interface623task_scheduler_observerD1Ev]+0x3a): undefined reference to `tbb::internal::task_scheduler_observer_v3::observe(bool)'C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: file653019f55aed.o:file653019f55aed.cpp:(.text$_ZN3tbb10interface
Error in sink(type = β€œoutput”) : invalid connection

I use R4.2.1

See the RStan Getting Started guide: Configuring C Toolchain for Windows Β· stan-dev/rstan Wiki Β· GitHub

It says that the promblem is that R 4.2 is not compatible with the current Rstan, tried the suggested solution, to install the preview of rstan 2.26 using:

install.packages(β€œStanHeaders”, repos = c(β€œRepository for distributing (some) stan-dev R packages | r-packages”, getOption(β€œrepos”)))
install.packages(β€œrstan”, repos = c(β€œRepository for distributing (some) stan-dev R packages | r-packages”, getOption(β€œrepos”)))

I typed these lines in the code I’m running, at the beginning of the code, and I got the session aborted mssg, I appreciate your help, I’m stuck in this for 5 days so far

thank you

1 Like

Can you try restarting R, and then only running these lines of code (make sure you copy and paste, rather than typing):

install.packages("StanHeaders", repos = c("https://mc-stan.org/r-packages/", getOption("repos")))
install.packages("rstan", repos = c("https://mc-stan.org/r-packages/", getOption("repos")))

After you run these two lines of code (and no others), does the session abort?

I face the same problem as @sumaya_zoubi and i have followed the steps in the Rstan Getting Started guide. After i ran
install.packages(β€œStanHeaders”, repos = c(β€œRepository for distributing (some) stan-dev R packages | r-packages”, getOption(β€œrepos”)))
install.packages(β€œrstan”, repos = c(β€œRepository for distributing (some) stan-dev R packages | r-packages”, getOption(β€œrepos”)))
these two lines, the session did not abort.So how can i solve the problem?

Can you be more specific about what the issue is? If the session isn’t aborting, doesn’t that resolve the issue?

but after i ran the example, the error in compileCode(f, code, language = language, verbose = verbose) still came out. And i tried
system(β€œtouch foo.cpp”)
system(β€œR CMD SHLIB foo.cpp”)
these two lines and got two β€œ[1] 0”
is it the reason of why i had that error?

i use R4.2.1, win11

Can you post the full compileCode error output that you get, as well as the output from:

if (!requireNamespace("devtools")) {
  install.packages("devtools")
}
devtools::session_info("rstan")

this is the full compileCode error output:

Error in compileCode(f, code, language = language, verbose = verbose) : 
  C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: file35441cb83cc1.o:file35441cb83cc1.cpp:(.text$_ZN3tbb8internal26task_scheduler_observer_v3D0Ev[_ZN3tbb8internal26task_scheduler_observer_v3D0Ev]+0x1d): undefined reference to `tbb::internal::task_scheduler_observer_v3::observe(bool)'C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: file35441cb83cc1.o:file35441cb83cc1.cpp:(.text$_ZN3tbb10interface623task_scheduler_observerD1Ev[_ZN3tbb10interface623task_scheduler_observerD1Ev]+0x1d): undefined reference to `tbb::internal::task_scheduler_observer_v3::observe(bool)'C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: file35441cb83cc1.o:file35441cb83cc1.cpp:(.text$_ZN3tbb10interface623task_scheduler_observerD1Ev[_ZN3tbb10interface623task_scheduler_observerD1Ev]+0x3a): undefined reference to `tbb::internal::task_scheduler_observer_v3::observe(bool)'C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: file35441cb83cc1.o:file35441cb83cc1.cpp:(.text$_ZN3tbb10interface

and after running two lines:

i got:

devtools::session_info("rstan")
─ Session info ────────────────────────────────────────────────────────
 setting  value
 version  R version 4.2.1 (2022-06-23 ucrt)
 os       Windows 10 x64 (build 22000)
 system   x86_64, mingw32
 ui       RStudio
 language (EN)
 collate  Chinese (Simplified)_China.utf8
 ctype    Chinese (Simplified)_China.utf8
 tz       Asia/Taipei
 date     2022-10-20
 rstudio  2022.07.2+576 Spotted Wakerobin (desktop)
 pandoc   NA

─ Packages ────────────────────────────────────────────────────────────
 ! package      * version   date (UTC) lib source
   backports      1.4.1     2021-12-13 [1] CRAN (R 4.2.0)
   BH             1.78.0-0  2021-12-15 [1] CRAN (R 4.2.0)
   callr          3.7.2     2022-08-22 [1] CRAN (R 4.2.1)
   checkmate      2.1.0     2022-04-21 [1] CRAN (R 4.2.1)
   cli            3.4.1     2022-09-23 [1] CRAN (R 4.2.1)
   colorspace     2.0-3     2022-02-21 [1] CRAN (R 4.2.1)
   crayon         1.5.2     2022-09-29 [1] CRAN (R 4.2.1)
   desc           1.4.2     2022-09-08 [1] CRAN (R 4.2.1)
   digest         0.6.29    2021-12-01 [1] CRAN (R 4.2.1)
   fansi          1.0.3     2022-03-24 [1] CRAN (R 4.2.1)
   farver         2.1.1     2022-07-06 [1] CRAN (R 4.2.1)
   ggplot2      * 3.3.6     2022-05-03 [1] CRAN (R 4.2.1)
   glue           1.6.2     2022-02-24 [1] CRAN (R 4.2.1)
   gridExtra      2.3       2017-09-09 [1] CRAN (R 4.2.1)
   gtable         0.3.1     2022-09-01 [1] CRAN (R 4.2.1)
   inline         0.3.19    2021-05-31 [1] CRAN (R 4.2.1)
   isoband        0.2.6     2022-10-06 [1] CRAN (R 4.2.1)
   labeling       0.4.2     2020-10-20 [1] CRAN (R 4.2.0)
   lattice        0.20-45   2021-09-22 [1] CRAN (R 4.2.1)
   lifecycle      1.0.3     2022-10-07 [1] CRAN (R 4.2.1)
   loo            2.5.1     2022-03-24 [1] CRAN (R 4.2.1)
   magrittr       2.0.3     2022-03-30 [1] CRAN (R 4.2.1)
   MASS           7.3-57    2022-04-22 [1] CRAN (R 4.2.1)
   Matrix         1.4-1     2022-03-23 [1] CRAN (R 4.2.1)
   matrixStats    0.62.0    2022-04-19 [1] CRAN (R 4.2.1)
   mgcv           1.8-40    2022-03-29 [1] CRAN (R 4.2.1)
   munsell        0.5.0     2018-06-12 [1] CRAN (R 4.2.1)
   nlme           3.1-157   2022-03-25 [1] CRAN (R 4.2.1)
   pillar         1.8.1     2022-08-19 [1] CRAN (R 4.2.1)
   pkgbuild       1.3.1     2021-12-20 [1] CRAN (R 4.2.1)
   pkgconfig      2.0.3     2019-09-22 [1] CRAN (R 4.2.1)
   prettyunits    1.1.1     2020-01-24 [1] CRAN (R 4.2.1)
   processx       3.7.0     2022-07-07 [1] CRAN (R 4.2.1)
   ps             1.7.1     2022-06-18 [1] CRAN (R 4.2.1)
   R6             2.5.1     2021-08-19 [1] CRAN (R 4.2.1)
   RColorBrewer   1.1-3     2022-04-03 [1] CRAN (R 4.2.0)
   Rcpp           1.0.9     2022-07-08 [1] CRAN (R 4.2.1)
   RcppEigen      0.3.3.9.2 2022-04-08 [1] CRAN (R 4.2.1)
 D RcppParallel   5.1.5     2022-01-05 [1] CRAN (R 4.2.1)
   rlang          1.0.6     2022-09-24 [1] CRAN (R 4.2.1)
   rprojroot      2.0.3     2022-04-02 [1] CRAN (R 4.2.1)
   rstan        * 2.21.7    2022-09-08 [1] CRAN (R 4.2.1)
   scales         1.2.1     2022-08-20 [1] CRAN (R 4.2.1)
   StanHeaders  * 2.21.0-7  2020-12-17 [1] CRAN (R 4.2.1)
   tibble         3.1.8     2022-07-22 [1] CRAN (R 4.2.1)
   utf8           1.2.2     2021-07-24 [1] CRAN (R 4.2.1)
   vctrs          0.4.2     2022-09-29 [1] CRAN (R 4.2.1)
   viridisLite    0.4.1     2022-08-22 [1] CRAN (R 4.2.1)
   withr          2.5.0     2022-03-03 [1] CRAN (R 4.2.1)

 [1] E:/R-4.2.1/library

 D ── DLL MD5 mismatch, broken installation.

According that session_info, you haven’t successfully installed the rstan preview. Can you close and restart R, making sure that no packages or objects get loaded, and run:

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

install.packages("rstan", repos = c("https://mc-stan.org/r-packages/", getOption("repos")))

And post any errors that you get

there seems to be no error in the output:

> remove.packages(c("StanHeaders", "rstan"))
Removing packages from β€˜E:/R-4.2.1/library’
(as β€˜lib’ is unspecified)
> install.packages("rstan", repos = c("https://mc-stan.org/r-packages/", getOption("repos")))
also installing the dependency β€˜StanHeaders’

trying URL 'https://mc-stan.org/r-packages/bin/windows/contrib/4.2/StanHeaders_2.26.13.zip'
Content type 'application/zip' length 3199156 bytes (3.1 MB)
downloaded 3.1 MB

trying URL 'https://mc-stan.org/r-packages/bin/windows/contrib/4.2/rstan_2.26.13.zip'
Content type 'application/zip' length 2344412 bytes (2.2 MB)
downloaded 2.2 MB

package β€˜StanHeaders’ successfully unpacked and MD5 sums checked
package β€˜rstan’ successfully unpacked and MD5 sums checked

The downloaded binary packages are in
	C:\Users\Administrator\AppData\Local\Temp\RtmpAXULWa\downloaded_packages

Now that the preview has been installed, you should be able to run the example model

There is no error any more! Amazing! Thank you for your patient help!😭

Dear Andrew,
I seem to be having a similar problem. I followed the steps given here to install Rtools, configure the C++ Toolchain, and install RStan:

  • but at the step for verifying the RStan installation, I get this error:

Error in compileCode(f, code, language = language, verbose = verbose) :
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: file277864591613.o:file277864591613.cpp:(.text$_ZN3tbb8internal26task_scheduler_observer_v3D0Ev[_ZN3tbb8internal26task_scheduler_observer_v3D0Ev]+0x1d): undefined reference to tbb::internal::task_scheduler_observer_v3::observe(bool)'C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: file277864591613.o:file277864591613.cpp:(.text$_ZN3tbb10interface623task_scheduler_observerD1Ev[_ZN3tbb10interface623task_scheduler_observerD1Ev]+0x1d): undefined reference to tbb::internal::task_scheduler_observer_v3::observe(bool)'C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: file277864591613.o:file277864591613.cpp:(.text$_ZN3tbb10interface623task_scheduler_observerD1Ev[_ZN3tbb10interface623task_scheduler_observerD1Ev]+0x3a): undefined reference to `tbb::internal::task_scheduler_observer_v3::observe(bool)'C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: file277864591613.o:file277864591613.cpp:(.text$_ZN3tbb10interface

Can you please help?

See the Getting Started guide