Sebastian Ankargren bio photo

Sebastian Ankargren

Ph. D. Student
Department of Statistics
Uppsala University

Email LinkedIn Github

Recently, I was preparing a package on my Windows machine at work. Building and checking the package showed no signs of problems, and I was almost ready to submit to CRAN. But before doing so, I decided to use continuous integration via Travis CI to also test the package on Mac and Linux. Turns out the package did not work as intended everywhere.

The problem occurred on OS X and was the dreaded:

clang: error: unsupported option '-fopenmp'

Long story short: the problem is that the package makes use of the Armadillo C++ library for linear algebra via RcppArmadillo. Armadillo has support for OpenMP, but under an out-of-the-box OS X installation, there is no OpenMP support. From R version 3.4.0, R provides support for OpenMP. So essentially, R and (Rcpp)Armadillo attempts to use OpenMP, but OS X is unable to handle this (unless you fix this yourself).

The solution I ended up using is having a system-dependent Makevars file (which sets up links to libraries), which turns on or off OpenMP support. In the following, I will briefly document the steps I took to set this up.

The standard Makevars file

The standard Makevars file I use for RcppArmadillo-dependent packages is as follows:


As this shows, there are two links here to OpenMP libraries. On Linux, we want to keep this as it is. On OS X (if there is no OpenMP support), we’d like to turn this off. In particular, the following Makevars allows the package to compile on OS X by turning off OpenMP:


So how can such a system-dependent Makevars be created?

Using a configure script

The main part to get this all up and running is the inclusion of a configure script. Essentially, the purpose of the configure script is to configure the Makevars file based on the system specifications.

First, I added the following code taken from the RcppArmadillo package into the main directory of my package in a file called (note the name and version of the package in AC_INIT):

## -*- mode: autoconf; autoconf-indentation: 4; -*-
##  Copyright (C) 2016 - 2017  Dirk Eddelbuettel for
##  the RcppArmadillo package. Licensed under GPL-2 or later
##  This file is a subset of the used by
##  RcppArmadillo, adapted to the mfbvar package by
##  Sebastian Ankargren

## require at least autoconf 2.61

## Process this file with autoconf to produce a configure script.

## Set R_HOME, respecting an environment variable if one is set
: ${R_HOME=$(R RHOME)}
if test -z "${R_HOME}"; then
	AC_MSG_ERROR([Could not determine R_HOME.])
## Use R to set CXX and CXXFLAGS
CXX=$(${R_HOME}/bin/R CMD config CXX)

## We are using C++

## Default the OpenMP flag to the empty string.
## If and only if OpenMP is found, expand to $(SHLIB_OPENMP_CXXFLAGS)

## Check for broken systems produced by a corporation based in Cupertino
RSysinfoName=$("${R_HOME}/bin/Rscript" --vanilla -e 'cat([["sysname"]])')
if test x"${RSysinfoName}" == x"Darwin"; then
   AC_MSG_WARN([OpenMP unavailable and turned off.])
   AC_MSG_RESULT([not found as on ${RSysinfoName}])
   ## Check for OpenMP
   AC_MSG_CHECKING([for OpenMP])
   ## if R has -fopenmp we should be good
   allldflags=$(${R_HOME}/bin/R CMD config --ldflags)
   hasOpenMP=$(echo ${allldflags} | grep -- -fopenmp)
   if test x"${hasOpenMP}" == x""; then

AC_SUBST([OPENMP_CFLAG], ["${openmp_cflag}"])
AC_SUBST([OPENMP_FLAG], ["${openmp_flag}"])

Next, remove src/Makevars and add src/


Now, what will happen is this: a system-specific src/Makevars is created based on the template file src/, where the variables @OPENMP_FLAG@ and @OPENMP_CFLAG@ are replaced by the appropriate values, which are in turn obtained in the file. Thus, depending on which is more appropriate, either of the two Makevars files described in the previous section will be created.

Finally, it is also appropriate to add a cleanup file (also in the main directory) so that created files are removed:


rm -f config.* src/Makevars

Running autoconf

At this point, I first thought I was done and it would work. But to my surprise, it did not. What I was missing was a final but important step: to create a configure file from the script. This is easily done on a Linux computer by the following terminal statement:

autoconf --output=configure

Next, just copy the newly-created (and incredibly long) configure file into the main directory of the package.

At this point, the package should compile on both Linux and OS X, with OpenMP support being dynamically enabled.

Fixing line endings

However, there’s rarely a problem fix that doesn’t cause a new problem, and this is no exception. When checking the package on win-builder, I received a warning about CRLF (Windows-style) line endings in and cleanup, which should be LF (Unix-style endings). Luckily, this can easily be fixed automatically by GitHub.

First, set the default for the repository to be LF:

git config --global core.autocrlf input

Next, create .gitattributes in the main directory, containing the following:

^configure\.ac$ text eol=lf
^cleanup$ text eol=lf

This forces the line endings to always be LF when pushing to GitHub. (You will also need to add ^/\.gitattributes$ to .Rbuildignore.)