I actually deleted a part of my reply, dealing with exactly that. Sorry!
You need to declare the C wrapper to take a pointer to a double ("double
*") rather than a double. C passes by value whereas Fortran passes by
reference; in C you get this effect by passing a pointer to the value
(which is also a value). So you want
double F77_SUB(mygammafn)(double *x) { return gammafn(x); }
That should work; if not let me know and I'll look more carefully at
Fortran <-> C conventions.
Reid Huntsinger
-----Original Message-----
From: Gilles GUILLOT [mailto:gilles.guillot at inapg.inra.fr]
Sent: Wednesday, June 15, 2005 3:50 AM
To: Huntsinger, Reid
Subject: Re: [Rd] Calling C from Fortran
Thanks Reid!
And now if I want to call a C function with arguments,
e.g. to compute the gamma function at x,
my C wrapper is:
#include <R.h>
#include <Rmath.h>
void F77_SUB(rndstart)(void) { GetRNGstate(); }
void F77_SUB(rndend)(void) { PutRNGstate(); }
double F77_SUB(normrnd)(void) { return norm_rand(); }
double F77_SUB(mygammafn)(double x) { return gammafn(x); }
And my Fortran is:
subroutine testit()
implicit none
double precision normrnd, x, y, mygammafn
call rndstart()
x = dabs(normrnd())
write(*,*) 'x=',x
call rndend()
y = mygammafn(x)
write(*,*) 'y=',y
end
And it does not work, all calls of testit return the same y value.
What is incorrect in my files ?
Gilles
---------------------------------------------------------------------------
--- Notice: This e-mail message, together with any attachments, contains
information of Merck & Co., Inc. (One Merck Drive, Whitehouse Station, New
Jersey, USA 08889), and/or its affiliates (which may be known outside the
United States as Merck Frosst, Merck Sharp & Dohme or MSD and in Japan, as
Banyu) that may be confidential, proprietary copyrighted and/or legally
privileged. It is intended solely for the use of the individual or entity
named on this message. If you are not the intended recipient, and have
received this message in error, please notify us immediately by reply
e-mail and then delete it from your system.
---------------------------------------------------------------------------
---