Error when Configuring RStan C++ toolchain

I think it may work now, because I got quite an elaborate output. However, if I try to install from source

install.packages(“jsonlite”, type = “source”)

I still get an error

C:/Users/laura/OneDrive/Documenten/R/win-library/4.0/StanHeaders/libs/x64/StanHeaders.dll: file not recognized: file format not recognized
collect2.exe: error: ld returned 1 exit status
no DLL was created
ERROR: compilation failed for package ‘jsonlite’

  • removing ‘C:/Users/laura/OneDrive/Documenten/R/win-library/4.0/jsonlite’
    Warning in install.packages :
    installation of package ‘jsonlite’ had non-zero exit status

Is this okay as long as rstan works now?

Try restarting R and seeing if the jsonlite install works, make sure that you don’t load rstan before this.

Also make sure you pause the synchronisation on your OneDrive when installing packages from source. The synchronisation can cause issues with compilation.

If the rstan example model compiles and samples then it looks like you’re fine to run your models

Hi! I’m also a new RStan user, running into a similar issue with the C++ toolchain.

I have followed these instructions for configuring the toolchain in R4.0: Configuring C Toolchain for Windows · stan-dev/rstan Wiki · GitHub. Everything works fine up until trying to install the package “jsonlite” from source, at which point I get the following error:


> install.packages("jsonlite", type = "source")
Installing package into ‘C:/Users/Murray/Documents/R/win-library/4.1’
(as ‘lib’ is unspecified)
trying URL 'https://cran.rstudio.com/src/contrib/jsonlite_1.7.2.tar.gz'
Content type 'application/x-gzip' length 421716 bytes (411 KB)
downloaded 411 KB

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

*** arch - i386
C:/rtools40/usr/mingw_32/bin/gcc  -I"C:/PROGRA~1/R/R-41~1.1/include" -DNDEBUG -Iyajl/api       -D__USE_MINGW_ANSI_STDIO   -O2 -Wall  -std=gnu99 -mfpmath=sse -msse2 -mstackrealign  -c base64.c -o base64.o
sh: line 1: C:/rtools40/usr/mingw_32/bin/gcc: No such file or directory
make: *** [C:/PROGRA~1/R/R-41~1.1/etc/i386/Makeconf:238: base64.o] Error 127
ERROR: compilation failed for package 'jsonlite'
* removing 'C:/Users/Murray/Documents/R/win-library/4.1/jsonlite'
Warning in install.packages :
  installation of package ‘jsonlite’ had non-zero exit status

Any help would be much appreciated!
Thanks,
Murray

Can you post the output from:

readLines("~/.Rprofile")
readLines("~/.R/Makevars.win")

Hi! Thanks your message. I get the following output:

`> readLines(“~/.Rprofile”)
[1] “Sys.setenv(BINPREF = "C:/rtools40/usr/mingw_$(WIN)/bin/")”

readLines(“~/.R/Makevars.win”)
[1] “”
[2] " CXX14FLAGS += -mtune=native -O3 -mmmx -msse -msse2 -msse3 -mssse3 -msse4.1 -msse4.2"`

Looks like R is still configured for the old RTools version, can you try following these steps in the Getting Started guide and seeing if they help: Configuring C Toolchain for Windows · stan-dev/rstan Wiki · GitHub

I’ve tried uninstalling RTools, then following the guide in the link step by step but I get the same problem and the same output from the two readLines() calls:

> Sys.getenv("BINPREF")
[1] ""
> #install RTools and put it on the path
> install.packages("pkgbuild")
Installing package into ‘C:/Users/Murray/Documents/R/win-library/4.1’
(as ‘lib’ is unspecified)
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.1/pkgbuild_1.2.0.zip'
Content type 'application/zip' length 141813 bytes (138 KB)
downloaded 138 KB

package ‘pkgbuild’ successfully unpacked and MD5 sums checked

The downloaded binary packages are in
	C:\Users\Murray\AppData\Local\Temp\RtmpMfSNZh\downloaded_packages
> rt_path = gsub("\\","/",pkgbuild::rtools_path(),fixed=T)
> rt_bin = paste0(substr(rt_path,1,nchar(rt_path)-4),"/mingw_$(WIN)/bin/")
> writeLines(paste0('PATH="',rt_path,';${PATH}"'), con = "~/.Renviron")
> writeLines(paste0('Sys.setenv(BINPREF = "',rt_bin,'")'), con = "~/.Rprofile")

Then I close RStudio and open it again, continuing

> Sys.which("make")
                              make 
"C:\\rtools40\\usr\\bin\\make.exe" 
> install.packages("jsonlite", type = "source")
Installing package into ‘C:/Users/Murray/Documents/R/win-library/4.1’
(as ‘lib’ is unspecified)
trying URL 'https://cran.rstudio.com/src/contrib/jsonlite_1.7.2.tar.gz'
Content type 'application/x-gzip' length 421716 bytes (411 KB)
downloaded 411 KB

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

