Compilation error on Mac

My goodness, it works!! Thank you so very much!! :D

So I used Sys.setenv(USE_CXX14=1) and had the same problem.
Updated R to the latest version 3.5 and still had the same problem.

I ran this code in a fresh session:

Sys.setenv(USE_CXX14=1)
library(rstan)


rstan_options(auto_write = TRUE)
schools_dat <- list(J = 8, 
                    y = c(28,  8, -3,  7, -1,  1, 18, 12),
                    sigma = c(15, 10, 16, 11,  9, 11, 10, 18))
fit <- stan(file = '8schools.stan', data = schools_dat, 
            iter = 1000, chains = 4)

And it generated these errors:

Show in New WindowClear OutputExpand/Collapse Output
Loading required package: ggplot2
Loading required package: StanHeaders
rstan (Version 2.17.4, 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)
StanHeaders version is ahead of rstan version; update to latest rstanrunning command '/Library/Frameworks/R.framework/Resources/bin/R CMD SHLIB filee98755dfa93.cpp 2> filee98755dfa93.cpp.err.txt' had status 1In file included from filee98755dfa93.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:
/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: *** [filee98755dfa93.o] Error 1

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

Error in compileCode(f, code, language = language, verbose = verbose) : Compilation ERROR, function(s)/method(s) not created! In file included from filee98755dfa93.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: /
6.
stop(paste("Compilation ERROR, function(s)/method(s) not created!", paste(errmsg, collapse = "\n")))
5.
compileCode(f, code, language = language, verbose = verbose)
4.
cxxfunction(sig = sig, body = body, plugin = plugin, includes = includes, settings = settings, ..., verbose = verbose)
3.
cxxfunctionplus(signature(), body = paste(" return Rcpp::wrap(\"", model_name, "\");", sep = ""), includes = inc, plugin = "rstan", save_dso = save_dso | auto_write, module_name = paste("stan_fit4", model_cppname, "_mod", sep = ""), verbose = verbose)
2.
stan_model(file, model_name = model_name, model_code = model_code, stanc_ret = NULL, boost_lib = boost_lib, eigen_lib = eigen_lib, save_dso = save_dso, verbose = verbose)
1.
stan(file = "8schools.stan", data = schools_dat, iter = 1000, chains = 4)

			

@stenknutsen Can you look in Spotlight or whatever to see where the stdlib.h file is on your system?

@bgoodri I found in in:

Users/stenknutsen/miniconda3/pkgs/libcxx-4.0.1-h579ed51_0/include/c++/v1

Yeah, Anaconda is known to mess things up royally. Someone with a Mac will have to help you.

Worked for me as well. What’s up, though?

Ok. So this does indeed seem to be a conda problem. I was able to get everything up and working on another mac without conda (using Sys.setenv(USE_CXX14=1)). I also noticed that on that machine the stdlib.h is in /usr/include.

Going back to the mac with the problem, I found another stdlib.h in /usr/include as well. How would I point Stan towards that stdlib.h?

You are going to need to add -isystem /usr/include to CXX14FLAGS in your ~/.R/Makevars file and then try again. Something else will inevitably be missing, so add its directory with another -isystem thing. Repeat.

Or setup your R environment outside of Conda.

1 Like

That seems to do the trick. Thanks @bgoodri !

Hi, I have a similar problem, but although the above posts have gotten me closer, I haven’t quite solved it. I get the same compiling error as Lux_Aeterna and stenknutsen. I do have anaconda installed, and there is a stdlib.h file in the conda environment, but the contents of my Makevars file looks like this:

# The following statements are required to use the clang4 binary
LDFLAGS= -L/usr/local/clang4/lib
# End clang4 inclusion statements
# The following statement changes the Fortran linking path
FLIBS=-L/usr/local/gfortran/lib/gcc/x86_64-apple-darwin16/6.3.0
# End Fortran linking path statement
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++
# The following statement changes the Fortran linking path
FLIBS=-L/usr/local/gfortran/lib/gcc/x86_64-apple-darwin16/6.3.0
# End Fortran linking path statement

There is also a stdlib.h file in /usr/local/clang4/bin/clang++/v1, but even after changing the Makevars file to: CXX14=/usr/local/clang4/bin/clang++/v1 I still get an error when running the standard toolchain check. I have put a pastebin of the output here:

I have also tried

CXX14=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1

where there is also a stdlib.h file, but still no luck.

Hope someone can help me, I am running out of ideas for how to solve this!

CXX14 should still be /usr/local/clang4/bin/clang++ but you need CXX14FLAGS=-O3 -isystem /usr/include or similar

