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

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

	[[alternative HTML version deleted]]

