473,322 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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 2170
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Markus Faust | last post by:
Hi, I'm trying to link Fortran files generated with “Compaq Visual Fortran Optimizing Compiler Version 6.6 (Update B)” under “Enthought Edition build 1028, Python 2.3 (#46, Aug 11 2003,...
4
by: NM | last post by:
Hello All I am writing some progam that involves both C++ and Fortran. Some of the existing code is in Fortran. The main program will be in C++ and it will call some Fortran subroutine. All the...
6
by: Santosh | last post by:
Hello all, I am trying to interface c++ and fortran, the fortran code is already there and I was successful in calling the subroutines and functions, but was not able to send the fortran module...
1
by: Sam | last post by:
Hello all I have a two dimensional array (the dimensions are not known) that needs to be passed to fortran from c++, allocate the dimensions of the array in fortran code, do some filling up of...
2
by: NM | last post by:
Hello all, I am supposed to do some mixed programming with c++ and fortran. I was succeeful in exchanging the 2D arrays from fortran to c++ and the other way, but was unable to that same with...
2
by: Pavan Zope | last post by:
Hello I am facing a weired problem in calling a fortran routine from c. The fortran function in question is like this:- subroutine fortran_fun(idtab, nx, x, nf, nh, nhd, info, rinfo, fa, ga,...
81
by: Matt | last post by:
I have 2 questions: 1. strlen returns an unsigned (size_t) quantity. Why is an unsigned value more approprate than a signed value? Why is unsighned value less appropriate? 2. Would there...
2
by: Ray J. | last post by:
I have a C++ program written and compiled on Solaris 8 with gcc. With gcc lets me compile fortran code along with the C++ program to be able to call the fortran code as a subroutine. The...
4
by: sara_patty | last post by:
fortran command for c command strcopy(infile, argv)
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.