For some reason Apple decided to break building C/C++ on MacOS by moving the C and C++ stdlib headers into a rando location that most toolchains don’t understand. Amazing. I ran into this while trying to build the R ggplot2 package from source and got missing header errors.
When installing a package from source fails, you’ll see a message like this.Warning in install.packages :
installation of package ‘Rcpp’ had non-zero exit status
If you look back in the build output, you’ll see errors about missing headers. Something like this:In file included from api.cpp:26:
In file included from ../inst/include/Rcpp.h:27:
In file included from ../inst/include/RcppCommon.h:30:
In file included from ../inst/include/Rcpp/r/headers.h:66:
../inst/include/Rcpp/platform/compiler.h:100:10: fatal error: 'cmath' file not found
The problem is that Apple moved these files out of /usr/include and /usr/local/include into a magical SDK package that most things don’t know about. Luckily R provides a way to set up the build environment when building packages from source with a file called Makevars.
First you need to find the location of your headers. Assuming you already have the Xcode command line tools installed, run xcrun --show-sdk-path> xcrun --show-sdk-path
> /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk
In that package, you’ll find usr/include — but that’s not where the C++ headers are either. They are actually in usr/include/c++/v1.
Next, you need to create the Makevars file ~/.R/Makevars — you may need to create the .R folder first. (mkdir ~/.R)
Once you’ve created the Makevars file, add this build variable and save:CPPFLAGS = -I /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1 -I /opt/homebrew/include
Open an R command prompt and run tools::makevars_user() — you should see the path to your new Makevars file if you put it in the right place.[1] "/Users/username/.R/Makevars"
You should be good to go.
UPDATE – I still ran into more problems while trying to install the devtools package and needed additional tweaks. For reference, I’m trying to follow the setup steps for the book “The Art of Machine Learning” by Maloff where the ultimate goal of all this mess is to get install_github("https://github.com/matloff/qeML") to install correctly.
brew install libgit2
My Makevars file now looks like this hack-a-palooza. The R source packages are pretty dirty and their build scripts seem to need a lot of help selecting C++ version especially.INCLUDE_PATH = -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1 -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include -I/opt/homebrew/include
CPPFLAGS = $(INCLUDE_PATH)
CFLAGS = $(INCLUDE_PATH)
CC = clang $(INCLUDE_PATH)
CXX = clang++ -std=c++17 $(INCLUDE_PATH)
CXX11 = clang++ -std=c++11 $(INCLUDE_PATH)
CXX14 = clang++ -std=c++14 $(INCLUDE_PATH)
CXX17 = clang++ -std=c++17 $(INCLUDE_PATH)
OBJCXX = clang++ -std=c++17 $(INCLUDE_PATH)
References:






