macOS Mojave fix for Rcpp compilation headers

C++ headers are currently messed up inside R on Mojave by default.

Note: Rcpp headers appear to now work correctly on Catalina. This step is no longer necessary and won’t work on macOS 10.15+, since the macOS_SDK_headers_for_macOS_10.14.pkg installer (see below) is no longer bundled with Xcode CLI tools.

Note: If you install a macOS system update, you’ll likely need to re-run this. Refer to this comment for additional details.

For example, say we want to build the data.table and rlang packages from source.

Update old packages: 'data.table', 'rlang'
Update all/some/none? [a/s/n]: a

  There are binary versions available but the source versions are later:
           binary source needs_compilation
data.table 1.12.0 1.12.2              TRUE
rlang       0.3.1  0.3.4              TRUE

If your Rcpp configuration is messed up, you’ll see error messages like this:

In file included from ./data.table.h:1:
/Library/Frameworks/R.framework/Resources/include/R.h:55:11: fatal error: 'stdlib.h' file
      not found
# include <stdlib.h> /* Not used by R itself, but widely assumed in packages */

Here’s another common error indicating Rcpp headers aren’t configured correctly:

fatal error: 'string.h' file not found
   #include <string.h>

We need to hard install the headers back to the default location of previous macOS builds.

sudo installer \
    -pkg /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg \
    -target /

You should see this return in the terminal:

installer: Package name is macOS_SDK_headers_for_macOS_10.14
installer: Installing at base path /
installer: The install was successful.

If you see this error, remove the Xcode command line tools, reinstall, and then try the above command again.

installer: The install failed (The Installer encountered an error that caused the installation to fail. Contact the software manufacturer for assistance.)
sudo rm -rf /Library/Developer/CommandLineTools
xcode-select --install
sudo xcodebuild -license accept