Collect2.exe: error: ld returned 1 exit status on Windows 10, R 4.0.2

Thanks for helping! I updated StanHeaders, inline, and rstan packages. Now I get the following error:

Compilation ERROR, function(s)/method(s) not created!
Error in 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: file3ee078575c35.o:file3ee078575c35.cpp:(.text$_ZN3tbb8internal26task_scheduler_observer_v3D1Ev[_ZN3tbb8internal26task_scheduler_observer_v3D1Ev]+0x14): undefined reference to tbb::internal::task_scheduler_observer_v3::observe(bool)'C:/rtools40/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: file3ee078575c35.o:file3ee078575c35.cpp:(.text$_ZN3tbb8internal26task_scheduler_observer_v3D0Ev[_ZN3tbb8internal26task_scheduler_observer_v3D0Ev]+0x1c): undefined reference to tbb::internal::task_scheduler_observer_v3::observe(bool)‘C:/rtools40/mingw64/bin/…/lib/gcc/x86_64-w64-mingw32/8.3.0/…/…/…/…/x86_64-w64-mingw32/bin/ld.exe: file3ee078575c35.o:file3ee078575c35.cpp:(.text$_ZN4stan4math16ad_tape_observerD1Ev[ZN4stan4math16ad_tape_observerD1Ev]+0x15): undefined reference to `tbb::internal::task_scheduler_observer_v3::observe(bool)'C:/rt
In addition: Warning message:
In system(paste(CXX, ARGS), ignore.stdout = TRUE, ignore.stderr = TRUE) :
'C:/rtools40/usr/mingw
/bin/g++’ not found

sessionInfo()
R version 4.0.2 (2020-06-22)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19041)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252

attached base packages:
[1] stats graphics grDevices utils datasets methods base

other attached packages:
[1] rstan_2.21.2 ggplot2_3.3.2 StanHeaders_2.21.0-6 RevoUtils_11.0.2 RevoUtilsMath_11.0.0

loaded via a namespace (and not attached):
[1] Rcpp_1.0.5 pillar_1.4.6 compiler_4.0.2 prettyunits_1.1.1 tools_4.0.2 pkgbuild_1.1.0
[7] jsonlite_1.7.0 lifecycle_0.2.0 tibble_3.0.3 gtable_0.3.0 pkgconfig_2.0.3 rlang_0.4.7
[13] cli_2.0.2 rstudioapi_0.11 parallel_4.0.2 curl_4.3 loo_2.3.1 gridExtra_2.3
[19] withr_2.2.0 dplyr_1.0.0 generics_0.0.2 vctrs_0.3.2 stats4_4.0.2 grid_4.0.2
[25] tidyselect_1.1.0 glue_1.4.1 inline_0.3.16 R6_2.4.1 processx_3.4.3 fansi_0.4.1
[31] purrr_0.3.4 callr_3.4.3 magrittr_1.5 codetools_0.2-16 matrixStats_0.56.0 scales_1.1.1
[37] ps_1.3.3 ellipsis_0.3.1 assertthat_0.2.1 colorspace_1.4-1 V8_3.2.0 RcppParallel_5.0.2
[43] munsell_0.5.0 crayon_1.3.4

Can you try installing rstan and stanheaders from source? Like:

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

Be sure to speed up the compilation using multiple cores by first calling:

Sys.setenv(MAKEFLAGS="-jC")

Where C is the number of cores your computer has

I was able to install StanHeaders from source, but not rstan. Below is the R command and output:

install.packages(“rstan”, type = “source”, repos = “https://cloud.r-project.org/”, dependencies = TRUE, verbose = TRUE)
Installing package into ‘C:/Users/bill/R/win-library/4.0’
(as ‘lib’ is unspecified)
system (cmd0): C:/PROGRA~1/MICROS~1/ROPEN~1/R-40~1.2/bin/x64/R CMD INSTALL
trying URL ‘https://cloud.r-project.org/src/contrib/rstan_2.21.2.tar.gz
Content type ‘application/x-gzip’ length 1152008 bytes (1.1 MB)
downloaded 1.1 MB

foundpkgs: rstan, C:\Users\bill\AppData\Local\Temp\RtmpcbbgNi/downloaded_packages/rstan_2.21.2.tar.gz
files: C:\Users\bill\AppData\Local\Temp\RtmpcbbgNi/downloaded_packages/rstan_2.21.2.tar.gz
Microsoft R Open 4.0.2
The enhanced R distribution from Microsoft
Microsoft packages Copyright (C) 2020 Microsoft Corporation

Using the Intel MKL for parallel mathematical computing (using 8 cores).

Default CRAN mirror snapshot taken on 2020-07-16.
See: https://mran.microsoft.com/.

  • installing source package ‘rstan’ …
    ** package ‘rstan’ successfully unpacked and MD5 sums checked
    ** using staged installation
    ** libs
    “C:/rtools40/mingw64/bin/“g++ -m64 -std=gnu++14 -I"C:/PROGRA~1/MICROS~1/ROPEN~1/R-40~1.2/include” -DNDEBUG -I”…/inst/include" -I"…/inst/include/boost_not_in_BH" -I"." -DBOOST_DISABLE_ASSERTS -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION -DBOOST_NO_AUTO_PTR -DSTAN_THREADS Microsoft R Open 4.0.2 The enhanced R distribution from Microsoft Microsoft packages Copyright (C) 2020 Microsoft Corporation Using the Intel MKL for parallel mathematical computing (using 8 cores). Default CRAN mirror snapshot taken on 2020-07-16. See: https://mran.microsoft.com/. -DRCPP_PARALLEL_USE_TBB=1 -I’C:/Program Files/Microsoft/R Open/R-4.0.2/library/Rcpp/include’ -I’C:/Users/bill/R/win-library/4.0/RcppEigen/include’ -I’C:/Users/bill/R/win-library/4.0/BH/include’ -I’C:/Users/bill/R/win-library/4.0/StanHeaders/include’ -I’C:/Users/bill/R/win-library/4.0/RcppParallel/include’ -I"C:/a/w/1/s/vendor/extsoft/include" -DRCPP_PARALLEL_USE_TBB=1 -O2 -Wall -mfpmath=sse -msse2 -mstackrealign -c sparse_extractors.cpp -o sparse_extractors.o
    sh: -c: line 0: syntax error near unexpected token (' sh: -c: line 0: “C:/rtools40/mingw64/bin/“g++ -m64 -std=gnu++14 -I"C:/PROGRA~1/MICROS~1/ROPEN~1/R-40~1.2/include” -DNDEBUG -I”…/inst/include" -I"…/inst/include/boost_not_in_BH" -I"." -DBOOST_DISABLE_ASSERTS -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION -DBOOST_NO_AUTO_PTR -DSTAN_THREADS Microsoft R Open 4.0.2 The enhanced R distribution from Microsoft Microsoft packages Copyright (C) 2020 Microsoft Corporation Using the Intel MKL for parallel mathematical computing (using 8 cores). Default CRAN mirror snapshot taken on 2020-07-16. See: https://mran.microsoft.com/. -DRCPP_PARALLEL_USE_TBB=1 -I’C:/Program Files/Microsoft/R Open/R-4.0.2/library/Rcpp/include’ -I’C:/Users/bill/R/win-library/4.0/RcppEigen/include’ -I’C:/Users/bill/R/win-library/4.0/BH/include’ -I’C:/Users/bill/R/win-library/4.0/StanHeaders/include’ -I’C:/Users/bill/R/win-library/4.0/RcppParallel/include’ -I"C:/a/w/1/s/vendor/extsoft/include" -DRCPP_PARALLEL_USE_TBB=1 -O2 -Wall -mfpmath=sse -msse2 -mstackrealign -c sparse_extractors.cpp -o sparse_extractors.o’
    make: *** [C:/PROGRA~1/MICROS~1/ROPEN~1/R-40~1.2/etc/x64/Makeconf:229: sparse_extractors.o] Error 1
    ERROR: compilation failed for package ‘rstan’
  • removing ‘C:/Users/bill/R/win-library/4.0/rstan’
  • restoring previous ‘C:/Users/bill/R/win-library/4.0/rstan’
    Warning in install.packages :
    installation of package ‘rstan’ had non-zero exit status

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

That’s odd, the issue is that R Open is putting this text:

Microsoft R Open 4.0.2 The enhanced R distribution from Microsoft Microsoft packages Copyright © 2020
 Microsoft Corporation Using the Intel MKL for parallel mathematical computing (using 8 cores). 
Default CRAN mirror snapshot taken on 2020-07-16. See: https://mran.microsoft.com/. 

In the middle of the compilation argument.

Are you able to install other packages from source?

Can you post the output from:

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

Yes, I installed jsonlite package from source successfully. Here is the output you requested:

Sys.getenv(‘PATH’)
[1] “C:\rtools40\usr\bin;C:\Program Files\Microsoft\R Open\R-4.0.2\bin\x64;C:\Windows\System32;C:\Windows;C:\Windows\System32\wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Windows\System32\OpenSSH;C:\Program Files\Git\cmd;C:\Users\bill\AppData\Local\Microsoft\WindowsApps”

Sys.getenv(‘BINPREF’)
[1] “”

readLines(“~/.R/Makevars.win”)
Error in file(con, “r”) : cannot open the connection
In addition: Warning message:
In file(con, “r”) :
cannot open file ‘C:/Users/bill/.R/Makevars.win’: No such file or directory

readLines(“~/.Rprofile”)
Error in file(con, “r”) : cannot open the connection
In addition: Warning message:
In file(con, “r”) :
cannot open file ‘C:/Users/bill/.Rprofile’: No such file or directory

readLines(“~/.Renviron”)
[1] “PATH="{RTOOLS40_HOME}\\usr\\bin;{PATH}"”

Thank you very much for you help.

I’ve been getting many, many different problems with rstan since I updated Microsoft R Open to 4.02, but after much trial and error (and error, and error…) I think this is now the one most related to what I’m experiencing.

So @billdoepp, I had the problem with not being able to install rstan from source too, but managed to solve it a bit serendipitously. The issue seems to be with R Open rprofile.site file, which lies in the “etc” folder of its install directory. Can’t see exactly why but it seems the way it writes its branding message can have unintended effects. If you remove the following lines, being careful with respecting the closure brackets before and after them, rstan installation from source should work… Or at least it did for me!

else if (!quiet) {
	cat("Microsoft R Open ",R.version$major,".",R.version$minor,"\n",sep="")
    cat("The enhanced R distribution from Microsoft\n",sep="")
    cat("Microsoft packages Copyright (C)", Revo.version$year, "Microsoft Corporation\n\n")
	if (MKLmsg == "") {
	   cat(paste("Using the Intel MKL for parallel mathematical computing (using", ncores, "cores).\n", sep = " "))
	} else {
	   cat(MKLmsg)
	}
	cat("\nDefault CRAN mirror snapshot taken on ",repos.date, ".", sep = "")
	cat("\n", "See: https://mran.microsoft.com/.",sep="")
	cat("\n\n")
}	

Now, even after installing rstan and RStanHeaders from source I cannot run the rstan example. This is the output and errors I get: https://pastebin.com/j3PgL88Q

And my diagnostics:

> Sys.getenv("PATH") [1] "C:\\Research\\RTools\\usr\\bin;C:\\Windows\\System32\\wbem;C:\\Windows\\System32\\wbem;C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v10.0\\bin;C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v10.0\\libnvvp;C:\\Windows\\System32\\wbem;C:\\ProgramData\\Oracle\\Java\\javapath_target_3265750;C:\\Windows\\System32;C:\\Windows;C:\\Windows\\System32\\wbem;C:\\Windows\\System32\\WindowsPowerShell\\v1.0;C:\\Program Files (x86)\\Bitvise SSH Client;C:\\Program Files\\Calibre2;C:\\Program Files (x86)\\NVIDIA Corporation\\PhysX\\Common;C:\\Program Files\\Git\\cmd;C:\\Users\\Antonio\\AppData\\Local\\Microsoft\\WindowsApps;C:\\adb;C:\\ProgramData\\chocolatey\\bin;C:\\Windows\\System32;C:\\Windows;C:\\Windows\\System32\\wbem;C:\\Windows\\System32\\WindowsPowerShell\\v1.0;C:\\Windows\\System32\\OpenSSH;C:\\Program Files\\Docker Toolbox;C:\\libxml2\\bin;C:\\libxml2\\include;C:\\libxml2\\lib;C:\\Program Files\\MiKTeX\\miktex\\bin\\x64;C:\\Program Files\\NVIDIA Corporation\\NVIDIA NvDLISR;C:\\Program Files\\TortoiseSVN\\bin;C:\\Users\\Antonio\\AppData\\Local\\GitHubDesktop\\bin;C:\\Users\\Antonio\\AppData\\Local\\Pandoc;C:\\Program Files (x86)\\GnuPG\\bin;C:\\Windows\\System32;C:\\Windows;C:\\Windows\\System32\\wbem;C:\\Windows\\System32\\WindowsPowerShell\\v1.0;C:\\Windows\\System32\\OpenSSH"

> Sys.getenv("BINPREF") [1] ""

> readLines("~/.Renviron") [1] "PATH=\"${RTOOLS40_HOME}\\usr\\bin;${PATH}\""

Also I have already deleted my .Rprofile and .Renviron files following advice on other threads. Thank you in advance for any help as I don’t know what else to try!

sessionInfo()
R version 4.0.2 (2020-06-22)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19041)
Matrix products: default
locale:
[1] LC_COLLATE=Spanish_Spain.1252 LC_CTYPE=Spanish_Spain.1252
[3] LC_MONETARY=Spanish_Spain.1252 LC_NUMERIC=C
[5] LC_TIME=Spanish_Spain.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] rstan_2.21.2 ggplot2_3.3.2 StanHeaders_2.21.0-6
[4] RevoUtils_11.0.2 RevoUtilsMath_11.0.0
loaded via a namespace (and not attached):
[1] Rcpp_1.0.5 pillar_1.4.6 compiler_4.0.2 prettyunits_1.1.1
[5] tools_4.0.2 pkgbuild_1.1.0 jsonlite_1.7.1 lifecycle_0.2.0
[9] tibble_3.0.3 gtable_0.3.0 pkgconfig_2.0.3 rlang_0.4.7
[13] cli_2.0.2 curl_4.3 parallel_4.0.2 loo_2.3.1
[17] gridExtra_2.3 withr_2.3.0 dplyr_1.0.2 generics_0.0.2
[21] vctrs_0.3.4 stats4_4.0.2 grid_4.0.2 tidyselect_1.1.0
[25] glue_1.4.2 inline_0.3.16 R6_2.4.1 processx_3.4.4
[29] fansi_0.4.1 purrr_0.3.4 callr_3.4.4 magrittr_1.5
[33] codetools_0.2-16 matrixStats_0.57.0 scales_1.1.1 ps_1.3.4
[37] ellipsis_0.3.1 assertthat_0.2.1 colorspace_1.4-1 V8_3.2.0
[41] RcppParallel_5.0.2 munsell_0.5.0 crayon_1.3.4

