How to change and set Rcpp compile arguments

Working off Writing R Extension, Section 1.2, it seems like you should be able to handle this with a couple of shell scripts. As a minimal example, (working on a Linux machine), I created a basic package from Rcpp::Rcpp.package.skeleton, and put the following two files in the project root directory:

configure

#!/bin/bash
if [ ! -d "~/.R" ]; then
  mkdir ~/.R; touch ~/.R/Makevars
  echo "CXXFLAGS= -O3 -std=c++11 -Wall -mtune=core2" > ~/.R/Makevars
elif [ ! -e "~/.R/Makevars" ]; then
  touch ~/.R/Makevars
  echo "CXXFLAGS= -O3 -std=c++11 -Wall -mtune=core2" > ~/.R/Makevars
else
  mv ~/.R/Makevars ~/.R/Makevars.bak_CustomConfig
  echo "CXXFLAGS= -O3 -std=c++11 -Wall -mtune=core2" > ~/.R/Makevars
fi

cleanup

#!/bin/bash
if [ -e "~/.R/Makevars.bak_CustomConfig" ]; then
  mv -f ~/.R/Makevars.bak_CustomConfig ~/.R/Makevars
fi

and then made them executable (chmod 777 path/to/project/root/configure and chmod 777 path/to/project/root/cleanup). When I ran Build and Reload I got (excerpt):

g++ -m64 -I/usr/include/R -DNDEBUG  
-I/usr/local/include 
-I"/home/nr07/R/x86_64-redhat-linux-gnu-library/3.2/Rcpp/include"  
-fpic  -O3 -std=c++11 -Wall -mtune=core2
-c rcpp_hello.cpp -o rcpp_hello.o

g++ -m64 -shared -L/usr/lib64/R/lib 
-Wl,-z,relro -o CustomConfig.so RcppExports.o rcpp_hello.o 
-L/usr/lib64/R/lib -lR

which overrides the R Makevars defaults, and uses the correct options.


This was just a basic example, so you probably would want to take it a couple steps further, depending on your goals:

  1. Adapt the scripts for different platforms (e.g. Unix/Linux vs Windows/Windows 64-bit), which is touched on in the linked article I believe.
  2. Ensure that setting the permissions of the file from your machine is sufficient to for these files to be executed on a different computer (I think it will work, but I'm not completely positive).

Tags:

C++

C++11

R

Rcpp