Editor’s Note: This is an updated post that covers how to install the macOS toolchain for versions of R starting at 3.4.z.
For R versions between R 3.0.0 - 3.3.z, please see R Compiler Tools for Rcpp on OS X before R 3.4.0.
Intro
The objective behind this post is to provide users with information on how to setup
the macOS toolchain for compiling used in the 3.4.* series of R.
This has been a bit problematic for many R users since OS X Mavericks, which
resulted in gfortran
binaries being dropped from the R installer. More curiously,
the additional demand to have access to a compiler vs. downloading a binary from
CRAN became apparent slightly after Rcpp’s 0.10.0 version, when attributes where
added that removed the necessity to use R’s
SEXP objects.
Installation
There are two ways to go about setting up the R toolchain for compiled code on macOS:
- An online installer that installs all elements.
- Individually installing each element yourself.
Pick one and follow it to completion.
Automated Installer
The automated installer is just that, an automatic way to acquire all of the official components of the R toolchain on macOS. Download and run the installer while connected to the internet to acquire the appropriate software and configuration files.
Installer Download: https://uofi.box.com/v/r-macos-rtools-pkg
Source Code for Installer: https://github.com/coatless/r-macos-rtools
Manual Install Guide
This guide provides a step-by-step breakdown of the actions the automated installer
takes. As a result, this guide will use both installers and script commands
ia Terminal.app
found in /Applications/Utilities/
. Terminal is macOS’s
equivalent to Linux’s shell and Window’s command line. From Terminal, we will
install only the
XCode Command Line Tools.
These provide the system headers used to build the official CRAN binary for R.
XCode Command Line Tools
- Open the
Terminal
from/Applications/Utilities/
- Type the following into Terminal
xcode-select --install
- Press “Install”
- Verify installation by typing into terminal:
gcc --version
Installing the clang4
R binary
There are two options to install the clang4
binary that is prebuilt by
the CRAN macOS maintainer at: http://r.research.att.com/libs/.
- Use the
clang4-r
binary installer- Disclaimer: I created the installer from the listed binary
- Financial support was provided to sign the installer by Professor Timothy Bates of the University of Edinburgh.
- Use Terminal commands
Pick only one of these options.
Option 1: clang4-r
binary installer
Download and install the clang4-r
binary installer from:
https://uofi.box.com/v/r-macos-clang-pkg
The clang4-r
installer performs two actions that require
the users password to accomplish. These actions are:
- unpack a set of pre-made binary files into
the
/usr/local/clang4
directory - establish the proper paths for
CC
,CXX
,CXX**
, andLDFLAGS
in the~/.R/Makevars
file
In essence, it provides a graphical user interface installation guide,
more secure path manipulation, and a smarter handling of a pre-existing ~/.R/Makevars
To view how the installer was created please see: https://github.com/coatless/r-macos-clang
Option 2: Terminal approach
# Download clang4 binary
curl -O http://r.research.att.com/libs/clang-4.0.0-darwin15.6-Release.tar.gz
# Extract clang4 binary into root directory
tar fvxz clang-4.0.0-darwin15.6-Release.tar.gz -C /
# Verify file exists
touch ~/.R/Makevars
# Overwrites ~/.R/Makevars file if present otherwise creates it
cat <<- EOF > ~/.R/Makevars
# The following statements are required to use the clang4 binary
CC=/usr/local/clang4/bin/clang
CXX=/usr/local/clang4/bin/clang++
CXX1X=/usr/local/clang4/bin/clang++
CXX98=/usr/local/clang4/bin/clang++
CXX11=/usr/local/clang4/bin/clang++
CXX14=/usr/local/clang4/bin/clang++
CXX17=/usr/local/clang4/bin/clang++
LDFLAGS=-L/usr/local/clang4/lib
# End clang4 inclusion statements
EOF
# Clean up after ourself
rm -rf clang-4.0.0-darwin15.6-Release.tar.gz
Install gfortran
binary
- Visit https://gcc.gnu.org/wiki/GFortranBinaries#MacOS
- Select the appropriate gfortran binary for your system.
- To find your operating system version, go to: Apple menu () > About This Mac. The version of your operating system is displayed beneath “macOS” or “OS X” in the about window. For more assistance, please see: https://support.apple.com/en-us/HT201260
- macOS Sierra / High Sierra (10.12 - 10.13): http://coudert.name/software/gfortran-6.3-Sierra.dmg
- OS X El Capitan (10.11): http://coudert.name/software/gfortran-6.1-ElCapitan.dmg
- Install
gfortran
via the installer package.
Note: If you are on macOS Sierra / High Sierra (10.12 - 10.13), you will
need to set an FLIBS
variable in the ~/.R/Makevars
to avoid a compilation warning. The FLIBS
variable
sets the linker flags needed to link Fortran code. Execute the following
code in Terminal:
touch ~/.R/Makevars
cat <<- EOF >> ~/.R/Makevars
# The following statement changes the Fortran linking path
FLIBS=-L/usr/local/gfortran/lib/gcc/x86_64-apple-darwin16/6.3.0
# End Fortran linking path statement
EOF
Doing so will avoid the compilation error of:
ld: warning: directory not found for option
'-L/usr/local/gfortran/lib/gcc/x86_64-apple-darwin15/6.1.0'
The reason for this warning message is because R 3.4.* is compiled on
El Capitan that uses gfortran v6.1.0. Due to the way R’s site-wide Makevar
file is structured, this largely is a cosmetic fix as it has a generic catch-all
inclusion of /usr/local/gfortran/lib/gcc
.
Quick check
To verify that everything is working appropriately, let’s do a quick C++ program using Rcpp and Armadillo.
First, let’s install Rcpp
and RcppArmadillo
within R.
install.packages(c('Rcpp', 'RcppArmadillo'))
Create a new file, name the follow: helloworld.cpp
By adding the .cpp
extension, the file is viewed as being C++ code.
Within the file write:
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::export]]
void hello_world() {
Rcpp::Rcout << "Hello World!" << std::endl;
}
// After compile, this function will be immediately called using
// the below snippet and results will be sent to the R console.
/*** R
hello_world()
*/
Compile the function using:
Rcpp::sourceCpp('~/path/to/file/helloworld.cpp')
where 'path/to/file/'
is the location containing helloworld.cpp
If everything is installed appropriately, then you should see the following in the console:
> hello_world()
Hello World!
In addition, you should have a new function within the global environment scope called “hello_world”. You can call this function like a normal R function via:
hello_world()