I have problems running STAN code on a Mac M1. Previously, everything worked as expected but then I needed to install llvm
with brew
to compile some other packages from source (I think that’s the cause of the problem). Now, I cannot compile STAN code anymore. I have been working on this for several days and messed around a lot with the Makevars
, but I don’t know what to do. If I understood correctly, there seems to be something going on with the links to libraries that are used during compilation.
I actually want to use brms
with the cmdstanr
backend, but it might be easier to figure out what’s going on with the rstan
package.
I try to run the rstan example example(stan_model, package = "rstan", run.dontrun = TRUE)
. It returns this error:
Compilation ERROR, function(s)/method(s) not created!
Error in compileCode(f, code, language = language, verbose = verbose) :
ineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/sstream:278:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/istream:170:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/ostream:179:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__memory/shared_ptr.h:33:
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__memory/unique_ptr.h:170:54: error: member 'nullptr_t' declared as a template
169 | template <bool _Dummy = true, class = _EnableIfDeleterDefaultConstructible<_Dummy> >
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
170 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT
| ^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
20 errors generated.
make: *** [file580d1fb2a771.o
Curenlty, my Makevars
are:
CXXFLAGS = -std=c++17 -isysroot $(shell xcrun --show-sdk-path)
LDFLAGS = -L$(xcrun --show-sdk-path)/usr/lib -L/usr/lib -Wl,-rpath,/usr/lib
CPPFLAGS = -I$(xcrun --show-sdk-path)/usr/include
I can compile packages from source, e.g. Rcpp
. I can also run Rcpp::evalCpp("2 + 2")
. In terminal, clang - v
returns:
Apple clang version 16.0.0 (clang-1600.0.26.4)
Target: arm64-apple-darwin24.1.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
In a fresh R session, pkgbuild::check_build_tools(debug = TRUE)
returns:
Trying to compile a simple C file
Running /Library/Frameworks/R.framework/Resources/bin/R CMD SHLIB foo.c
using C compiler: ‘Apple clang version 16.0.0 (clang-1600.0.26.4)’
using SDK: ‘MacOSX15.1.sdk’
clang -arch arm64 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I/usr/include -fPIC -falign-functions=64 -Wall -g -O2 -c foo.c -o foo.o
clang -arch arm64 -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -L/Library/Frameworks/R.framework/Resources/lib -L/usr/lib -L/usr/lib -Wl,-rpath,/usr/lib -o foo.so foo.o -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation
Your system is ready to build packages!
After unsuccessfully running the rstan
example, pkgbuild::check_build_tools(debug = TRUE)
returns:
Compilation ERROR, function(s)/method(s) not created!
Error in compileCode(f, code, language = language, verbose = verbose) :
ineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/sstream:278:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/istream:170:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/ostream:179:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__memory/shared_ptr.h:33:
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__memory/unique_ptr.h:170:54: error: member 'nullptr_t' declared as a template
169 | template <bool _Dummy = true, class = _EnableIfDeleterDefaultConstructible<_Dummy> >
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
170 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT
| ^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
20 errors generated.
make: *** [file5bd050511086.o
For additional info:
cmdstanr::check_cmdstan_toolchain()
returns:
The C++ toolchain required for CmdStan is setup properly!
cmdstanr::install_cmdstan(cores = 2, overwrite = T)
returns a very long list of errors, I guess the important part is like this:
--- Compiling pre-compiled header. This might take a few seconds. ---
In file included from src/cmdstan/main.cpp:1:
In file included from src/cmdstan/command.hpp:4:
In file included from src/cmdstan/arguments/arg_data.hpp:4:
In file included from src/cmdstan/arguments/categorical_argument.hpp:4:
In file included from src/cmdstan/arguments/argument.hpp:4:
In file included from stan/src/stan/callbacks/writer.hpp:4:
In file included from stan/lib/stan_math/stan/math/prim/fun/Eigen.hpp:22:
In file included from stan/lib/stan_math/lib/eigen_3.4.0/Eigen/Dense:1:
In file included from stan/lib/stan_math/lib/eigen_3.4.0/Eigen/Core:34:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/new:396:
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/cstdlib:90:5: error: <cstdlib> tried including <stdlib.h> but didn't find libc++'s <stdlib.h> header. This usually means that your header search paths are not configured properly. The header search paths should contain the C++ Standard Library headers before any C Standard Library, and you are probably using compiler flags that make that not be the case.
90 | # error <cstdlib> tried including <stdlib.h> but didn't find libc++'s <stdlib.h> header. \
| ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/cstdlib:132:9: error: target of using declaration conflicts with declaration already in scope
132 | using ::abs _LIBCPP_USING_IF_EXISTS;
| ^
/usr/local/include/_stdlib.h:124:6: note: target of using declaration
124 | int abs(int) __pure2;
| ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/cmath:355:1: note: conflicting declaration
355 | using ::abs _LIBCPP_USING_IF_EXISTS;
| ^