[R] help in R calling C function

Uwe Ligges ligges at statistik.uni-dortmund.de
Sun Jun 27 12:49:43 CEST 2004


Lei Liu wrote:
> Hi there,
> 
> I want to call a C function in R. I have some experience on it, but this 
> time I need to call another C function in the "main" C function. As a 
> simple example, I use the following C code:
> 
> #include <stdio.h>
> #include <math.h>
> #include <stdlib.h>
> #include <time.h>
> 
> 
> void main(double *alpha)

1. don't call it "main".
2. alpha must not be a pointer.


> {
>     double test();
>     double beta;
>     beta= *alpha *2 + test(*alpha);
> }
>
> double test(double *alpha)
> {
>     double value;
>     value = *alpha *3;
>     return(value);
> }
> 
> I used "gcc -c main.c" and "Rcmd SHLIB main.o" and I got the main.dll 
> file. In R I use the following code to call the C function "main":

3. "Rcmd SHLIB main" is sufficient.


> dyn.load("c:/Program Files/R/rw1060/bin/main.dll")

4. Why do you compile in R's /bin directory? That's not necessary, it's 
rather confusing.

Uwe Ligges

> a=4.2
> b=.C('main', as.double(a))
>
> But the R system crashed. I know I can only define the function type in 
> C as "void" to be called by R. But what if I want to call another C 
> function in the "main" C function? Thank you for your help!
> 
> Sincerely,
> 
> Lei Liu
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html




More information about the R-help mailing list