Error in sink(type = "output") : invalid connection on Mac OS

Hello,

I have recently started using a new computer (Mac OS 12.2, R 4.1.2, brms 2.16.3). In my first session using R on the new computer, I receive first a pop-up message asking if I would “like to install build tools.” Regardless of what I select, the model does not compile and the following error message appears:

Error in sink(type = “output”) : invalid connection

The full statement is:

make cmd is
make -f ‘/Library/Frameworks/R.framework/Resources/etc/Makeconf’ -f ‘/Library/Frameworks/R.framework/Resources/share/make/shlib.mk’ -f ‘/Users/JaredVasil/.R/Makevars’ CXX=‘(CXX14) (CXX14STD)’ CXXFLAGS=‘(CXX14FLAGS)' CXXPICFLAGS='(CXX14PICFLAGS)’ SHLIB_LDFLAGS=‘(SHLIB_CXX14LDFLAGS)' SHLIB_LD='(SHLIB_CXX14LD)’ SHLIB=‘file3cd323fcb070.so’ OBJECTS=‘file3cd323fcb070.o’

make would use
if test “zfile3cd323fcb070.o” != “z”; then
echo clang++ -mmacosx-version-min=10.13 -std=gnu++14 -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -Wl,-rpath,/Library/Frameworks/R.framework/Resources/lib /Library/Frameworks/R.framework/Resources/lib/libc++abi.1.dylib -L"/Library/Frameworks/R.framework/Resources/lib" -L/usr/local/lib -o file3cd323fcb070.so file3cd323fcb070.o ‘/Library/Frameworks/R.framework/Versions/4.1/Resources/library/rstan/lib//libStanServices.a’ -L’/Library/Frameworks/R.framework/Versions/4.1/Resources/library/StanHeaders/lib/’ -lStanHeaders -L’/Library/Frameworks/R.framework/Versions/4.1/Resources/library/RcppParallel/lib/’ -ltbb -F/Library/Frameworks/R.framework/… -framework R -Wl,-framework -Wl,CoreFoundation;
clang++ -mmacosx-version-min=10.13 -std=gnu++14 -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -Wl,-rpath,/Library/Frameworks/R.framework/Resources/lib /Library/Frameworks/R.framework/Resources/lib/libc++abi.1.dylib -L"/Library/Frameworks/R.framework/Resources/lib" -L/usr/local/lib -o file3cd323fcb070.so file3cd323fcb070.o ‘/Library/Frameworks/R.framework/Versions/4.1/Resources/library/rstan/lib//libStanServices.a’ -L’/Library/Frameworks/R.framework/Versions/4.1/Resources/library/StanHeaders/lib/’ -lStanHeaders -L’/Library/Frameworks/R.framework/Versions/4.1/Resources/library/RcppParallel/lib/’ -ltbb -F/Library/Frameworks/R.framework/… -framework R -Wl,-framework -Wl,CoreFoundation;
fi
Error in compileCode(f, code, language = language, verbose = verbose) :
file3cd323fcb070.cpp:356:24: warning: unused typedef ‘local_scalar_t__’ [-Wunused-local-typedef] typedef double local_scalar_t__; ^28 warnings generated.clang: error: no such file or directory: '/Library/Frameworks/R.framework/Resources/lib/libc++abi.1.dylib’make: *** [file3cd323fcb070.so] Error 1
In addition: Warning message:
Rows containing NAs were excluded from the model.
Error in sink(type = “output”) : invalid connection

I have seen other threads about this topic (e.g., Error in sink(type = "output") : invalid connection ; c++ - Stan model will not compile in R or Rstudio - Stack Overflow). However, following the solutions I’ve come across has not helped. Any help with this issue is greatly appreciated, and I am happy to offer more information that may be of use. Thanks.

Update for anyone who comes across this later. I fixed the issue by uninstalling and reinstalling rstan, brms, and dependencies. Next, I uninstalled the R development toolchain, following:

Uninstalling the R development toolchain on macOS | The Coatless Professor

Next, I followed:

R Compiler Tools for Rcpp on macOS | The Coatless Professor

to reinstall the Xcode CLI. Then, I reinstalled the appropriate gfortran for Monterey, at:

Release gfortran 12-experimental for ARM, macOS 12 (Monterey) · fxcoudert/gfortran-for-macOS · GitHub

I then restarted R, and now things seem to be working appropriately (model compiles and sampling proceeds in parallel without error). Will update if further issues arise.

Hello, can anyone help on the similar issue met in 2026:

Mac OS Sequoia 15.5

R version 4.5.2(2025-10-31)

The error message :

