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

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?