[R] C interface

From: michael meyer <mjhmeyer_at_googlemail.com>
Date: Fri, 18 Jun 2010 16:18:14 +0200


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);
  return result;


It is compiled with

R CMD SHLIB source.c

with flag


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){

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,


        [[alternative HTML version deleted]]

R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Fri 18 Jun 2010 - 14:25:59 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 18 Jun 2010 - 16:00:33 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive