Re: how to use fortran modules in c++
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
.
Relevant Pages
- Re: aliased intent(in out) arguments
... > Is the program below standard-conforming Fortran 95? ... None of the Windows Fortran 95 compilers I tried ... subroutine calls, etc., the rule isn't one that compilers ... > end module foo ... (comp.lang.fortran) - Re: Fortran based MEX w/ COMMON/SAVE
... I believe dat (pointer to the array) and the ... SAVE at the begining of each subroutine is a bit unusual to me. ... > to unload it if there isn't a Fortran END or STOP statement. ... interaction w/ Matlab). ... (comp.soft-sys.matlab) - Re: Help Constructing Fictional Cross-Religious Movement
... You know how to do something in Fortran that I ... >don't know how to do despite being pretty familiar with it, COBOL ... > subroutine point ... >was finding a programming problem that seemed reasonably amenable to ... (rec.arts.sf.composition) - Re: Callbacks - Delphi and CVF
... | I have a FORTRAN DLL that does the computations and takes about hours ... | want to be able to access the callback functions address from every ... | subroutine in the fortran dll. ... SUBROUTINE ReportProgress ... (comp.lang.fortran) - Re: Callbacks - Delphi and CVF
... | I have a FORTRAN DLL that does the computations and takes about hours ... | want to be able to access the callback functions address from every ... | subroutine in the fortran dll. ... SUBROUTINE ReportProgress ... (comp.lang.fortran) |
|