using ranlux in fortran

R

rudra

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?
 
R

rudra

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
 
U

utab

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top