@bgoodri It looks like Microsoft’s R Open 4.0.2 is causing issues with the TBB. The two users above both have rstan 2.21.2 and StanHeaders 2.21.0-6, and are getting the undefined reference error that popped up with 2.21.1:

C:/rtools40/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: file3bc828c2234.o:file3bc828c2234.cpp:(.text+0x1317): undefined reference to `rstan::stan_fit::stan_fit(SEXPREC*, int)'
C:/rtools40/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: file3bc828c2234.o:file3bc828c2234.cpp:(.text+0x75e): undefined reference to `tbb::internal::task_scheduler_observer_v3::observe(bool)'
C:/rtools40/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: file3bc828c2234.o:file3bc828c2234.cpp:(.text$_ZN3tbb8internal26task_scheduler_observer_v3D1Ev[_ZN3tbb8internal26task_scheduler_observe

It does look that way. @wds15 do we have any experience with Microsoft R?

No. Never used that.

I am right now digging into the prophet issues on windows…

I did not find any more the thread with the prophet issue… anyway. It looks as if the prophet R package misses in the DESCRIPTION file to import RcppParallel and they als do not list this under “LinkingTo:”. I hope this fixes the issues under Windows. If you ping me on the right github issue then we can dig further as needed.

EDIT: I can confirm that prophet 0.7 now loads for me under Windows just fine with the changes as outlined. So we should have a check in the rstantools config thing which ensure that RcppParallel is in the imports and linkingTo section of the descirption file.

Thanks @Pintaius. Your solution fixed the rstan install from source problem. I still get (the same?) errors when trying compile the 8schools example.

Andrew – With the help of @Pintaius, I was able to install the rstan package from source. I think I am still getting the same error when running the rstan example, which I think is being worked on(?)

Error in 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: file20681fa83c0b.o:file20681fa83c0b.cpp:(.text$_ZN3tbb8internal26task_scheduler_observer_v3D1Ev[_ZN3tbb8internal26task_scheduler_observer_v3D1Ev]+0x14): undefined reference to `tbb::internal::task_scheduler_observer_v3::observe(bool)'
C:/rtools40/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: file20681fa83c0b.o:file20681fa83c0b.cpp:(.text$_ZN3tbb8internal26task_scheduler_observer_v3D0Ev[_ZN3tbb8internal26task_scheduler_observer_v3D0Ev]+0x1c): undefined reference to `tbb::internal::task_scheduler_observer_v3::observe(bool)'
C:/rtools40/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: file20681fa83c0b.o:file20681fa83c0b.cpp:(.text$_ZN4stan4math16ad_tape_observerD1Ev[_ZN4stan4math16ad_tape_observerD1Ev]+0x15): undefined reference to `tbb::internal::task_scheduler_observer_v3::observe(bool)'
C:/rt
In addition: Warning message:
In system(paste(CXX, ARGS), ignore.stdout = TRUE, ignore.stderr = TRUE) :
'C:/rtools40/usr/mingw_/bin/g++' not found

I will try installing R 4.0.3 this weekend when it’s released. If successful, it will support the conclusion the issue lies within the Microsoft R Open 4.0.2. By the way, I had no problem with installing rstan on my old machine running Microsoft’s R Open 3.5.3.

Yep, it’s something to do with R Open. But the issue is on the radar now.

In the interim, you might have more luck with the cmdstanR package. See this link for more information on it getting that installed and how to use it: https://mc-stan.org/cmdstanr/

1 Like

Thank you @andrjohns, just tried to install cmdstanR and run its examples and it does work!

1 Like

Great to hear!

A quick tip. If you’re planning on running your models through cmdstanR and then reading the results back into a stanfit object (like you would get from RStan; as covered in this section), when you call the $sample(...) method, make sure you add validate_csv = FALSE. Otherwise, cmdstanR will automatically read those samples into R, and you’ll end up using twice the memory if you then read the samples into a stanfit object.

1 Like

I tried but had trouble installing cmdstan. I will wait for the fix or use a non-R Open installation of R.

Thank you very much for your help.

No worries, did you want to troubleshoot the cmdstan installation? We can get that working for you in the interim?

No thanks, Andrew. I do appreciate the offer though.

I think I might have found a solution this problem, again serendipitously. Turns out the ./etc/x64/Makeconf file of Microsoft R Open has a few differences with that of the standard R distribution. After checking them one-by-one, rstan starts working in my computer if I change

LOCAL_SOFT ?= C:/a/w/1/s/vendor/extsoft

to

LOCAL_SOFT ?=

As you can imagine I don’t know why, but seems to do the trick and hasn’t broken anything else (fingers crossed). @billdoepp I don’t know if this is still a relevant issue for you but if you want to try this fix hopefully it’ll work for you as well!

It seems now is finally time to go back to my brms models… :)

1 Like

Thanks @Pintaius. Your solution works! I do get an R warning message after compiling the Stan model, but it runs as expected.

The warning messages is:

Warning message:
In system(paste(CXX, ARGS), ignore.stdout = TRUE, ignore.stderr = TRUE) :
‘C:/rtools40/usr/mingw_/bin/g++’ not found

Thanks again for the solution!!!

1 Like