Error in compileCode(f, code, language = language, verbose = verbose) : using C++ compiler: ‘Apple clang version 17.0.0 (clang-1700.0.13.5)’ using C++17 using SDK: ‘MacOSX15.5.sdk’ In file included from :1: In file included from /Library/Frameworks/R.framework/Versions/4.5-x86_64/Resources/library/StanHeaders/include/stan/math/prim/fun/Eigen.hpp:22: In file included from /Library/Frameworks/R.framework/Versions/4.5-x86_64/Resources/library/RcppEigen/include/Eigen/Dense:1: In file included from /Library/Frameworks/R.framework/Versions/4.5-x86_64/Resources/library/RcppEigen/include/Eigen/Core:19: /Library/Frameworks/R.framework/Versions/4.5-x86_64/Resources/library/RcppEigen/include/Eigen/src/Core/util/Macros.h:679:10: fatal error: ‘cmath’ file not found 679 | #include | ^~~~~~~ 1 error generated. make: *** [file5ad225663ab.o] Error 1

Error in sink(type = “output”) : invalid connection

I have went through the above method in the post, but could not work for me. Programming experts, kindly give some suggestions~Thanks!

p.s before two errors the full statement is as such:

make cmd is
  make -f '/Library/Frameworks/R.framework/Resources/etc/Makeconf' -f '/Library/Frameworks/R.framework/Resources/share/make/shlib.mk' CXX='$(CXX17) $(CXX17STD)' CXXFLAGS='$(CXX17FLAGS)' CXXPICFLAGS='$(CXX17PICFLAGS)' SHLIB_LDFLAGS='$(SHLIB_CXX17LDFLAGS)' SHLIB_LD='$(SHLIB_CXX17LD)' SHLIB='file5ad225663ab.so' OBJECTS='file5ad225663ab.o'

make would use
clang++ -arch x86_64 -std=gnu++17 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG   -I"/Library/Frameworks/R.framework/Versions/4.5-x86_64/Resources/library/Rcpp/include/"  -I"/Library/Frameworks/R.framework/Versions/4.5-x86_64/Resources/library/RcppEigen/include/"  -I"/Library/Frameworks/R.framework/Versions/4.5-x86_64/Resources/library/RcppEigen/include/unsupported"  -I"/Library/Frameworks/R.framework/Versions/4.5-x86_64/Resources/library/BH/include" -I"/Library/Frameworks/R.framework/Versions/4.5-x86_64/Resources/library/StanHeaders/include/src/"  -I"/Library/Frameworks/R.framework/Versions/4.5-x86_64/Resources/library/StanHeaders/include/"  -I"/Library/Frameworks/R.framework/Versions/4.5-x86_64/Resources/library/RcppParallel/include/"  -I"/Library/Frameworks/R.framework/Versions/4.5-x86_64/Resources/library/rstan/include" -DEIGEN_NO_DEBUG  -DBOOST_DISABLE_ASSERTS  -DBOOST_PENDING_INTEGER_LOG2_HPP  -DSTAN_THREADS  -DUSE_STANC3 -DSTRICT_R_HEADERS  -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION  -D_HAS_AUTO_PTR_ETC=0  -include '/Library/Frameworks/R.framework/Versions/4.5-x86_64/Resources/library/StanHeaders/include/stan/math/prim/fun/Eigen.hpp'  -D_REENTRANT -DRCPP_PARALLEL_USE_TBB=1   -I/opt/R/x86_64/include    -fPIC  -falign-functions=64 -Wall -g -O2   -c file5ad225663ab.cpp -o file5ad225663ab.o
if test  "zfile5ad225663ab.o" != "z"; then \
	  echo clang++ -arch x86_64 -std=gnu++17 -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -L"/Library/Frameworks/R.framework/Resources/lib" -L/opt/R/x86_64/lib -o file5ad225663ab.so file5ad225663ab.o  '/Library/Frameworks/R.framework/Versions/4.5-x86_64/Resources/library/rstan/lib//libStanServices.a' -L'/Library/Frameworks/R.framework/Versions/4.5-x86_64/Resources/library/StanHeaders/lib/' -lStanHeaders -L'/Library/Frameworks/R.framework/Versions/4.5-x86_64/Resources/library/RcppParallel/lib/' -ltbb -L/Library/Frameworks/R.framework/Versions/4.5-x86_64/Resources/library/RcppParallel/lib -ltbb -ltbbmalloc   -F/Library/Frameworks/R.framework/.. -framework R ; \
	  clang++ -arch x86_64 -std=gnu++17 -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -L"/Library/Frameworks/R.framework/Resources/lib" -L/opt/R/x86_64/lib -o file5ad225663ab.so file5ad225663ab.o  '/Library/Frameworks/R.framework/Versions/4.5-x86_64/Resources/library/rstan/lib//libStanServices.a' -L'/Library/Frameworks/R.framework/Versions/4.5-x86_64/Resources/library/StanHeaders/lib/' -lStanHeaders -L'/Library/Frameworks/R.framework/Versions/4.5-x86_64/Resources/library/RcppParallel/lib/' -ltbb -L/Library/Frameworks/R.framework/Versions/4.5-x86_64/Resources/library/RcppParallel/lib -ltbb -ltbbmalloc   -F/Library/Frameworks/R.framework/.. -framework R ; \
	fi

