[Rd] Detecting compilation under R

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Thu Dec 14 16:14:45 CET 2006


The docs tell me:

"The header files define USING_R, which should be used to test if the 
code is indeed being used with R."

but surely you only get that if you #include <R.h>, which you can only 
do if you are using R. If you have code that you might want to compile 
in R and for other purposes, how can you detect this?

  As an example, suppose I have some C that uses GSL to get a random 
number if its a standalone program, or uses R's internals if its being 
compiled to run in R. I want to do something like:

#ifdef USING_R
#include <R.h>
#else
#include <gsl_random.h>
#endif

  and then:

#ifdef USING_R
  x = rand_unif(0.0,1.0);
#else
  x = gsl_runif(0.0,1.0);
#endif

  (cant remember the exact names but hopefully you get the idea). This 
fails because USING_R is only set when R.h is included.

  Are there any preprocessor definitions set by R CMD SHLIB and R CMD 
BUILD that I can test against?

  Of course I can stick PKG_CFLAGS=-DYES_THIS_IS_USING_R in Makevars, 
but I just wondered if there was a default solution I'd missed. Or if it 
was a good idea anyway.

Barry



More information about the R-devel mailing list