Problem with C++ Compiler on Mac

I’m having a problem getting rstan to work due to an issue with the C++ compiler. I’m running OS X 10.14 Mojave with Xcode 10.0, clang 6.0, R 3.5.1, Rstudio 1.1.456, Rstan 2.18.1, gfortran 6.1.

I’ve run the exact same code on colleagues’ computers who have followed the same steps to install the requisite packages etc and it worked fine for them. I’m at a loss as to what to do.

My model is very basic:

data{
int N;
real y[N];
}

parameters{
real mu;
real<lower=0> sigma;
}

model{
y ~ normal(mu, sigma);
}

When I try to call stan, I get the following error:

mod1fit ← stan(“mod1.stan”, data=list(N=N, y=y), iter=1000, warmup=500, chains=3)
In file included from file231a5cb7c026.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/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: *** [file231a5cb7c026.o] Error 1

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

Program source:
1:
2: // includes from the plugin
3: // [[Rcpp::plugins(cpp14)]]
4:
5: // user includes
6: #define STAN__SERVICES__COMMAND_HPP// Code generated by Stan version 2.18.0
7:
8: #include <stan/model/model_header.hpp>
9:
10: namespace model231a3d907a56_mod1_namespace {
11:
12: using std::istream;
13: using std::string;
14: using std::stringstream;
15: using std::vector;
16: using stan::io::dump;
17: using stan::math::lgamma;
18: using stan::model::prob_grad;
19: using namespace stan::math;
20:
21: static int current_statement_begin__;
22:
23: stan::io::program_reader prog_reader__() {
24: stan::io::program_reader reader;
25: reader.add_event(0, 0, “start”, “model231a3d907a56_mod1”);
26: reader.add_event(15, 13, “end”, “model231a3d907a56_mod1”);
27: return reader;
28: }
29:
30: class model231a3d907a56_mod1 : public prob_grad {
31: private:
32: int N;
33: vector y;
34: public:
35: model231a3d907a56_mod1(stan::io::var_context& context__,
36: std::ostream* pstream__ = 0)
37: : prob_grad(0) {
38: ctor_body(context__, 0, pstream__);
39: }
40:
41: model231a3d907a56_mod1(stan::io::var_context& context__,
42: unsigned int random_seed__,
43: std::ostream* pstream__ = 0)
44: : prob_grad(0) {
45: ctor_body(context__, random_seed__, pstream__);
46: }
47:
48: void ctor_body(stan::io::var_context& context__,
49: unsigned int random_seed__,
50: std::ostream* pstream__) {
51: typedef double local_scalar_t__;
52:
53: boost::ecuyer1988 base_rng__ =
54: stan::services::util::create_rng(random_seed__, 0);
55: (void) base_rng__; // suppress unused var warning
56:
57: current_statement_begin__ = -1;
58:
59: static const char* function__ = “model231a3d907a56_mod1_namespace::model231a3d907a56_mod1”;
60: (void) function__; // dummy to suppress unused var warning
61: size_t pos__;
62: (void) pos__; // dummy to suppress unused var warning
63: std::vector vals_i__;
64: std::vector vals_r__;
65: local_scalar_t__ DUMMY_VAR__(std::numeric_limits::quiet_NaN());
66: (void) DUMMY_VAR__; // suppress unused var warning
67:
68: // initialize member variables
69: try {
70: current_statement_begin__ = 2;
71: context__.validate_dims(“data initialization”, “N”, “int”, context__.to_vec());
72: N = int(0);
73: vals_i__ = context__.vals_i(“N”);
74: pos__ = 0;
75: N = vals_i__[pos__++];
76: current_statement_begin__ = 3;
77: validate_non_negative_index(“y”, “N”, N);
78: context__.validate_dims(“data initialization”, “y”, “double”, context__.to_vec(N));
79: validate_non_negative_index(“y”, “N”, N);
80: y = std::vector(N,double(0));
81: vals_r__ = context__.vals_r(“y”);
82: pos__ = 0;
83: size_t y_limit_0__ = N;
84: for (size_t i_0__ = 0; i_0__ < y_limit_0__; ++i_0__) {
85: y[i_0__] = vals_r__[pos__++];
86: }
87:
88: // validate, data variables
89: current_statement_begin__ = 2;
90: current_statement_begin__ = 3;
91: // initialize data variables
92:
93:
94: // validate transformed data
95:
96: // validate, set parameter ranges
97: num_params_r__ = 0U;
98: param_ranges_i__.clear();
99: current_statement_begin__ = 7;
100: ++num_params_r__;
101: current_statement_begin__ = 8;
102: ++num_params_r__;
103: } catch (const std::exception& e) {
104: stan::lang::rethrow_located(e, current_statement_begin__, prog_reader__());
105: // Next line prevents compiler griping about no return
106: throw std::runtime_error(“*** IF YOU SEE THIS, PLEASE REPORT A BUG ");
107: }
108: }
109:
110: ~model231a3d907a56_mod1() { }
111:
112:
113: void transform_inits(const stan::io::var_context& context__,
114: std::vector& params_i__,
115: std::vector& params_r__,
116: std::ostream
pstream__) const {
117: stan::io::writer writer__(params_r__,params_i__);
118: size_t pos__;
119: (void) pos__; // dummy call to supress warning
120: std::vector vals_r__;
121: std::vector vals_i__;
122:
123: if (!(context__.contains_r(“mu”)))
124: throw std::runtime_error(“variable mu missing”);
125: vals_r__ = context__.vals_r(“mu”);
126: pos__ = 0U;
127: context__.validate_dims(“initialization”, “mu”, “double”, context__.to_vec());
128: double mu(0);
129: mu = vals_r__[pos__++];
130: try {
131: writer__.scalar_unconstrain(mu);
132: } catch (const std::exception& e) {
133: throw std::runtime_error(std::string("Error transforming variable mu: ") + e.what());
134: }
135:
136: if (!(context__.contains_r(“sigma”)))
137: throw std::runtime_error(“variable sigma missing”);
138: vals_r__ = context__.vals_r(“sigma”);
139: pos__ = 0U;
140: context__.validate_dims(“initialization”, “sigma”, “double”, context__.to_vec());
141: double sigma(0);
142: sigma = vals_r__[pos__++];
143: try {
144: writer__.scalar_lb_unconstrain(0,sigma);
145: } catch (const std::exception& e) {
146: throw std::runtime_error(std::string("Error transforming variable sigma: ") + e.what());
147: }
148:
149: params_r__ = writer__.data_r();
150: params_i__ = writer__.data_i();
151: }
152:
153: void transform_inits(const stan::io::var_context& context,
154: Eigen::Matrix<double,Eigen::Dynamic,1>& params_r,
155: std::ostream
pstream__) const {
156: std::vector params_r_vec;
157: std::vector params_i_vec;
158: transform_inits(context, params_i_vec, params_r_vec, pstream__);
159: params_r.resize(params_r_vec.size());
160: for (int i = 0; i < params_r.size(); ++i)
161: params_r(i) = params_r_vec[i];
162: }
163:
164:
165: template <bool propto__, bool jacobian__, typename T__>
166: T__ log_prob(vector<T__>& params_r__,
167: vector& params_i__,
168: std::ostream
pstream__ = 0) const {
169:
170: typedef T__ local_scalar_t__;
171:
172: local_scalar_t__ DUMMY_VAR__(std::numeric_limits::quiet_NaN());
173: (void) DUMMY_VAR__; // suppress unused var warning
174:
175: T__ lp__(0.0);
176: stan::math::accumulator<T__> lp_accum__;
177:
178: try {
179: // model parameters
180: stan::io::reader<local_scalar_t__> in__(params_r__,params_i__);
181:
182: local_scalar_t__ mu;
183: (void) mu; // dummy to suppress unused var warning
184: if (jacobian__)
185: mu = in__.scalar_constrain(lp__);
186: else
187: mu = in__.scalar_constrain();
188:
189: local_scalar_t__ sigma;
190: (void) sigma; // dummy to suppress unused var warning
191: if (jacobian__)
192: sigma = in__.scalar_lb_constrain(0,lp__);
193: else
194: sigma = in__.scalar_lb_constrain(0);
195:
196:
197: // transformed parameters
198:
199:
200:
201: // validate transformed parameters
202:
203: const char* function__ = “validate transformed params”;
204: (void) function__; // dummy to suppress unused var warning
205:
206: // model body
207:
208: current_statement_begin__ = 12;
209: lp_accum__.add(normal_log<propto__>(y, mu, sigma));
210:
211: } catch (const std::exception& e) {
212: stan::lang::rethrow_located(e, current_statement_begin__, prog_reader__());
213: // Next line prevents compiler griping about no return
214: throw std::runtime_error(”*** IF YOU SEE THIS, PLEASE REPORT A BUG ");
215: }
216:
217: lp_accum__.add(lp__);
218: return lp_accum__.sum();
219:
220: } // log_prob()
221:
222: template <bool propto, bool jacobian, typename T_>
223: T_ log_prob(Eigen::Matrix<T_,Eigen::Dynamic,1>& params_r,
224: std::ostream
pstream = 0) const {
225: std::vector<T_> vec_params_r;
226: vec_params_r.reserve(params_r.size());
227: for (int i = 0; i < params_r.size(); ++i)
228: vec_params_r.push_back(params_r(i));
229: std::vector vec_params_i;
230: return log_prob<propto,jacobian,T_>(vec_params_r, vec_params_i, pstream);
231: }
232:
233:
234: void get_param_names(std::vectorstd::string& names__) const {
235: names__.resize(0);
236: names__.push_back(“mu”);
237: names__.push_back(“sigma”);
238: }
239:
240:
241: void get_dims(std::vector<std::vector<size_t> >& dimss__) const {
242: dimss__.resize(0);
243: std::vector<size_t> dims__;
244: dims__.resize(0);
245: dimss__.push_back(dims__);
246: dims__.resize(0);
247: dimss__.push_back(dims__);
248: }
249:
250: template
251: void write_array(RNG& base_rng__,
252: std::vector& params_r__,
253: std::vector& params_i__,
254: std::vector& vars__,
255: bool include_tparams__ = true,
256: bool include_gqs__ = true,
257: std::ostream
pstream__ = 0) const {
258: typedef double local_scalar_t__;
259:
260: vars__.resize(0);
261: stan::io::reader<local_scalar_t__> in__(params_r__,params_i__);
262: static const char
function__ = “model231a3d907a56_mod1_namespace::write_array”;
263: (void) function__; // dummy to suppress unused var warning
264: // read-transform, write parameters
265: double mu = in__.scalar_constrain();
266: double sigma = in__.scalar_lb_constrain(0);
267: vars__.push_back(mu);
268: vars__.push_back(sigma);
269:
270: // declare and define transformed parameters
271: double lp__ = 0.0;
272: (void) lp__; // dummy to suppress unused var warning
273: stan::math::accumulator lp_accum__;
274:
275: local_scalar_t__ DUMMY_VAR__(std::numeric_limits::quiet_NaN());
276: (void) DUMMY_VAR__; // suppress unused var warning
277:
278: try {
279:
280:
281:
282: // validate transformed parameters
283:
284: // write transformed parameters
285: if (include_tparams__) {
286: }
287: if (!include_gqs__) return;
288: // declare and define generated quantities
289:
290:
291:
292: // validate generated quantities
293:
294: // write generated quantities
295: } catch (const std::exception& e) {
296: stan::lang::rethrow_located(e, current_statement_begin__, prog_reader__());
297: // Next line prevents compiler griping about no return
298: throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ");
299: }
300: }
301:
302: template
303: void write_array(RNG& base_rng,
304: Eigen::Matrix<double,Eigen::Dynamic,1>& params_r,
305: Eigen::Matrix<double,Eigen::Dynamic,1>& vars,
306: bool include_tparams = true,
307: bool include_gqs = true,
308: std::ostream
pstream = 0) const {
309: std::vector params_r_vec(params_r.size());
310: for (int i = 0; i < params_r.size(); ++i)
311: params_r_vec[i] = params_r(i);
312: std::vector vars_vec;
313: std::vector params_i_vec;
314: write_array(base_rng,params_r_vec,params_i_vec,vars_vec,include_tparams,include_gqs,pstream);
315: vars.resize(vars_vec.size());
316: for (int i = 0; i < vars.size(); ++i)
317: vars(i) = vars_vec[i];
318: }
319:
320: static std::string model_name() {
321: return “model231a3d907a56_mod1”;
322: }
323:
324:
325: void constrained_param_names(std::vectorstd::string& param_names__,
326: bool include_tparams__ = true,
327: bool include_gqs__ = true) const {
328: std::stringstream param_name_stream__;
329: param_name_stream__.str(std::string());
330: param_name_stream__ << “mu”;
331: param_names__.push_back(param_name_stream__.str());
332: param_name_stream__.str(std::string());
333: param_name_stream__ << “sigma”;
334: param_names__.push_back(param_name_stream__.str());
335:
336: if (!include_gqs__ && !include_tparams__) return;
337:
338: if (include_tparams__) {
339: }
340:
341:
342: if (!include_gqs__) return;
343: }
344:
345:
346: void unconstrained_param_names(std::vectorstd::string& param_names__,
347: bool include_tparams__ = true,
348: bool include_gqs__ = true) const {
349: std::stringstream param_name_stream__;
350: param_name_stream__.str(std::string());
351: param_name_stream__ << “mu”;
352: param_names__.push_back(param_name_stream__.str());
353: param_name_stream__.str(std::string());
354: param_name_stream__ << “sigma”;
355: param_names__.push_back(param_name_stream__.str());
356:
357: if (!include_gqs__ && !include_tparams__) return;
358:
359: if (include_tparams__) {
360: }
361:
362:
363: if (!include_gqs__) return;
364: }
365:
366: }; // model
367:
368: }
369:
370: typedef model231a3d907a56_mod1_namespace::model231a3d907a56_mod1 stan_model;
371:
372: #include <rstan/rstaninc.hpp>
373: /

374: * Define Rcpp Module to expose stan_fit’s functions to R.
375: */
376: RCPP_MODULE(stan_fit4model231a3d907a56_mod1_mod){
377: Rcpp::class_<rstan::stan_fit<model231a3d907a56_mod1_namespace::model231a3d907a56_mod1,
378: boost::random::ecuyer1988> >(“stan_fit4model231a3d907a56_mod1”)
379: // .constructorRcpp::List()
380: .constructor<SEXP, SEXP, SEXP>()
381: // .constructor<SEXP, SEXP>()
382: .method(“call_sampler”,
383: &rstan::stan_fit<model231a3d907a56_mod1_namespace::model231a3d907a56_mod1, boost::random::ecuyer1988>::call_sampler)
384: .method(“param_names”,
385: &rstan::stan_fit<model231a3d907a56_mod1_namespace::model231a3d907a56_mod1, boost::random::ecuyer1988>::param_names)
386: .method(“param_names_oi”,
387: &rstan::stan_fit<model231a3d907a56_mod1_namespace::model231a3d907a56_mod1, boost::random::ecuyer1988>::param_names_oi)
388: .method(“param_fnames_oi”,
389: &rstan::stan_fit<model231a3d907a56_mod1_namespace::model231a3d907a56_mod1, boost::random::ecuyer1988>::param_fnames_oi)
390: .method(“param_dims”,
391: &rstan::stan_fit<model231a3d907a56_mod1_namespace::model231a3d907a56_mod1, boost::random::ecuyer1988>::param_dims)
392: .method(“param_dims_oi”,
393: &rstan::stan_fit<model231a3d907a56_mod1_namespace::model231a3d907a56_mod1, boost::random::ecuyer1988>::param_dims_oi)
394: .method(“update_param_oi”,
395: &rstan::stan_fit<model231a3d907a56_mod1_namespace::model231a3d907a56_mod1, boost::random::ecuyer1988>::update_param_oi)
396: .method(“param_oi_tidx”,
397: &rstan::stan_fit<model231a3d907a56_mod1_namespace::model231a3d907a56_mod1, boost::random::ecuyer1988>::param_oi_tidx)
398: .method(“grad_log_prob”,
399: &rstan::stan_fit<model231a3d907a56_mod1_namespace::model231a3d907a56_mod1, boost::random::ecuyer1988>::grad_log_prob)
400: .method(“log_prob”,
401: &rstan::stan_fit<model231a3d907a56_mod1_namespace::model231a3d907a56_mod1, boost::random::ecuyer1988>::log_prob)
402: .method(“unconstrain_pars”,
403: &rstan::stan_fit<model231a3d907a56_mod1_namespace::model231a3d907a56_mod1, boost::random::ecuyer1988>::unconstrain_pars)
404: .method(“constrain_pars”,
405: &rstan::stan_fit<model231a3d907a56_mod1_namespace::model231a3d907a56_mod1, boost::random::ecuyer1988>::constrain_pars)
406: .method(“num_pars_unconstrained”,
407: &rstan::stan_fit<model231a3d907a56_mod1_namespace::model231a3d907a56_mod1, boost::random::ecuyer1988>::num_pars_unconstrained)
408: .method(“unconstrained_param_names”,
409: &rstan::stan_fit<model231a3d907a56_mod1_namespace::model231a3d907a56_mod1, boost::random::ecuyer1988>::unconstrained_param_names)
410: .method(“constrained_param_names”,
411: &rstan::stan_fit<model231a3d907a56_mod1_namespace::model231a3d907a56_mod1, boost::random::ecuyer1988>::constrained_param_names)
412: .method(“standalone_gqs”,
413: &rstan::stan_fit<model231a3d907a56_mod1_namespace::model231a3d907a56_mod1, boost::random::ecuyer1988>::standalone_gqs)
414: ;
415: }
416:
417: // declarations
418: extern “C” {
419: SEXP file231a5cb7c026( ) ;
420: }
421:
422: // definition
423:
424: SEXP file231a5cb7c026( ){
425: return Rcpp::wrap(“mod1”);
426: }
427:
428:
Error in compileCode(f, code, language = language, verbose = verbose) :
Compilation ERROR, function(s)/method(s) not created! In file included from file231a5cb7c026.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/clang6/include/c++/v1/cstdlib:86:
/usr/lo
In addition: Warning message:
In system(cmd, intern = !verbose) :
running command ‘/Library/Frameworks/R.framework/Resources/bin/R CMD SHLIB file231a5cb7c026.cpp 2> file231a5cb7c026.cpp.err.txt’ had status 1

This has come up a couple of times. Are you using Anaconda? If so, the first link here may help.

Also, maybe

thanks, i followed your advice, and when I went to edit it I realized that for some reason R wasn’t generating a Makevars file, so I manually created one and now everything is working fine.