[R] Rcpp with OpenMP - Need example Makevars

Asis Hallab asis.hallab at gmail.com
Fri May 24 11:10:08 CEST 2013

Dear R experts,

recently I started developing a Rcpp package "OpenMPTest".
Within that package I want to use OpenMP, as in the following code example:

// header file
#include <omp.h>
using namespace Rcpp ;
RcppExport SEXP testOpenMP( SEXP nThreads ) ;

// cpp file
SEXP testOpenMP( SEXP nThreads ) {

  NumericVector numberThreads = NumericVector( nThreads );
  omp_set_num_threads( numberThreads(0) );
  #pragma omp parallel
    // Code inside this region runs in parallel.


As I am an absolute newbie with writing C++ extensions and have not
much understanding of the Makevars file, I am unsure what to write
into it. Currently I have:

## Use the R_HOME indirection to support installations of multiple R version
PKG_LIBS = `$(R_HOME)/bin/Rscript -e "Rcpp:::LdFlags()"`
## -- compiling for OpenMP
## -- linking for OpenMP
PKG_LIBS= -fopenmp -lgomp

Obviously compilation fails with error:

Symbol not found: __ZTIN4Rcpp19index_out_of_boundsE

With standard setup of the above Makevars I get the
Symbol not found: omp_set_num_threads

Inline also does not work:

fb <- 'omp_set_num_threads(10);
+ #pragma omp parallel
+ { Rf_PrintValue(wrap("HALLO JUPP")); }
+ '
> funk <-  cxxfunction( signature(), body=fb, plugin='Rcpp' )

error: ‘omp_set_num_threads’ was not declared in this scope

How do set the PKG_LIBS -and eventually other variables- to all
required values in a single statement?
Could anyone provide me with a working Makevars example, please?

Help will be much appreciated!
Kind regards and have a nice weekend!

More information about the R-help mailing list