Unable to get Stan working with R 3.5.1 and Catalina 10.15.2

I follow the directions to install here:

https://github.com/stan-dev/rstan/wiki/Installing-RStan-from-source-on-a-Mac

When I try to install Stan using source

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

, I get this error:

1 error generated.

make: *** [init.o] Error 1
make: *** Waiting for unfinished jobs....
1 error generated.
make: *** [misc.o] Error 1
1 error generated.
make: *** [pointer-tools.o] Error 1
1 error generated.
make: *** [chains.o] Error 1
ERROR: compilation failed for package ‘rstan’
* removing ‘/Users/myname/Library/R/3.5/library/rstan’
* restoring previous ‘/Users/myname/Library/R/3.5/library/rstan’

When I install using this:

install.packages("rstan", repos = "https://cran.r-project.org",subdir = "rstan/rstan", build_opts = "")

and then run this snippet,

example(stan_model, run.dontrun = TRUE)

I get the following error:

Error in compileCode(f, code, language = language, verbose = verbose) : 
  Compilation ERROR, function(s)/method(s) not created! In file included from <built-in>:1:
In file included from /Users/myname/Library/R/3.5/library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp:13:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/Eigen/Dense:1:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/Eigen/Core:82:
In file included from /usr/local/clang6/include/c++/v1/new:91:
In file included from /usr/local/clang6/include/c++/v1/exception:82:
In file included from /usr/local/clang6/include/c++/v1/cstdlib:86:
/usr/local/clang6/include/c++/v1/stdlib.h:94:15: fatal error: 'stdlib.h' file not found
#include_next <stdlib.h>
              ^~~~~~~~~~
1 error generated.
make: *** [file6271b3da8a4.o] Error 1

I cannot seem to get the C++ example to Run in Rccp. Can someone point a new user in the right direction?

Welcome to the community! Have you read this?

Hi!

I did actually, I followed the instructions here.

Did you do xcode-select --install in the terminal?

When I run that, I get the following message:

xcode-select: error: command line tools are already installed, use "Software Update" to install updates

This is very peculiar. I would recommend going through the manual install guide (again if needed):
https://thecoatlessprofessor.com/programming/cpp/r-compiler-tools-for-rcpp-on-macos/

and then make sure to compile rstan etc. from source, i.e.,

remove.packages("rstan")
install.packages('rstan", type = "source")

Hey Torkar,

I tried that, and as it says to do at the end, I try running the simple Rccp file.

Like the guy who commented at the bottom of that page (Ryan Chan), I have the same issue he does:

Error in Rcpp::sourceCpp("/Users/myname/blahblahblah/helloworld.cpp") : 
  Error 1 occurred building shared library.

Any sense on how to deal with this or what this error means?

Can you try calling sourceCpp with the verbose = TRUE argument to see more detail about what the problem is?

That didn’t change the output, but here is more of the output that might be relevant:

In file included from /usr/local/clang6/include/c++/v1/cstdlib:86:
/usr/local/clang6/include/c++/v1/stdlib.h:94:15: fatal error: 'stdlib.h' file not found
#include_next <stdlib.h>
              ^~~~~~~~~~
1 error generated.
make: *** [helloworld.o] Error 1
Error in Rcpp::sourceCpp("/Users/.../helloworld.cpp",  : 
  Error 1 occurred building shared library.

If you type in stdlib.h in Finder does anything show up on your hard disk?

yes, 7 files called “stdlib.h” and 2 called “_stdlib.h”

OK. What are the seven directories containing a stdlib.h?

Here they are below:

  1. /Library/Developer/CommandLineTools/usr/include/c++/v1/stdlib.h
  2. /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/stdlib.h
  3. /usr/local/gfortran/include/c++/6.1.0/tr1/stdlib.h
  4. /Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include/stdlib.h
  5. /usr/local/clang7/include/c++/v1/stdlib.h
  6. /usr/local/clang6/include/c++/v1/stdlib.h
  7. /usr/local/gfortran/include/c++/6.1.0/stdlib.h

OK. @wds15 says that

if you download the installer from

CTRL+right click on the file, then clicking “Open” will get the installer going

So, try that first.

I have installed this and now when I run the Rccp snippet, I get the following error:

clang-7: error: no such file or directory: '/Library/Frameworks/R.framework/Resources/lib/libc++abi.1.dylib'
make: *** [sourceCpp_5.so] Error 1
Error in Rcpp::sourceCpp("/Users/.../helloworld.cpp",  : 
  Error 1 occurred building shared library.

I think it is supposed to be using clang++-8. Do you have a clang8 directory in /usr/local/ and something that refers to clang++-7 in ~/.R/Makevars ?

I don’t think I have clang8 folder at all. Only clang7.

Can you remind me how I find the path to the right Makevars file again?

If you do in R,

writeLines(readLines("~/.R/Makevars"))

you can see it. But to open it in Finder, you have to do CTRL+. or something in order to see folders that start with a period.

for me its clang-7

you should probably search the entire system for libc++abi.1.dylib

Here is the result of that:

# clang: start
CFLAGS=-isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk
CCFLAGS=-isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk
CXXFLAGS=-isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk
CPPFLAGS=-isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -I/usr/local/include

SHLIB_CXXLDFLAGS+=-Wl,-rpath,${R_HOME}/lib ${R_HOME}/lib/libc++abi.1.dylib
SHLIB_CXX14LDFLAGS+=-Wl,-rpath,${R_HOME}/lib ${R_HOME}/lib/libc++abi.1.dylib
# clang: end