Are you able to compile other C++ code from R that’s not related to Stan? For example, here’s some Rcpp code you can run in R that will try compiling some C++ code:

library(Rcpp)
cppFunction(
  code = 'double test_cmath(double x) { return std::sqrt(x); }',
  includes = '#include <cmath>'
)
test_cmath(9)

Does that work for you or does that also fail?

Also what does this return?

system2(file.path(R.home("bin"), "R"), c("CMD", "config", "CXX17"), stdout = TRUE)

The C++ code also fail, full statement with error message as below:

using C++ compiler: ‘Apple clang version 17.0.0 (clang-1700.0.13.5)’
using SDK: ‘MacOSX15.5.sdk’
clang++ -arch x86_64 -std=gnu++17 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I"/Library/Frameworks/R.framework/Versions/4.5-x86_64/Resources/library/Rcpp/include" -I"/private/var/folders/6s/0zb8sp252yg07z9zmr_0pqbc0000gn/T/RtmpPnEf3v/sourceCpp-x86_64-apple-darwin20-1.1.1" -I/opt/R/x86_64/include -fPIC -falign-functions=64 -Wall -g -O2 -c file4e97d627a08.cpp -o file4e97d627a08.o
In file included from file4e97d627a08.cpp:1:
In file included from /Library/Frameworks/R.framework/Versions/4.5-x86_64/Resources/library/Rcpp/include/Rcpp.h:27:
In file included from /Library/Frameworks/R.framework/Versions/4.5-x86_64/Resources/library/Rcpp/include/RcppCommon.h:30:
In file included from /Library/Frameworks/R.framework/Versions/4.5-x86_64/Resources/library/Rcpp/include/Rcpp/r/headers.h:67:
/Library/Frameworks/R.framework/Versions/4.5-x86_64/Resources/library/Rcpp/include/Rcpp/platform/compiler.h:37:10: fatal error: ‘cmath’ file not found
37 | #include
| ^~~~~~~
1 error generated.
make: *** [file4e97d627a08.o] Error 1

Error in sourceCpp(code = code, env = env, rebuild = rebuild, cacheDir = cacheDir, :
Error 1 occurred building shared library.

and the return for the system2 query is as such:

[1] “clang++ -arch x86_64”

Thanks Jonah for keeping an eye on my issue!

This suggests that this isn’t a Stan issue, but a more general issue compiling code. Your Mac’s C++ standard library headers aren’t being found. So I think this is a system toolchain / Xcode Command Line Tools issue. It might be worth installing the command line tools again. You can open the terminal and run

xcode-select --install

Or did you do that already?

I’m a little rusty debugging toolchain issues like this, but if compilation still fails after that then can you share what’s in your Makevars file? You can view it using:

usethis::edit_r_makevars()

And also what’s the output from running the commands below?

# What kind of Mac CPU do you have
Sys.info()[["machine"]]
R.version$arch

# Where macOS thinks the developer tools are
system2("xcode-select", "-p", stdout = TRUE, stderr = TRUE)

# Where the macOS SDK is (this is where headers like <cmath> live)
system2("xcrun", "--show-sdk-path", stdout = TRUE, stderr = TRUE)

# Check if the <cmath> header exists inside that SDK
sdk <- system2("xcrun", "--show-sdk-path", stdout = TRUE)
file.exists(file.path(sdk, "usr/include/c++/v1/cmath"))

Thanks Jonah!

I have installed the command tool before, and just tested and it proves installed:

xcode-select: note: Command line tools are already installed. Use “Software Update” in System Settings or the softwareupdate command line interface to install updates

However, there’s issue running the below commands in the terminal:

zsh: command not found: R.version

zsh: number expected

zsh: number expected

zsh: number expected

zsh: bad pattern: file.exists(file.path(sdk, usr/include/c++/v1/cmath))

Continuing the discussion from Error in sink(type = “output”) : invalid connection on Mac OS:

And for the result after keying usethis::edit_r_makevars() in the terminal, it shows:

function>

in the next row

Oh I think this means you were running this code also in the terminal. Only the xcode line was meant to run in the terminal, sorry I should have been more clear. Can you run the rest of the code again but inside an R session? Also the usethis::edit_r_makevars() should be run in R not the terminal.

xcode-select: note: Command line tools are already installed. Use “Software Update” in System Settings or the softwareupdate command line interface to install updates

Even though xcode-select --install says “already installed”, the missing cmath error often means the install is broken. A common (but not guaranteed) fix is to remove and reinstall the Command Line Tools:

# run in the terminal, not R 
# this will _remove_ the current installation of the command line tools 
sudo rm -rf /Library/Developer/CommandLineTools

# reinstall the command line tools
xcode-select --install