using fortran modules in c++

S

Santosh

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
 
V

Victor Bazarov

Santosh said:
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
 
L

Larry I Smith

Santosh said:
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.
 
P

Paul Schneider

Santosh said:
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
 
B

beliavsky

Gernot said:
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.
 
E

E. Robert Tisdale

Santosh said:
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
 

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

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top