*** arch - i386
C:/rtools40/usr/mingw_32/bin/gcc  -I"C:/PROGRA~1/R/R-41~1.1/include" -DNDEBUG -Iyajl/api       -D__USE_MINGW_ANSI_STDIO   -O2 -Wall  -std=gnu99 -mfpmath=sse -msse2 -mstackrealign  -c base64.c -o base64.o
sh: line 1: C:/rtools40/usr/mingw_32/bin/gcc: No such file or directory
make: *** [C:/PROGRA~1/R/R-41~1.1/etc/i386/Makeconf:238: base64.o] Error 127
ERROR: compilation failed for package 'jsonlite'
* removing 'C:/Users/Murray/Documents/R/win-library/4.1/jsonlite'
* restoring previous 'C:/Users/Murray/Documents/R/win-library/4.1/jsonlite'
Warning in install.packages :
  installation of package ‘jsonlite’ had non-zero exit status

The downloaded source packages are in
	‘C:\Users\Murray\AppData\Local\Temp\RtmpqiFHkQ\downloaded_packages’
>readLines("~/.Rprofile")
[1] "Sys.setenv(BINPREF = \"C:/rtools40/usr/mingw_$(WIN)/bin/\")"
> readLines("~/.R/Makevars.win")
[1] ""                                                                                    
[2] " CXX14FLAGS += -mtune=native -O3 -mmmx -msse -msse2 -msse3 -mssse3 -msse4.1 -msse4.2"

You’ve run the instructions for the wrong section unfortunately. You didn’t need to reinstall Rtools,just delete the Rprofile file.

Can you run the following commands to delete the files:

unlink("~/.Rprofile")
unlink("~/.R/Makevars.win")

Then restart R and again follow the instructions for R4.0 only

1 Like

Aha, silly me! And just like that it all worked, and the rstan installation was fine and the example model sampled - hooray!

Thanks so much for your help! :)

2 Likes

Hi,
I appreciate it if you could help me; I also had the same error as I updated the packages in RStudio.
Thus, I followed all your instructions here, however, I am getting the following error:

Loading required package: rstan
Loading required package: ggplot2
Error: package or namespace load failed for ‘ggplot2’ in loadNamespace(i, c(lib.loc, .libPaths()), versionCheck = vI[[i]]):
 namespace ‘ellipsis’ 0.3.1 is being loaded, but >= 0.3.2 is required
Failed with error:  ‘package ‘ggplot2’ could not be loaded’
In addition: Warning messages:
1: package ‘rstan’ was built under R version 4.0.5 
2: package ‘ggplot2’ was built under R version 4.0.5

The error is saying that there’s a problem with ggplot2 and the ellipsis packages. Try restarting R and running:

remove.packages(c("ggplot2","ellipsis"))
install.packages(c("ggplot2","ellipsis"))

Then trying rstan again

Thanks, but I got the below error: ( after running 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'.
Error in make_context(private$console) : 
  function 'Rcpp_precious_remove' not provided by package 'Rcpp'
In addition: Warning messages:
1: package ‘ggplot2’ was built under R version 4.0.5 
2: In system(paste(CXX, ARGS), ignore.stdout = TRUE, ignore.stderr = TRUE) :
  'C:/rtools40/usr/mingw_/bin/g++' not found

stn_md> fit <- sampling(mod, data = list(y_mean = 0))
Error in private:public : argument of length 0

So now that it’s saying that there’s an error with the Rcpp package, can you try reinstalling that?

now I got :

Error in private:public : argument of length 0
In addition: Warning message:
In system(paste(CXX, ARGS), ignore.stdout = TRUE, ignore.stderr = TRUE) :
  'C:/rtools40/usr/mingw_/bin/g++' not found

Also, for other example, I got the following error:

Error in prep_call_sampler(object) : 
  could not find function "prep_call_sampler"
In addition: Warning message:
In system(paste(CXX, ARGS), ignore.stdout = TRUE, ignore.stderr = TRUE) :
  'C:/rtools40/usr/mingw_/bin/g++' not found

Can you now try restarting R and reinstalling rstan and stanheaders from source?

# Compile packages using all cores
Sys.setenv(MAKEFLAGS = paste0("-j",parallel::detectCores()))

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

There will likely be several warnings appearing during the installation, but they’re safe to ignore

still the same error:

Error in private:public : argument of length 0
In addition: Warning messages:
1: package ‘ggplot2’ was built under R version 4.0.5 
2: In system(paste(CXX, ARGS), ignore.stdout = TRUE, ignore.stderr = TRUE) :
  'C:/rtools40/usr/mingw_/bin/g++' not found

Should I reinstall RStudio?

What version of R are you using?

It is R-4.0.2

Can you post the output from the following:

mod = "data{ real y_mean; } parameters{ real y;} model{ y ~normal(y_mean,1); }"
cppcode = rstan::stanc(model_code = mod)
str(cppcode)