RStan install problem on Mac (Nojave)

I solved the problem. Just for the next person who runs into this problem, I wanted to document my failures.

I installed xcode, deleted my R install and RStudio, and reinstalled these and then started the rstan install from scratch, following the steps laid out on mc-stan.org.

Then I got a new error message:

Error in compileCode(f, code, language = language, verbose = verbose) :
Compilation ERROR, function(s)/method(s) not created! In file included from file10025f51dc6d.cpp:3:
In file included from /Library/Frameworks/R.framework/Resources/include/R.h:47:
In file included from /usr/local/clang4/bin/…/include/c++/v1/cstdlib:86:
/usr/local/clang4/bin/…/include/c++/v1/stdlib.h:94:15: fatal error: ‘stdlib.h’ file not found
#include_next <stdlib.h>
^~~~~~~~~~
1 error generated.
make: *** [file10025f51dc6d.o] Error 1
In addition: Warning message:
In system(cmd, intern = !verbose) :
running command ‘/Library/Frameworks/R.framework/Resources/bin/R CMD SHLIB file10025f51dc6d.cpp 2> file10025f51dc6d.cpp.err.txt’ had status 1

The stdlib.h file does not exist on my hard drive.

My Makevars looks like this:

LDFLAGS= -L/usr/local/clang4/lib
CC=/usr/local/clang4/bin/clang
CXX=/usr/local/clang4/bin/clang++
CXX1X=/usr/local/clang4/bin/clang++
CXX98=/usr/local/clang4/bin/clang++
CXX11=/usr/local/clang4/bin/clang++
CXX14=/usr/local/clang4/bin/clang++
CXX17=/usr/local/clang4/bin/clang++

CXX14FLAGS=-O3 -mtune=native -march=native -Wno-unused-variable -Wno-unused-function -Wno-macro-redefined -Wno-unknown-pragmas

CC=clang
CXX14=clang++ -arch x86_64 -ftemplate-depth-256

Command line tools are installed too:

Shravans-MBP:~ shravanvasishth$ xcode-select --install
xcode-select: error: command line tools are already installed, use “Software Update” to install updates

Googling for the stdlib.h error, I found this exchange:

https://github.com/stan-dev/rstan/issues/527

At the very end, Ben says we have to run

Sys.setenv(USE_CXX14 = 1)

Once I did that, test test succeeds. Ben says he won't explain in polite company why this is necessary. Is it possible to write this step into the installation guide for now? I mean here (here). It would have saved me a lot of time if the workaround was mentioned there.

Checking your installation

  1. Restart R after the installation and, before loading RStan, verify that no objects created by an older version of RStan are (perhaps auto-)loaded into R.

  2. Verify that your toolchain works by executing the code below in R and checking that it returns the value 10:

fx <- inline::cxxfunction( signature(x = "integer", y = "numeric" ) , '
	return ScalarReal( INTEGER(x)[0] * REAL(y)[0] ) ;
' ) fx( 2L, 5 ) # should be 10



3 Likes