Re: [R] Calling C from Fortran

From: Shusong Jin <shusong.jin_at_gmail.com>
Date: Wed 15 Jun 2005 - 11:19:28 EST

Dear Gilles,

You can try this.
Save the fortran file as 1.f
Save the C file as 2.c
Then
R CMD SHLIB 1.f 2.c
You should obtain a file named 1.so.

Then start you R program, then
dyn.load('/where/is/your/1.so')
.Fortran("testit")

You should obtain a random number which is normally distributed.

Good luck.

Jin

On 6/15/05, Gilles GUILLOT <gilles.guillot@inapg.inra.fr> wrote:
> I would like to call C routines from Fortran as suggested in section 5.6 of
> the "Writing R extensions" documentation.
>
> I'm familiar with Fortran but not with C.
> I understand the example provided in Fortran:
>
> subroutine testit()
> double precision normrnd, x
> call rndstart()
> x = normrnd()
> call dblepr("X was", 5, x, 1)
> call rndend()
> end
>
>
> but I don't understand the purpose of this C wrapper:
> #include <R.h>
> void F77_SUB(rndstart)(void) { GetRNGstate(); }
> void F77_SUB(rndend)(void) { PutRNGstate(); }
> double F77_SUB(normrnd)(void) { return norm_rand(); }
>
> neither how I should compile it.
>
> Could anyone explain how I should compile and link
> the C and Fortran files above, and call the Fortran subroutine from R.
>
> Thanks,
>
> Gilles
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Wed Jun 15 11:23:50 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:32:39 EST