469,929 Members | 1,456 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,929 developers. It's quick & easy.

using ranlux in fortran

dear friends,
i am new to C/C++ and i need to call ranlux routine from my fortran
code...here is a toy fortran code, which is calling the C++ program
that uses ranlux(its actually C code given in gsl manual, i transferd
it to C++ headers. hope this is fine)
i have read the available sources, but with little luck.
can anybody show me how to call it from the fortran code?
#include <iostream>
#include <gsl/gsl_rng.h>
using namespace std;
extern "C" /* this line should be omitted in C */
{
float hello_()
{
const gsl_rng_type * T;
gsl_rng * r;

int i, n = 10;

gsl_rng_env_setup();

T = gsl_rng_default;
r = gsl_rng_alloc (T);

for (i = 0; i < n; i++)
{
double u = gsl_rng_uniform (r);
printf ("%.5f\n", u);
}

gsl_rng_free (r);

return 0;
}
}
extern "C" int mymain_();
int main()
{
return mymain_();
}
and corresponding fortran code looks like:

SUBROUTINE MYMAIN

CHARACTER*11 NAME

INTEG=42
NAME='HELLO WORLD'
CALL hello()

RETURN
END

can anybody plz let me know fault?
Jun 27 '08 #1
4 1995
rudra wrote:
dear friends,
i am new to C/C++ and i need to call ranlux routine from my fortran
code...here is a toy fortran code, which is calling the C++ program
that uses ranlux(its actually C code given in gsl manual, i transferd
it to C++ headers. hope this is fine)
i have read the available sources, but with little luck.
can anybody show me how to call it from the fortran code?
Somebody in a Fortran newsgroup, probably. C++ does not define any
calling convention with any other language except C.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #2
C++ does not define any
calling convention with any other language except C.
Really? oops!!! and internet is flooded with fortran calling C++ and
vice versa....thnx anyway
Jun 27 '08 #3
rudra wrote:
> C++ does not define any
calling convention with any other language except C.

Really? oops!!! and internet is flooded with fortran calling C++ and
vice versa....
Really. Whatever you have to do to make your Fortran and your C++
code talk to each other is *not* defined by the C++ Standard. I have
no idea whether it is defined by the Fortran Standard, you'd have to
ask in the Fortran newsgroup. However, last time I had to connect
the two was about 16 years ago, and it was compiler-specific, since
both compilers were MS, IIRC. Try reading your compiler documentation.

It is very likely that within, say, the same family of compilers the
calling conventions are well-established. I would even go as far as
to venture *a guess* that in the C++ program you need to declare your
C++ functions as 'extern "C" ' to be able to communicate to Fortran.
Of course, nothing prevents the compiler maker from providing some
kind of 'extern "Fortran" ' specification. The language allows to
have any language name inside the double quotes, except that it just
does *not* define the effects of it.
thnx anyway
You're welcome anyway

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #4
On Apr 25, 2:59 pm, rudra <bnrj.ru...@gmail.comwrote:
dear friends,
i am new to C/C++ and i need to call ranlux routine from my fortran
code...here is a toy fortran code, which is calling the C++ program
that uses ranlux(its actually C code given in gsl manual, i transferd
it to C++ headers. hope this is fine)
i have read the available sources, but with little luck.
can anybody show me how to call it from the fortran code?

#include <iostream>
#include <gsl/gsl_rng.h>
using namespace std;
extern "C" /* this line should be omitted in C */
{
float hello_()
{
const gsl_rng_type * T;
gsl_rng * r;

int i, n = 10;

gsl_rng_env_setup();

T = gsl_rng_default;
r = gsl_rng_alloc (T);

for (i = 0; i < n; i++)
{
double u = gsl_rng_uniform (r);
printf ("%.5f\n", u);
}

gsl_rng_free (r);

return 0;
}
}

extern "C" int mymain_();
int main()
{
return mymain_();
}
and corresponding fortran code looks like:

SUBROUTINE MYMAIN

CHARACTER*11 NAME

INTEG=42
NAME='HELLO WORLD'
CALL hello()

RETURN
END

can anybody plz let me know fault?
As Victor suggested, this is not possible through standard C++. But,
AFAIK, F2003 standard offers some binding for Fortran and C/C++ mixed
language programming. But these are also not portable and platform
dependent. (I did also some tries and posts in Fortran newsgroup,
search my name,'utab', there(of course, you can also find other posts
by other people) and see if they can be of help.)

HTH,

Umut
Jun 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Markus Faust | last post: by
4 posts views Thread by NM | last post: by
6 posts views Thread by Santosh | last post: by
2 posts views Thread by Pavan Zope | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.