Thanks for the reply. I have added CXX14FLAGS=-O3 -isystem /usr/include to the Makevars file, but the error persists. I’m not sure if it matters where in the Makevars file the line goes? The funny thing is, when I run pkgbuild::has_build_tools(debug = TRUE) it returns TRUE, which I guess should suggest that the toolchain is up and running correctly? But if I try to run this line that I got from @paul.buerkner fx <- inline::cxxfunction( signature(x = "integer", y = "numeric" ) , ' return ScalarReal( INTEGER(x)[0] * REAL(y)[0] ) ; I still get:

In file included from file60a734cbe40.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: *** [file60a734cbe40.o] Error 1

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

Program source:
1:
2: // includes from the plugin
3: #include <R.h>
4: #include <Rdefines.h>
5: #include <R_ext/Error.h>
6:
7:
8: // user includes
9:
10:
11: // declarations
12: extern “C” {
13: SEXP file60a734cbe40( SEXP x, SEXP y) ;
14: }
15:
16: // definition
17:
18: SEXP file60a734cbe40( SEXP x, SEXP y ){
19:
20: return ScalarReal( INTEGER(x)[0] * REAL(y)[0] ) ;
21:
22: Rf_warning(“your C++ program does not return anything”);
23: return R_NilValue ;
24: }
25:
26:
Error in compileCode(f, code, language = language, verbose = verbose) :
Compilation ERROR, function(s)/method(s) not created! In file included from file60a734cbe40.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: *** [file60a734cbe40.o] Error 1
In addition: Warning message:
In system(cmd, intern = !verbose) :
running command ‘/Library/Frameworks/R.framework/Resources/bin/R CMD SHLIB file60a734cbe40.cpp 2> file60a734cbe40.cpp.err.txt’ had status 1

I have also tried removing RStan and re-installing, as well as downgrading StanHeaders to 2.17.2, which helped a friend with his compilation woes, but not me.

You may need to run /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg once.

Ah, this has gotten me further!

For anyone else with a similar issue, although I already had the Xcode app installed, I needed to make a free Apple Developer account and download the Command_Line_Tools_macOS_10.14_for_Xcode_10.1.dmg file. Running the .dmg added the Packages directory to my /Library/Developer/CommandLineTools/ path. Then I needed to do sudo installer -pkg /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg -target /to actually install the package.

Now I no longer get the ‘stdlib.h’ file not found errors, and when I do

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

it returns 10, so that should be fine. This didn’t used to work. On the other hand, when I try:

library(brms)
fit <- brm(count ~ log_Age_c + log_Base4_c * Trt_c + (1|patient), data = epilepsy, family = poisson())

I get the following errors:

Compiling the C++ model
Error in compileCode(f, code, language = language, verbose = verbose) :
Compilation ERROR, function(s)/method(s) not created! In file included from filee4b86e906be6.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:12:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/StanHeaders/include/stan/math/rev/core/gevv_vvv_vari.hpp:5:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/StanHeaders/include/stan/math/rev/core/var.hpp:7:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/B
In addition: Warning message:
In system(cmd, intern = !verbose) :
running command ‘/Library/Frameworks/R.framework/Resources/bin/R CMD SHLIB filee4b86e906be6.cpp 2> filee4b86e906be6.cpp.err.txt’ had status 1
Error in sink(type = “output”) : invalid connection

But maybe this is now a bmrs problem, and not an RStan problem per se?

Thanks again for all the help!

It is still a configuration problem but it is probably easily resolved. Try doing

library(rstan)
stan_model(model_code = 'parameters { real theta; }', verbose = TRUE)

to see what the real error message is. It will involve the string error: (with a colon).

Alrighty. I ran

stan_model(model_code = 'parameters { real theta; }', verbose = TRUE)and got a lot of output. To save space here, I put it in a pastbin:

https://pastebin.com/1fceQ3iH

There are quite a few strings starting with error:, I’m afraid.

I just meant the first one. Usually the subsequent ones are redundant. In your case it is

/usr/local/clang4/bin/…/include/c++/v1/cmath:313:9: error: no member named ‘signbit’ in the global namespace
so I think it is still not finding the correct headers. What I would do next is

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

so that it falls back to clang++ from Xcode rather than clang++ from the clang4 installer.

1 Like

Yes yes yes!! That did the trick - thank you so much for all your help :-)

I had similar problem and I tried Sys.setenv(USE_CXX14=1), but, didn’t work. Any help? appreciated!

Error in compileCode(f, code, language = language, verbose = verbose) :
Compilation ERROR, function(s)/method(s) not created! In file included from file10a6e4e8c517a.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:5:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/StanHeaders/include/stan/math/rev/core/build_vari_array.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/
In addition: Warning message:
In system(cmd, intern = !verbose) :
running command ‘/Library/Frameworks/R.framework/Resources/bin/R CMD SHLIB file10a6e4e8c517a.cpp 2> file10a6e4e8c517a.cpp.err.txt’ had status 1
Error in sink(type = “output”) : invalid connection

That shouldn’t be necessary. We do need the output when specifying verbose = TRUE when calling stan or sampling to see the cause of the compilation error.