[R] C interface

Romain Francois romain.francois at dbmail.com
Fri Jun 18 17:50:45 CEST 2010


Hello,

This is not the appropriate mailing list. Use R-devel for questions 
about C, etc ...

One thing that might help you is the inline package.

require( inline )
fx <- cfunction( signature( s = "numeric" ), '

   SEXP result;
   PROTECT(result = NEW_NUMERIC(1));
   double* ptr=NUMERIC_POINTER(result);
   double t = *REAL(s);
   double u = t-floor(t)-0.5;
   if(u>0) *ptr=-1+4*u; else *ptr=-1-4*u;
   Rprintf("The value is %f", *ptr);
   UNPROTECT(1);
   return result;
', verbose = TRUE )
fx( 10 )

The verbose = TRUE argument will show you how inline runs the show.

Romain

Le 18/06/10 16:18, michael meyer a écrit :
>
> Greetings,
>
> I am trying to call simple C-code from R.
> I am on Windows XP with RTools installed.
>
> The C-function is
>
> #include<R.h>
> #include<Rinternals.h>
> #include<Rmath.h>
> #include<Rdefines.h>
>
> // prevent name mangling
> extern "C" {
>
> SEXP __cdecl test(SEXP s){
>
>    SEXP result;
>    PROTECT(result = NEW_NUMERIC(1));
>    double* ptr=NUMERIC_POINTER(result);
>    double t = *REAL(s);
>    double u = t-floor(t)-0.5;
>    if(u>0) *ptr=-1+4*u; else *ptr=-1-4*u;
>    Rprintf("The value is %f", *ptr);
>    UNPROTECT(1);
>    return result;
> }
>
> };
>
> It is compiled with
>
> R CMD SHLIB source.c

If you want C++, then name your file source.cpp

SHLIB compiles files with .c extensions with a C compiler, which will 
not be happy about extern "C"

> with flag
>
> MAKEFLAGS="CC=g++"
>
> If I compile with the default flags I get an error message about an
> undefined reference to "__gxx_personality_v0".
> However when I call this code from R with
>
> test<- function(t){
>    .Call("test",t)
> }
> dyn.load("./source.dll")
> test(0)
> dyn.unload("./source.dll")
>
> then R crashes.
> I have a vague idea of the issue of calling conventions and was hoping that
> the __cdecl
> specifier would force the appropriate convention.
> I also have Cygwin installed as part of the Python(x,y) distribution but I
> am assuming that
> R CMD SHLIB source.c
> calls the right compiler.
>
> What could the problem be?
>
> Many thanks,
>
> Michael


-- 
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://bit.ly/98Uf7u : Rcpp 0.8.1
|- http://bit.ly/c6YnCi : graph gallery collage
`- http://bit.ly/bZ7ltC : inline 0.3.5



More information about the R-help mailing list