Re: [Rd] C Interface

From: Simon Urbanek <simon.urbanek_at_r-project.org>
Date: Fri, 18 Jun 2010 16:01:51 -0700

On Jun 18, 2010, at 10:23 AM, michael meyer <mjhmeyer_at_googlemail.com> wrote:

> 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 OrthoFunctions.c
>
> with flag
>
> MAKEFLAGS="CC=g++"
>

That is entirely wrong - g++ is not a C compiler.

Cheers,
Simon

>
> However when I call this code from R with
>
> test <- function(t){
> .Call("test",t)
> }
> dyn.load("./OrthoFunctions.dll")
> test(0)
> dyn.unload("./OrthoFunctions.dll")
>
> then R crashes.
>
> If I compile with the default flags (no extern "C", no __cdecl) I get an
> error message about an undefined reference to "__gxx_personality_v0":
>
> C:\...>R CMD SHLIB OrthoFunctions.c
> C:/Programme/R/R-2.10.1/etc/Makeconf:151: warning: overriding commands for
> target `.c.o'
> C:/Programme/R/R-2.10.1/etc/Makeconf:142: warning: ignoring old commands for
> target `.c.o'
> C:/Programme/R/R-2.10.1/etc/Makeconf:159: warning: overriding commands for
> target `.c.d'
> C:/Programme/R/R-2.10.1/etc/Makeconf:144: warning: ignoring old commands for
> target `.c.d'
> C:/Programme/R/R-2.10.1/etc/Makeconf:169: warning: overriding commands for
> target `.m.o'
> C:/Programme/R/R-2.10.1/etc/Makeconf:162: warning: ignoring old commands for
> target `.m.o'
> g++ -I"C:/Programme/R/R-2.10.1/include" -O2 -Wall -c
> OrthoFunctions.c -o OrthoFunctions.o
> gcc -shared -s -o OrthoFunctions.dll tmp.def OrthoFunctions.o
> -LC:/Programme/R/R-2.10.1/bin -lR
> OrthoFunctions.o:OrthoFunctions.c:(.eh_frame+0x11): undefined reference to
> `__gxx_personality_v0'
> collect2: ld returned 1 exit status
>
>
>
> 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]]
>
> ______________________________________________
> R-devel_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>
>



R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Fri 18 Jun 2010 - 23:04:38 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 Sat 19 Jun 2010 - 02:41:12 GMT.

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

list of date sections of archive