Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change and set Rcpp compile arguments

Tags:

c++

r

c++11

rcpp

I created a new Rcpp package (by using RStudio). This package contains a C++ function that is compiled by using the following compiler options:

clang++ -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I/usr/local/include -I/usr/local/include/freetype2 -I/opt/X11/include -I"/Library/Frameworks/R.framework/Versions/3.2/Resources/library/Rcpp/include" -fPIC -Wall -mtune=core2 -g -O2 -c RcppExports.cpp -o RcppExports.o

I would like to change/set these arguments, for example remove -g, add -std=c++11 and change the argument -O2 to -O3. Also, it would be better to have the possibility to specify these changes once (for the package).

like image 861
Nick Avatar asked Sep 15 '15 12:09

Nick


1 Answers

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).
like image 165
nrussell Avatar answered Nov 18 '22 03:11

nrussell