| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"Santosh" |
| Date: |
26 Jun 2005 07:32:10 AM |
| Object: |
using fortran modules in c++ |
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 data into my C++ code and viceversa. I used gcc 3.3 and
g95 for c++ and fortran resp. Here is a little made up sample code so
that u could understand my problem.
$cat fort.f90
MODULE foo
real*8 a, b, c
INTEGER d
END MODULE foo
subroutine just ()
use foo
......
! some code which manipulates with the variables in the module just
! Now after manipulation i want them to be sent to my c++ code
......
end subroutine
$cat main.cpp
#include <iostream.h>
#include <math.h>
extern "C" { int just_ ( );}
// is there any structure for the module as well so that i could
//use its variables in c++
int main()
{
just_( );
// I actually wanted to access variables a,b of the module manipulate
them
// and pass it to the fortran like below
// can i do something like just_(&a,&b)
// do some manipulation in fortran and access the latest data in c++
return 0;
}
It wud be great if anyone could explain me how to do this,
Thanx in advance
Regards
Santosh
.
|
|
| User: "Gernot Frisch" |
|
| Title: Re: using fortran modules in c++ |
28 Jun 2005 04:16:43 AM |
|
|
Maybe using a program like "f2c" (fortran to C) would help?
<duck>
-Gernot
.
|
|
|
| User: "" |
|
| Title: Re: using fortran modules in c++ |
28 Jun 2005 07:16:28 AM |
|
|
Gernot Frisch wrote:
Maybe using a program like "f2c" (fortran to C) would help?
I'm afraid it would not help, since f2c translates Fortran 77 to C, and
modules were introduced in Fortran 90.
.
|
|
|
|
|
| User: "Larry I Smith" |
|
| Title: Re: using fortran modules in c++ |
26 Jun 2005 01:02:19 PM |
|
|
Santosh wrote:
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 data into my C++ code and viceversa. I used gcc 3.3 and
g95 for c++ and fortran resp. Here is a little made up sample code so
that u could understand my problem.
[snip]
Regards
Santosh
Try the gnu.g++.help newsgroup.
.
|
|
|
|
| User: "Paul Schneider" |
|
| Title: Re: using fortran modules in c++ |
26 Jun 2005 01:04:52 PM |
|
|
Santosh wrote:
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 data into my C++ code and viceversa. I used gcc 3.3 and
g95 for c++ and fortran resp. Here is a little made up sample code so
that u could understand my problem.
[snip]
It is a little bit off topic, but check out the example in
http://seehuhn.de/comp/linear#lapack-hard
You can also browse through the portland compiler manual. It will give
you interoperability in all directions. You will get the idea for any
compiler/platform from this platform.
Best,
Paul
.
|
|
|
|
| User: "E. Robert Tisdale" |
|
| Title: Re: using fortran modules in c++ |
28 Jun 2005 10:27:30 AM |
|
|
Santosh wrote:
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
What do you mean by "send"?
the Fortran module data into my C++ code and viceversa.
I used gcc 3.3 and g95 for C++ and Fortran respexctively.
Here is a little made up sample code so
that you could understand my problem.
$cat fort.f90
MODULE foo
real*8 a, b, c
INTEGER d
END MODULE foo
subroutine just ()
use foo
! .....
! some code which manipulates with the variables in the module just
! Now after manipulation i want them to be sent to my c++ code
! .....
end subroutine
$cat main.cpp
#include <iostream.h>
#include <math.h>
extern "C" { void just_(void); }
// Is there any structure for the module as well
// so that I could use its variables in C++
int main(int argc, char* argv[]) {
just_();
// I actually wanted to access variables a, b of the module,
// manipulate them and pass it to the Fortran like below.
// Can I do something like just_(&a, &b),
// do some manipulation in Fortran and access the latest data in C++
return 0;
}
cat fort.f90
MODULE foo
real*8 a, b, c
INTEGER d
END MODULE foo
g95 -c fort.f90
nm fort.o
0000000000000000 B foo_MP_a
0000000000000018 B foo_MP_b
0000000000000008 B foo_MP_c
0000000000000010 B foo_MP_d
Unfortunately, there is no standard for mangling
module variable names to generate the symbols left behind
in the object file for the link editor.
Some compilers generate symbols that aren't even
valid C identifiers so you can't reference them
from a C (or C++) program directly.
cat just.f90
subroutine just()
use foo
a = 13.0
b = 42.0
end subroutine just
subroutine get(x, y)
use foo
real, intent(out)::x, y
x = a
y = b
end subroutine get
g95 -c just.f90
cat main.cc
#include <iostream>
extern "C" {
void just_(void);
void get_(float&, float&);
}
int main(int argc, char* argv[]) {
just_();
float a, b;
get_(a, b);
std::cout << "a = " << a << '\t'
<< "b = " << b << std::endl;
return 0;
}
g++ -Wall -ansi -pedantic -o main main.cc just.o fort.o
./main
a = 13 b = 42
.
|
|
|
|
| User: "Victor Bazarov" |
|
| Title: Re: using fortran modules in c++ |
26 Jun 2005 09:35:40 AM |
|
|
Santosh wrote:
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 data into my C++ code and viceversa. I used gcc 3.3 and
g95 for c++ and fortran resp. Here is a little made up sample code so
that u could understand my problem.
[...]
Interlanguage connectivity is extremely compiler-specific and as such
is OT here. Please ask in a newsgroup for your compiler.
V
.
|
|
|
|

|
Related Articles |
|
|