Dealing with Catalina II

If you have Catalina and clang++ installed from @coatless’s installer, try executing (once) in R

cat("CXX14 = /usr/local/clang7/bin/clang++ -stdlib=libc++ -nostdinc++ -I/usr/local/clang7/include/c++/v1", 
    "SHLIB_CXX14LD = /usr/local/clang7/bin/clang++ -L/usr/local/clang7/lib/",
    file = "~/.R/Makevars", append = TRUE, sep = "\n")

before calling rstan::stan or rstan::stan_model.

4 Likes

I tried this and got the error described here,
https://discourse.mc-stan.org/t/configuration-for-clang/4890

when I tried to change the permissions (again), using
chmod g+rwx /Users/me/.R/Makevars
I received,
Unable to change file mode on /Users/me/.R/Makevars Operation not permitted

Maybe

sudo chmod g+rwx /Users/me/.R/Makevars

?

1 Like

Ben, I have @coatless’s installer and changed Makevars accordingly and then ran rstan::stan_model with bar.stan (3.6 KB) .

n=1000
x=-log(runif(n))
library(rstan)
fitted_model = stan_model(file='bar.stan')
stan_data <- list(y=x, N = n, ymin = 0)
stan_fit = sampling(fitted_model, data = stan_data)

Same thing happens:

Chain 1: Iteration: 1 / 2000 [ 0%] (Warmup)
[1] “Error in sampler$call_sampler(args_list[[i]]) : "
[2] " c++ exception (unknown reason)”

Was this what you wanted?

My Makevars looks like this now:

CFLAGS=-isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk
CCFLAGS=-isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk
CXXFLAGS=-isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk
CPPFLAGS=-isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -I/usr/local/include
SHLIB_CXX14LD = /usr/local/clang7/bin/clang++ -L$(R_HOME)/lib/libc++.1.dylib

this ran fine after what you posted

dat_list <- list(
y = scale(mtcars$disp),
b = scale(mtcars$cyl))

mtcars_model <-
"
data{
vector[32] y;
vector[32] b;
}
parameters{
real beta;
real alpha;
real<lower=0> sigma;
}
model{
vector[32] mu;
sigma ~ exponential( 1 );
alpha ~ normal( 0 , 1 );
beta ~ normal( 0 , 1 );
for ( i in 1:32 ) {
mu[i] = alpha + beta * b[i];
}
y ~ normal( mu , sigma );
}

"

There should have been a

CXX14 = /usr/local/clang7/bin/clang++ -stdlib=libc++ -nostdinc++ -I/usr/local/clang7/include/c++/v1

in the ~/.R/Makevars somewhere.

Using the previous example I posted above, and my Makevars looking like this:

CFLAGS=-isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk
CCFLAGS=-isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk
CXXFLAGS=-isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk
CPPFLAGS=-isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -I/usr/local/include
SHLIB_CXX14LD = /usr/local/clang7/bin/clang++ -L$(R_HOME)/lib/libc++.1.dylib
CXX14 = /usr/local/clang7/bin/clang++ -stdlib=libc++ -nostdinc++ -I/usr/local/clang7/include/c++/v1

Leads to,

Chain 4: Iteration: 1 / 2000 [ 0%] (Warmup)
[1] “Error in sampler$call_sampler(args_list[[i]]) : "
[2] " c++ exception (unknown reason)”

When you then do

dlls <- getLoadedDLLs()
paths <- vapply(dlls, `[[`, "path", FUN.VALUE = character(1))
invisible(lapply(paths, function(path) {
  
  if (!file.exists(path))
    return(FALSE)
  
  output <- system(paste("otool -L", shQuote(path), "| grep libc++ || true"),
                   intern = TRUE)
  if (length(output) == 0)
    return(FALSE)
  
  writeLines(paste0(path, ":"))
  writeLines(output)
  
}))

does it indicate that the .so files from the temporary directory are linked against
/usr/lib/libc++.1.dylib ?

When I run your code after executing the example I get this (before executing the example it doesn’t output anything):

/Users/rr/Library/R/3.6/library/Rcpp/libs/Rcpp.so:
/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 800.6.0)
/Users/rr/Library/R/3.6/library/scales/libs/scales.so:
/Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libc++.1.dylib (compatibility version 1.0.0, current version 1.0.0)
/Users/rr/Library/R/3.6/library/tidyselect/libs/tidyselect.so:
/Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libc++.1.dylib (compatibility version 1.0.0, current version 1.0.0)
/Users/rr/Library/R/3.6/library/dplyr/libs/dplyr.so:
/Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libc++.1.dylib (compatibility version 1.0.0, current version 1.0.0)
/Users/rr/Library/R/3.6/library/rstan/libs/rstan.so:
/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 800.6.0)
/var/folders/5p/dlcj_kd94bx83r83k_d77j54pspdk5/T//RtmpyQ0YVv/file7bad2d33500f.so:
/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 800.7.0)

OK. That is suppose to link against $(R_HOME)/lib/libc++.1.dylib . We need to figure out why that is not happening despite the new ~/.R/Makevars . When you compile with verbose = TRUE, what are the lines that show how the compiler and linker are being called?

