Compilation error on Mac

Hi everyone, I’m new to RStan (just learned about it today). I encountered this frustrating problem on my day 1 with RStan, and I couldn’t find any solution online that works for me. Would really appreciate some help please. Thank you so much!!

I tried installing it on my Mac. I followed every step of the installation instructions described here: https://github.com/stan-dev/rstan/wiki/Installing-RStan-on-Mac-or-Linux and it seems that there was no problem.
However, when I tried running it in RStudio, I got the following error message:

Error in compileCode(f, code, language = language, verbose = verbose) : 
Compilation ERROR, function(s)/method(s) not created! In file included from file3d656890f1.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:
/Library/Frameworks/R.framework/Versions/3.5/Resources/library/StanHeaders/include/stan/math/memory/stack_alloc.hpp:145:10: warning: 'auto' type specifier is a C++11 extension [-Wc++11-extensions]
  for (auto& block

In addition: Warning messages:
1: In stan_model(file, model_name = model_name, model_code = model_code,  :
StanHeaders version is ahead of rstan version; update to latest rstan
2: In system(cmd, intern = !verbose) :
running command '/Library/Frameworks/R.framework/Resources/bin/R CMD SHLIB file3d656890f1.cpp 2> file3d656890f1.cpp.err.txt' had status 1

Operating System

macOS Sierra 10.12.6
RStudio 1.1.456

RStan Version

2.17.4

Output of writeLines(readLines(file.path(Sys.getenv("HOME"), ".R/Makevars")))

# 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
  # 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
CXXFLAGS=-O3 -mtune=native -march=native -Wno-unused-variable -Wno-unused-function  -Wno-macro-redefined -Wno-unknown-pragmas

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

Output of devtools::session_info("rstan")

Session info -----------------------------------------------------------------------------------------------
 setting  value                       
 version  R version 3.5.1 (2018-07-02)
 system   x86_64, darwin15.6.0        
 ui       RStudio (1.1.456)           
 language (EN)                        
 collate  en_CA.UTF-8                 
 tz       Europe/Berlin               
 date     2018-10-09                  

Packages ---------------------------------------------------------------------------------------------------
 package      * version   date       source        
 assertthat     0.2.0     2017-04-11 CRAN (R 3.5.0)
 BH             1.66.0-1  2018-02-13 CRAN (R 3.5.0)
 cli            1.0.1     2018-09-25 CRAN (R 3.5.0)
 colorspace     1.3-2     2016-12-14 CRAN (R 3.5.0)
 crayon         1.3.4     2017-09-16 CRAN (R 3.5.0)
 digest         0.6.17    2018-09-12 CRAN (R 3.5.0)
 fansi          0.4.0     2018-10-05 CRAN (R 3.5.0)
 ggplot2      * 3.0.0     2018-07-03 CRAN (R 3.5.0)
 glue           1.3.0     2018-07-17 CRAN (R 3.5.0)
 graphics     * 3.5.1     2018-07-05 local         
 grDevices    * 3.5.1     2018-07-05 local         
 grid           3.5.1     2018-07-05 local         
 gridExtra      2.3       2017-09-09 CRAN (R 3.5.0)
 gtable         0.2.0     2016-02-26 CRAN (R 3.5.0)
 inline         0.3.15    2018-05-18 CRAN (R 3.5.0)
 labeling       0.3       2014-08-23 CRAN (R 3.5.0)
 lattice        0.20-35   2017-03-25 CRAN (R 3.5.1)
 lazyeval       0.2.1     2017-10-29 CRAN (R 3.5.0)
 magrittr       1.5       2014-11-22 CRAN (R 3.5.0)
 MASS           7.3-50    2018-04-30 CRAN (R 3.5.1)
 Matrix         1.2-14    2018-04-13 CRAN (R 3.5.1)
 methods      * 3.5.1     2018-07-05 local         
 mgcv           1.8-24    2018-06-23 CRAN (R 3.5.1)
 munsell        0.5.0     2018-06-12 CRAN (R 3.5.0)
 nlme           3.1-137   2018-04-07 CRAN (R 3.5.1)
 pillar         1.3.0     2018-07-14 CRAN (R 3.5.0)
 plyr           1.8.4     2016-06-08 CRAN (R 3.5.0)
 R6             2.3.0     2018-10-04 CRAN (R 3.5.0)
 RColorBrewer   1.1-2     2014-12-07 CRAN (R 3.5.0)
 Rcpp           0.12.19   2018-10-01 CRAN (R 3.5.0)
 RcppEigen      0.3.3.4.0 2018-02-07 CRAN (R 3.5.0)
 reshape2       1.4.3     2017-12-11 CRAN (R 3.5.0)
 rlang          0.2.2     2018-08-16 CRAN (R 3.5.0)
 rstan        * 2.17.4    2018-10-01 CRAN (R 3.5.0)
 scales         1.0.0     2018-08-09 CRAN (R 3.5.0)
 StanHeaders  * 2.18.0    2018-10-07 CRAN (R 3.5.0)
 stats        * 3.5.1     2018-07-05 local         
 stats4         3.5.1     2018-07-05 local         
 stringi        1.2.4     2018-07-20 CRAN (R 3.5.0)
 stringr        1.3.1     2018-05-10 CRAN (R 3.5.0)
 tibble         1.4.2     2018-01-22 CRAN (R 3.5.0)
 tools          3.5.1     2018-07-05 local         
 utf8           1.1.4     2018-05-24 CRAN (R 3.5.0)
 utils        * 3.5.1     2018-07-05 local         
 viridisLite    0.3.0     2018-02-01 CRAN (R 3.5.0)
 withr          2.1.2     2018-03-15 CRAN (R 3.5.0)
1 Like

You are going to first need to call Sys.setenv(USE_CXX14=1) for the time being.

2 Likes

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 :-)