Compiling error for 8schools example, MacOS Mojative

Hi all,

This is my first time using Rstan, I followed the instruction from https://github.com/stan-dev/rstan/wiki/RStan-Getting-Started to install it. When I try to compile the 8schools example in Rstudio, it returns an error which shows below:

Error in compileCode(f, code, language = language, verbose = verbose) :
Compilation ERROR, function(s)/method(s) not created! In file included from file146ec754c9e8c.cpp:8:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/StanHeaders/include/src/stan/model/model_header.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/StanHeaders/include/stan/math.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/StanHeaders/include/stan/math/rev/mat.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/StanHeaders/include/stan/math/rev/core.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/StanHeaders/include/stan/math/rev/core/autodiffstackstorage.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/StanHeaders/include/stan/math/memory/stack_alloc.hpp:8:
In file included from /usr/local/clang4/bin/…/include/c++/v1/cstdlib:86:
In addition: Warning message:
In system(cmd, intern = !verbose) :
running command ‘/Library/Frameworks/R.framework/Resources/bin/R CMD SHLIB file146ec754c9e8c.cpp 2> file146ec754c9e8c.cpp.err.txt’ had status 1
Error in sink(type = “output”) : invalid connection

Can anyone help me with that?

R version: 3.5.3
Rstuido version: 1.2.1330
Rstan version: 2.18.2

Thanks in advance!

Yize

What is the string surrounding the word error: (with the colon) when doing

stancode <- 'data {real y_mean;} parameters {real y;} model {y ~ normal(y_mean,1);}'
mod <- stan_model(model_code = stancode, verbose = TRUE)

Hi bgoodri,

Do you mean this part? (shown below)

Error in compileCode(f, code, language = language, verbose = verbose) :
Compilation ERROR, function(s)/method(s) not created! In file included from file146ec27adb17c.cpp:8:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/StanHeaders/include/src/stan/model/model_header.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/StanHeaders/include/stan/math.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/StanHeaders/include/stan/math/rev/mat.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/StanHeaders/include/stan/math/rev/core.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/StanHeaders/include/stan/math/rev/core/autodiffstackstorage.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/StanHeaders/include/stan/math/memory/stack_alloc.hpp:8:
In file included from /usr/local/clang4/bin/…/include/c++/v1/cstdlib:86:

No, I mean the part that starts with error:. It probably then says something to the effect of “cannot find this file”.

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: *** [file146ec4012a52f.o] Error 1

ERROR(s) during compilation: source code errors or compiler configuration errors!

I think this might be what you look for?

It stated that ‘stdlib.h’ file not found.

Then this applies

If that doesn’t work but you have a /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14. pkg , install that so it puts C++ headers back in /usr/include

For this one do you mean I need to type this code into R or Xcode?

It just means you need to click on /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14. pkg from Finder.

Sorry I’m still not clear about this stuff.

1. Make sure you have opened Xcode once via Spotlight to accept its license

I have open Xcode but I don’t need to do something else except open this app, do I?

2. If that doesn’t work but you have a /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14. pkg , install that so it puts C++ headers back in /usr/include

For this one I don’t have a finder called ‘Packages’ in CommandLineTools. So where can I download the relevant package?

3. If that doesn’t work, rename / delete / empty the ~/.R/Makevars file to use Xcode’s clang++ rather than LLVM’s

Similarly, how can I do this step?

4. If that doesn’t work, recreate ~/.R/Makevars with a line that says CXX14FLAGS = -isystem ??? where ??? means the path to the C++ header files such as /usr/include

And what about this?

Sorry for lots of questions regarding these. I’m also a stranger to MacOS system so I’m really confused about these stuff.

For 2, if you do in R

dir(file.path("Library", "Developer", "CommandLineTools", "Packages"))

what does it say?

Sorry for the late reply. It will return: character(0)

OK, try in R doing

file.rename(from = "~/.R/Makevars", to = "~/.R/Makevars_old")

and then try to compile something again.

It still does not work.

file.rename(from = “~/.R/Makevars”, to = “~/.R/Makevars_old”)
[1] FALSE
Warning message:
In file.rename(from = “~/.R/Makevars”, to = “~/.R/Makevars_old”) :
cannot rename file ‘/Users/mayize/.R/Makevars’ to ‘/Users/mayize/.R/Makevars_old’, reason ‘Permission denied’

That is atypical. Try

Sys.setenv(R_MAKEVARS_USER = "/dev/null")

and then compiling something.

Thank you so much for your patience! It works now!

OK, that is only temporary. Outside of R / RStudio, you need to find a way to delete the file at /Users/mayize/.R/Makevars.

But I cannot find this /Users/mayize/.R/Makevars file in my laptop. Actually there is no “.R” folder in /Users/mayize.

You may have to enable seeing “hidden” folders (those that start with a dot).

Wow thanks a lot! I figured it out.