This?

Compilation argument:
/Library/Frameworks/R.framework/Resources/bin/R CMD SHLIB file7fda7e5f0e0b.cpp 2> file7fda7e5f0e0b.cpp.err.txt
/usr/local/clang7/bin/clang++ -stdlib=libc++ -nostdinc++ -I/usr/local/clang7/include/c++/v1 -std=gnu++14 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I"/Users/torkarr/Library/R/3.6/library/Rcpp/include/" -I"/Users/torkarr/Library/R/3.6/library/RcppEigen/include/" -I"/Users/torkarr/Library/R/3.6/library/RcppEigen/include/unsupported" -I"/Users/torkarr/Library/R/3.6/library/BH/include" -I"/Users/torkarr/Library/R/3.6/library/StanHeaders/include/src/" -I"/Users/torkarr/Library/R/3.6/library/StanHeaders/include/" -I"/Users/torkarr/Library/R/3.6/library/rstan/include" -DEIGEN_NO_DEBUG -DBOOST_DISABLE_ASSERTS -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -I/usr/local/include -fPIC -Wall -g -O2 -c file7fda7e5f0e0b.cpp -o file7fda7e5f0e0b.o
/usr/local/clang7/bin/clang++ -L/Library/Frameworks/R.framework/Resources/lib/libc++.1.dylib -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/Library/Frameworks/R.framework/Resources/lib -L/usr/local/lib -o file7fda7e5f0e0b.so file7fda7e5f0e0b.o -L/Users/torkarr/Library/R/3.6/library/StanHeaders/lib/ -lStanHeaders -F/Library/Frameworks/R.framework/… -framework R -Wl,-framework -Wl,CoreFoundation

And the line below that?

updated above…

Wait Ben, my .Renviron is missing… I’ll update…

.Renviron in place, and running your function looks like this:

/Users/torkarr/Library/R/3.6/library/Rcpp/libs/Rcpp.so:
/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 800.6.0)
/Users/torkarr/Library/R/3.6/library/scales/libs/scales.so:
/Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libc++.1.dylib (compatibility version 1.0.0, current version 1.0.0)
/Users/torkarr/Library/R/3.6/library/tidyselect/libs/tidyselect.so:
/Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libc++.1.dylib (compatibility version 1.0.0, current version 1.0.0)
/Users/torkarr/Library/R/3.6/library/dplyr/libs/dplyr.so:
/Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libc++.1.dylib (compatibility version 1.0.0, current version 1.0.0)
/Users/torkarr/Library/R/3.6/library/rstan/libs/rstan.so:
/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 800.6.0)
/var/folders/5p/dlcj_kd94bx83r83k_d77j54pspdk5/T//Rtmp1dxXa1/file8434301b38ac.so:
/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 800.7.0)

And compiler output:

Compilation argument:
/Library/Frameworks/R.framework/Resources/bin/R CMD SHLIB file86f81f777c59.cpp 2> file86f81f777c59.cpp.err.txt
/usr/local/clang7/bin/clang++ -stdlib=libc++ -nostdinc++ -I/usr/local/clang7/include/c++/v1 -std=gnu++14 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I"/Users/torkarr/Library/R/3.6/library/Rcpp/include/" -I"/Users/torkarr/Library/R/3.6/library/RcppEigen/include/" -I"/Users/torkarr/Library/R/3.6/library/RcppEigen/include/unsupported" -I"/Users/torkarr/Library/R/3.6/library/BH/include" -I"/Users/torkarr/Library/R/3.6/library/StanHeaders/include/src/" -I"/Users/torkarr/Library/R/3.6/library/StanHeaders/include/" -I"/Users/torkarr/Library/R/3.6/library/rstan/include" -DEIGEN_NO_DEBUG -DBOOST_DISABLE_ASSERTS -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -I/usr/local/include -fPIC -Wall -g -O2 -c file86f81f777c59.cpp -o file86f81f777c59.o
/usr/local/clang7/bin/clang++ -L/Library/Frameworks/R.framework/Resources/lib/libc++.1.dylib -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/Library/Frameworks/R.framework/Resources/lib -L/usr/local/lib -o file86f81f777c59.so file86f81f777c59.o -L/Users/torkarr/Library/R/3.6/library/StanHeaders/lib/ -lStanHeaders -F/Library/Frameworks/R.framework/… -framework R -Wl,-framework -Wl,CoreFoundation

What about just

Rcpp::sourceCpp(code = 
'
#include <Rcpp.h>

// [[Rcpp::export]]
int throw_exception() { 
  std::stringstream errmsg; errmsg << "this is the expected behavior";
  throw std::domain_error(errmsg.str()); 
  return 0;
}
'
)

throw_exception()

?

Restarted RStudio and ran your code:
Error in throw_exception() : this is the expected behavior

OK. And you have StanHeaders and rstan from CRAN?

Sure. BUT, let me try it out again. What’s the best way to clean out StanHeaders and rstan completely?

remove.packages("rstan")
remove.packages("StanHeaders")
file.remove(".RData")

restart

install.packages("rstan", dependencies = TRUE)