Compilation issues with Rstan 2.21.1, R 4.0.2 on MacOS Catalina

Apologies in advance if this is duplicative, but I can’t seem to find another post here with the exact same problem, though it seems others are running into similar things.

I recently upgraded to R 4.0.2 and reinstalled rstan, rstanarm, and cmdstanr from their CRAN binaries. But running
example(stan_model, run.dontrun=T) after loading rstan gives me

Compilation argument:
 /Library/Frameworks/R.framework/Resources/bin/R CMD SHLIB file745c506a04e8.cpp 2> file745c506a04e8.cpp.err.txt
clang++ -mmacosx-version-min=10.13 -std=gnu++14 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -Wno-unknown-pragmas  -I/usr/local/include   -include /Library/Frameworks/R.framework/Versions/4.0/Resources/library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp -I '/Library/Frameworks/R.framework/Versions/4.0/Resources/library/StanHeaders/include' -I '/Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppEigen/include' -fPIC  -O3 -mtune=native -march=native -Wno-unused-variable -Wno-unused-function -fPIC -c file745c506a04e8.cpp -o file745c506a04e8.o
file745c506a04e8.cpp:7:10: fatal error: 'Rcpp.h' file not found
#include <Rcpp.h>
         ^~~~~~~~
1 error generated.
make: *** [file745c506a04e8.o] Error 1

And when I try to run a model with cmdstanr, I get

Error in processx::run(command = make_cmd(), args = c(cpp_options_to_compile_flags(cpp_options),  :
  System command 'make' failed, exit status: 2, stderr:
E> /Users/cmccartan/.cmdstanr/cmdstan-2.22.1/src/cmdstan/main.cpp:1:10: fatal error: 'stan/services/error_codes.hpp' file not found
E> #include <stan/services/error_codes.hpp>
E>          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
E> 1 error generated.
E> make: *** [/Users/cmccartan/.cmdstanr/cmdstan-2.22.1/src/cmdstan/main.o] Error 1

I’m able to use Rcpp+Eigen in another package I’m developing, so I don’t think the problem is with them.
My Makevars are

PKG_CPPFLAGS = -Wno-unknown-pragmas
FC = /usr/local/bin/gfortran
FLIBS = -L/usr/local/gfortran/lib/gcc/x86_64-apple-darwin15/6.1.0 -L/usr/local/gfortran/lib -lgfortran -lquadmath -lm
CXX14FLAGS=-O3 -mtune=native -march=native -Wno-unused-variable -Wno-unused-function -fPIC

and I have nothing except API keys in my .Renviron. I’ve upgraded the Mac command line tools recently as well.

Other threads suggested I install StanHeaders by downloading a zip, deleting zzz.R, and running R CMD Build, but that complained about a missing cvodes.h file.

What is the result of

rstan:::rstanplugin()

?

It’s

$includes
[1] "// [[Rcpp::plugins(cpp14)]]\n"

$body
function (x)
x
<bytecode: 0x7fbd14114828>
<environment: 0x7fbd14015ba0>

$env
$env$PKG_LIBS
[1] " /Library/Frameworks/R.framework/Versions/4.0/Resources/library/rstan/lib//libStanServices.a -L'/Library/Frameworks/R.framework/Versions/4.0/Resources/library/StanHeaders/lib/' -lStanHeaders -L'/Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppParallel/lib/' -ltbb -ltbbmalloc -ltbbmalloc_proxy"

$env$PKG_CPPFLAGS
[1] "  -I\"/Library/Frameworks/R.framework/Versions/4.0/Resources/library/Rcpp/include/\"  -I\"/Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppEigen/include/\"  -I\"/Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppEigen/include/unsupported\"  -I\"/Library/Frameworks/R.framework/Versions/4.0/Resources/library/BH/include\" -I\"/Library/Frameworks/R.framework/Versions/4.0/Resources/library/StanHeaders/include/src/\"  -I\"/Library/Frameworks/R.framework/Versions/4.0/Resources/library/StanHeaders/include/\"  -I\"/Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppParallel/include/\"  -I\"/Library/Frameworks/R.framework/Versions/4.0/Resources/library/rstan/include\" -DEIGEN_NO_DEBUG  -DBOOST_DISABLE_ASSERTS  -DBOOST_PENDING_INTEGER_LOG2_HPP  -DSTAN_THREADS  -DBOOST_NO_AUTO_PTR  -include '/Library/Frameworks/R.framework/Versions/4.0/Resources/library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp'  -D_REENTRANT -DRCPP_PARALLEL_USE_TBB=1 "

That line in your ~/.R/Makevars file is overriding PKG_CPPFLAGS for the Stan program. You can put that -Wno-unknown-pragmas into CXX14FLAGS.

Ah, thank you so much! That’s fixed the problem.