PDA

View Full Version : Accessing Fortran module data with C++


kdurgarao
09-15-2009, 12:36 PM
Hello,
I have a difficulty in Mixed language programming. There are many global variables in a fortran module, which have to be accessed from C++ main program. Although the compiler is giving no errors with the following simple structure, but, linker gives errors with - 'undefined reference to the common variable in C++ object'.

fortran module:

MODULE EXAMP
SAVE

REAL, X(4), Y(4), Z(4)
REAL, PARAMETER :: Pi =3.14159

END MODULE EXAMP

C++:
#include<iostream>

extern "C" float EXAMP_mp_X[4];
extern "C" float EXAMP_mp_Y[4];
extern "C" float EXAMP_mp_Z[4];
extern "C" float EXAMP_mp_Pi;
using namespace std;
int main()
{
.....
....
}
I am using Linux...g++ and gfortran compiler in the following way
gfortran -c mod.f
g++ -c testc.cpp
g++ -o test mod.o testc.o -llapack

Could anyone please share their experience in handling the similar problem. Thanking you,

tzeis
09-25-2009, 07:27 PM
If gfortran has a function that returns a variables address, you could pass the addresses to C++, which can use them as pointers to the global Fortran data.