C++ problem in Mac OS Sierra

Hi,

I am having trouble getting RStan to work on Mac OS Sierra and, after many attempts, google searches and double-checking against my well working RStan on a (different) Windows machine, I think the problem lies with C++.

The OS Sierra version is 10.12.6
Xcode version 8.3.3 (when installing it I opened it once and accepted the license agreement)
R version 3.4.1
RStan version 2.16.2 (seems to me to be well installed)

In the old google group mailing list there was an apparently related problem (https://groups.google.com/forum/#!topic/stan-users/8lG3y_kU6ao), but it didn’t work for me here, maybe because that other issue concerned Windows.

After following the instructions there, my Makevars file currently looks like this (it seems that this is what solved the problem reported in that conversation):
CXXFLAGS=-O3 -mtune=native -march=native -Wno-unused-variable -Wno-unused-function -Wno-macro-redefined

But when I try to run a stan() model I get the following error message:

Error in if (nchar(CXX) == 0) { : argument is of length zero
In addition: Warning messages:
1: In readLines(file, warn = TRUE) :
** incomplete final line found on ‘/Users/userInvite2/Documents/Evangelia/glmm_model.stan’**
2: running command ‘’/Library/Frameworks/R.framework/Resources/bin/R’ CMD config CXX 2>/dev/null’ had status 1

When I type in rstan:::get_CXX() I get the following output:
character(0)
attr(,“status”)
[1] 1
Warning message:
running command ‘’/Library/Frameworks/R.framework/Resources/bin/R’ CMD config CXX 2>/dev/null’ had status 1

I tried to uninstall and reinstall everything (R, RStan, even the OS), following the instructions on https://github.com/stan-dev/rstan/wiki/Installing-RStan-on-Mac-or-Linux , but nothing worked.

I’d really appreciate any help, thanks in advance

Have you tried with no makevars file?

No I haven’t. I assumed it was necessary given the instructions on the Stan Github page.

I can try now though

You might ultimately want something in there but for troubleshooting it’s fine to check without.

Might also be useful to see what R CMD config XXX does when run from the command line.

CXX not XXX

OK, I removed the Makevars file, uninstalled RStan just in case, closed R, reopened and installed the package.
The same error message appeared.

Here’s the output when I ran that command in terminal:

Last login: Tue Sep 12 11:32:46 on console
ogerd289:~ userInvite2$ R CMD config CXX
xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun
xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun
xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun
xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun
xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun
xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun

That looks like xcode did not install properly or R can’t find xcode. Can
you manually at the terminal see if xcrun is there (or just search for it
globally with spotlight to see if it’s at a different path?)

I don’t have xcrun, it appears.

What should I do? Is it really the installation of Xcode that didn’t work well?

Xcode is either not fully installed or at a different path. If you
searched globally (either spotlight or locate) and found nothing for xcrun
I would try the solutions at this link and the “missing tools” link
therein:

I had a mac for a while for Stan development and it worked great so my
guess is maybe there are more steps to the xcode install than there used to
be or something minor like that. I used to install everything through brew
and I recall the one problem initially was getting paths in place correctly.

Success!
I followed the instructions there and now I have a model running, looks fine.

What should I do about the Makevars file? Currently I have none, should I remake it?

Really appreciate your help, thanks a lot!

Super, you want the minimal one, so the easiest way to check is to compile a model without a Makevars but with the verbose flag. Check that the call to the compiler (clang++ I think) has a -O3 flag for the model compilation. I think the mtune native flag is fine (it should be the default anyway) as long as it’s not causing models to crash. The other flags should be unnecessary, though they might hide irrelevant warnings.

@bgoodri should be able to say what the official recommendation is.

So this line, basically?

cat("\nCXXFLAGS=-O3 -mtune=native -march=native -Wno-unused-variable -Wno-unused-function", **
** file = M, sep = “\n”, append = TRUE)

Yes, the compiler is clang++ if I’m not wrong. The line rstan:::get_CXX() gives me:
> rstan:::get_CXX()
[1] “clang++”

In the verbose output there should be an actual call to clang++ that looks like:

‘’‘
clang++ … … -O3 … model_name.hpp …
’’’

But I’m on a phone rn and don’t recall if rstan mangles model names.

Yes

I’m not sure this is what you meant, but when I ran the model, at the beginning it said the following:

In file included from file3e07cdbc2b1.cpp:8:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/src/stan/model/model_header.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/mat.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/core.hpp:12:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/core/gevv_vvv_vari.hpp:5:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/core/var.hpp:7:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/BH/include/boost/math/tools/config.hpp:13:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/BH/include/boost/config.hpp:39:
/Library/Frameworks/R.framework/Versions/3.4/Resources/library/BH/include/boost/config/compiler/clang.hpp:200:11: warning: ‘BOOST_NO_CXX11_RVALUE_REFERENCES’ macro redefined [-Wmacro-redefined]
#define BOOST_NO_CXX11_RVALUE_REFERENCES
^
:6:9: note: previous definition is here
#define BOOST_NO_CXX11_RVALUE_REFERENCES 1
^
1 warning generated.

Then the model ran normally until the end.

The chains (4, 3000 iterations each) look good, as well as the Rhat, so everything seems fine. Plus the results seem reasonable.

But I didn’t find the line you were expecting (clang++ … … -03 … ).

Can I rely on this model or should I change / modify anything else in terms of compiler etc.?

The model is fine, it looks like rstan doesn’t print the line I was looking for out and I don’t recall right now how to get it. If you don’t have a Makevars it’s possible the -O3 flag will not make it into your compilation flags and your model will run slower than it needs to. I’d just try it with the makevars as recommended in the rstan install instructions now that you have xcode in place.

Got it. Perfect.
And again thanks a lot for the help!

You’re welcome, I’m glad it works.