Please help -
I am trying to run a stan model (through HBayesDM) and I keep running into this error message. When the run the function:
hBayesDM::ts_par7(data = "example", niter=2000, nwarmup=1000, nchain=4, ncore=4)
it produces the error:
Error in compileCode(f, code, language = language, verbose = verbose) :
In file included from <built-in>:1:In file included from /Library/Frameworks/R.framework/Versions/4.2/Resources/library/StanHeaders/include/stan/math/prim/fun/Eigen.hpp:22:In file included from /Library/Frameworks/R.framework/Versions/4.2/Resources/library/RcppEigen/include/Eigen/Dense:1:In file included from /Library/Frameworks/R.framework/Versions/4.2/Resources/library/RcppEigen/include/Eigen/Core:82:In file included from /usr/local/opt/llvm/bin/../include/c++/v1/new:92:In file included from /usr/local/opt/llvm/bin/../include/c++/v1/cstdlib:85:/usr/local/opt/llvm/bin/../include/c++/v1/stdlib.h:93:15: fatal error: 'stdlib.h' file not found#include_next <stdlib.h> ^~~~~~~~~~1 error generated.make: *** [file41a1630639ae.o] Error 1
Error in sink(type = "output") : invalid connection
Here is my r session info:
R version 4.2.3 (2023-03-15)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Ventura 13.5
Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRlapack.dylib
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] rstanarm_2.21.3 Rcpp_1.0.11 rstan_2.32.3 StanHeaders_2.32.2
Any help much appreciated!
Does the error message occur specifically with hBayesDM, or does it occur with anything involving rstan? Are you able to run this: example(stan_model, package = "rstan", run.dontrun = TRUE)?
When I searched through Discourse with “Mac M2”, I found quite a few threads with error messages similar to yours, e.g. this thread may be useful.
Thanks for this Frank -
When I follow the thread you showed it gives some useful instructions to reinstall rstan from source, however, I encounter this error when compiling Rcppeigen:
ld: warning: -single_module is obsolete
ld: warning: -multiply_defined is obsolete
ld: warning: -single_module is obsolete
ld: warning: -multiply_defined is obsolete
ld: warning: search path '/usr/local/opt/qt@5/lib' not found
ld: warning: search path '/usr/local/gfortran/lib/gcc/x86_64-apple-darwin18/8.2.0' not found
ld: warning: search path '/usr/local/gfortran/lib' not found
ld: library 'gfortran' not found
clang-14: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [RcppEigen.so] Error 1
ERROR: compilation failed for package ‘RcppEigen’
* removing ‘/Library/Frameworks/R.framework/Versions/4.2/Resources/library/RcppEigen’
* restoring previous ‘/Library/Frameworks/R.framework/Versions/4.2/Resources/library/RcppEigen’
Warning in install.packages :
installation of package ‘RcppEigen’ had non-zero exit status
* installing *source* package ‘rstan’ ...
** package ‘rstan’ successfully unpacked and MD5 sums checked
** using staged installation
** libs
Error: package ‘RcppEigen’ 0.3.3.9.4 was found, but >= 0.3.4.0.0 is required by ‘rstan’
* removing ‘/Library/Frameworks/R.framework/Versions/4.2/Resources/library/rstan’
Warning in install.packages :
installation of package ‘rstan’ had non-zero exit status
I did try and reinstall ‘gfortran’ but I get told that it isn’t available for my version of R…
I’ve upgraded my R source, but I still get this message after running the line you asked me to try out…
Compilation ERROR, function(s)/method(s) not created!
Error in compileCode(f, code, language = language, verbose = verbose) :
using C++ compiler: ‘Homebrew clang version 14.0.6’using C++17using SDK: ‘’clang-14: warning: include path for libstdc++ headers not found; pass '-stdlib=libc++' on the command line to use the libc++ standard library instead [-Wstdlibcxx-not-found]In file included from <built-in>:1:In file included from /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/StanHeaders/include/stan/math/prim/fun/Eigen.hpp:22:In file included from /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/RcppEigen/include/Eigen/Dense:1:In file included from /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/RcppEigen/include/Eigen/Core:19:/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/RcppEigen/include/Eigen/src/Core/util/Macros.h:679:10: fatal error: 'cmath' file not found#include <cmath> ^~~~~~~1 error generated.make: *** [file6c7b2c424646.o] Error 1
Sorry that this isn’t working for you. The error message about RcppEigen suggests you might need to re-install some other packages like Rcpp, which rstan depends on. But I am by no means an expert on this. Perhaps one of the Stan developers like @andrjohns would be able to help?
It looks like you’re using a custom tool chain (Homebrew) instead of Apple’s. How have you configured the tool chain to be used? It looks like there’s a misconfiguration somewhere
Thanks for this - Ah I see. I hadn’t considered this would be conflicting. How would I configure the tool chain to be appropriate and reset the configuration?
How have you set R to use the Homebrew toolchain?
I have now uninstalled it:
brew info --cask rstudio
rstudio: 2022.07.0,548
https://www.rstudio.com/
Not installed
From: https://github.com/caskroom/homebrew-cask/blob/HEAD/Casks/rstudio.rb
==> Name
RStudio
==> Description
Data science software focusing on R and Python
==> Artifacts
RStudio.app (App)
==> Caveats
rstudio depends on R. The R Project provides official binaries:
brew install --cask r
Alternatively, the Homebrew-compiled version of R omits the GUI app:
brew install r
==> Analytics
install: 1,266 (30 days), 3,599 (90 days), 14,486 (365 days)
I still unfortunately get the issue when trying to install anything with Clang, e.g. cmdstanr
Error in compileCode(f, code, language = language, verbose = verbose) :
using C++ compiler: ‘Homebrew clang version 14.0.6’using C++17using SDK: ‘’clang-14: warning: include path for libstdc++ headers not found; pass '-stdlib=libc++' on the command line to use the libc++ standard library instead [-Wstdlibcxx-not-found]In file included from <built-in>:1:In file included from /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/StanHeaders/include/stan/math/prim/fun/Eigen.hpp:22:In file included from /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/RcppEigen/include/Eigen/Dense:1:In file included from /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/RcppEigen/include/Eigen/Core:19:/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/RcppEigen/include/Eigen/src/Core/util/Macros.h:679:10: fatal error: 'cmath' file not found#include <cmath> ^~~~~~~1 error generated.make: *** [file24385fb80314.o] Error 1
Error in sink(type = "output") : invalid connection
That’s RStudio, how have you installed R and set the Homebrew compilers to be used? Did you change environment variables, profile settings, etc?
Thanks for this @andrjohns. I am not sure exactly - I don’t recall changing my compiler or profile to specify the use of homebrew. How would I reset this/change it back to xcode?
r: 4.2.1
https://www.r-project.org/
/usr/local/Caskroom/r/4.2.1 (89.7MB)
From: https://github.com/caskroom/homebrew-cask/blob/HEAD/Casks/r.rb
==> Name
R
==> Description
Environment for statistical computing and graphics
==> Artifacts
R-4.2.1.pkg (Pkg)
==> Caveats
Cask r installs files under /usr/local. The presence of such
files can cause warnings when running `brew doctor`, which is considered
to be a bug in Homebrew Cask.
Ah, I think I see where some issues may be coming from. Your Homebrew installation is installing casks/packages to /usr/local, but Homebrew on Apple Silicon uses /opt/homebrew instead. This indicates that you might be using the intel/x86_64 version of Homebrew instead of the Apple Silicon version.
To double-check this, can you run brew config and post the output?
Thanks for this -
Here is the output:
HOMEBREW_VERSION: 3.5.4
ORIGIN: https://github.com/Homebrew/brew
HEAD: 88a5fac96752f17280e64f60926f58dff45990e0
Last commit: 1 year, 8 months ago
Core tap ORIGIN: https://github.com/Homebrew/homebrew-core
Core tap HEAD: b9229090a43eab220baac2fb5b89649a12b9751e
Core tap last commit: 1 year, 8 months ago
Core tap branch: master
HOMEBREW_PREFIX: /usr/local
HOMEBREW_CASK_OPTS: []
HOMEBREW_MAKE_JOBS: 12
Homebrew Ruby: 2.6.8 => /usr/local/Homebrew/Library/Homebrew/vendor/portable-ruby/2.6.8_1/bin/ruby
CPU: dodeca-core 64-bit westmere
Clang: 15.0.0 build 1500
Git: 2.39.3 => /Applications/Xcode.app/Contents/Developer/usr/bin/git
Curl: 8.1.2 => /usr/bin/curl
macOS: 13.5-x86_64
CLT: 14.3.1.0.1.1683849156
Xcode: 15.2
Rosetta 2: true
Yep the Rosetta 2: true confirms it, you have the Intel version of homebrew installed - so it’s installing and running Intel packages instead of Apple Silicon (which are then run through Rosetta). It might not be the direct cause of the compilation issues you’re seeing here, but R is attempting to use these packages.
If you didn’t have a reason to prefer the Rosetta-run Homebrew, it would make debugging dramatically easier if you could migrate your installation to the Apple Silicon homebrew. There’s a community guide on the process which several people have said works for them: Migrate from Intel (Rosetta2) to ARM brew on M1 · Homebrew · Discussion #417 · GitHub
Thanks for this - I have now completed these steps and it seems to have worked:
HOMEBREW_VERSION: 4.2.12
ORIGIN: https://github.com/Homebrew/brew
HEAD: 780fbbc65e90fbe09629aba180a1839e9e7dbaf2
Last commit: 3 days ago
Core tap: N/A
Core cask tap: N/A
HOMEBREW_PREFIX: /opt/homebrew
HOMEBREW_CASK_OPTS: []
HOMEBREW_MAKE_JOBS: 12
Homebrew Ruby: 3.1.4 => /opt/homebrew/Library/Homebrew/vendor/portable-ruby/3.1.4/bin/ruby
CPU: dodeca-core 64-bit arm_blizzard_avalanche
Clang: 15.0.0 build 1500
Git: 2.39.3 => /Applications/Xcode.app/Contents/Developer/usr/bin/git
Curl: 8.1.2 => /usr/bin/curl
macOS: 13.5-arm64
CLT: 14.3.1.0.1.1683849156
Xcode: 15.2
Rosetta 2: false
I try to then install rstan once more and I get this still:
trying URL 'https://cran.rstudio.com/src/contrib/rstan_2.32.6.tar.gz'
Content type 'application/x-gzip' length 1555968 bytes (1.5 MB)
==================================================
downloaded 1.5 MB
* installing *source* package ‘rstan’ ...
** package ‘rstan’ successfully unpacked and MD5 sums checked
** using staged installation
** libs
using C++ compiler: ‘clang version 14.0.6’
using C++17
using SDK: ‘’
clang++ -arch arm64 -std=gnu++17 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I"../inst/include" -I"../inst/include/boost_not_in_BH" -I"/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/StanHeaders/include/src" -DBOOST_DISABLE_ASSERTS -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION -D_REENTRANT -DSTAN_THREADS -DUSE_STANC3 -DSTRICT_R_HEADERS -D_HAS_AUTO_PTR_ETC=0 -I'/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/Rcpp/include' -I'/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/RcppEigen/include' -I'/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/BH/include' -I'/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/StanHeaders/include' -I'/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/RcppParallel/include' -I/opt/R/arm64/include -DSTRICT_R_HEADERS -fPIC -falign-functions=64 -Wall -g -O2 -c Module.cpp -o Module.o
clang-14: warning: include path for libstdc++ headers not found; pass '-stdlib=libc++' on the command line to use the libc++ standard library instead [-Wstdlibcxx-not-found]
In file included from Module.cpp:1:
In file included from /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/Rcpp/include/Rcpp.h:27:
In file included from /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/Rcpp/include/RcppCommon.h:30:
In file included from /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/Rcpp/include/Rcpp/r/headers.h:62:
/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/Rcpp/include/Rcpp/platform/compiler.h:100:10: fatal error: 'cmath' file not found
#include <cmath>
^~~~~~~
1 error generated.
make: *** [Module.o] Error 1
ERROR: compilation failed for package ‘rstan’
* removing ‘/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/rstan’
* restoring previous ‘/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/rstan’
Warning in install.packages :
installation of package ‘rstan’ had non-zero exit status
The next step is to cleanup any leftover Homebrew packages in the /usr/local directory, since the old clang compiler still appears to be there (which is causing this compilation error).
What output do you get from:
ls /usr/local/bin
ls /usr/local/include
I get this:
ls /usr/local/bin
2to3 lprsetup.sh python3
2to3-3.10 makeinfo python3-32
2to3-3.7 npm python3-config
2to3-3.9 npx python3-intel64
R pdf2dsc python3.10
Rscript pdf2ps python3.10-config
dvipdf pdftexi2dvi python3.10-intel64
easy_install-3.7 pf2afm python3.7
eps2eps pfbtopfa python3.7-32
font2c pip3.10 python3.7-config
gfortran pip3.7 python3.7m
gs pod2texi python3.7m-config
gs-X11 pphs python3.9
gs-noX11 printafm python3.9-config
gsbj ps2ascii python3.9-intel64
gsdj ps2epsi pyvenv
gsdj500 ps2pdf pyvenv-3.7
gslj ps2pdf12 rclone
gslp ps2pdf13 sqlite3_analyzer
gsnd ps2pdf14 tclsh8.6
idle3 ps2pdfwr texi2any
idle3.10 ps2ps texi2dvi
idle3.7 ps2ps2 texi2pdf
idle3.9 pydoc3 texindex
info pydoc3.10 unix-lpr.sh
infokey pydoc3.7 wftopfa
install-info pydoc3.9 wish8.6
ls /usr/local/include
JAGS mysqlStubs.h tclThread.h
fakemysql.h odbcStubs.h tclTomMath.h
fakepq.h omp-tools.h tclTomMathDecls.h
fakesql.h omp.h tdbc.h
itcl.h ompt.h tdbcDecls.h
itcl2TclOO.h pqStubs.h tdbcInt.h
itclDecls.h tcl.h tk.h
itclInt.h tclDecls.h tkDecls.h
itclIntDecls.h tclOO.h tkPlatDecls.h
itclMigrate2TclCore.h tclOODecls.h
itclTclIntStubsFcn.h tclPlatDecls.h