c++rsdkrcppmacos-sonoma

error sourcing c++ with Rcpp in Mac OS Sonoma 14.5: fatal error 'cmath' file not found


I have recently moved to Mac OS Sonoma 14.5 and I am now trying to use my previous R script based on Rcpp (sourcing C++ in R).

I have been having serious difficulties understanding how to link the right compilers. I have seen that many people are having the same problems.

I have followed the suggestions in Configuring compilers on Apple silicon (M1, M2, M3, ...) for Rcpp and other tools , and indeed it has reduced the number of errors. Now however I do get a slightly different type of error:

using C++ compiler: ‘Apple clang version 15.0.0 (clang-1500.3.9.4)’
using SDK: ‘’
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++  -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG   -I"/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/Rcpp/include" -I"/Users/testUser/Cpp_code" -I/opt/R/arm64/include -Xclang -fopenmp    -fPIC  -I/usr/local/opt/llvm/include  -c test_function.cpp -o test_function.o
In file included from test_function.cpp:1:
In file included from /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/Rcpp/include/Rcpp.h:27:
In file included from /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/Rcpp/include/RcppCommon.h:30:
In file included from /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/Rcpp/include/Rcpp/r/headers.h:66:
/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/Rcpp/include/Rcpp/platform/compiler.h:100:10: fatal error: 'cmath' file not found
#include <cmath>
         ^~~~~~~
1 error generated.

I suppose that one of the issues is that I do not have SDK header?

This is my ~/.R/Makevars:

CC=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang
CXX=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++
CXX1X=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++
LDFLAGS=-L/usr/local/opt/llvm/lib -Wl,-rpath,/usr/local/opt/llvm/lib
CXXFLAGS=-I/usr/local/opt/llvm/include
FLIBS=-L/usr/local/Cellar/gcc/14.1.0_2/lib/gcc/14
CPPFLAGS += -Xclang -fopenmp
LDFLAGS += -lomp

Solution

  • I solved my problem by simply deleting the ~/.R/Makevars file.