Lahey Support
08-15-2003, 01:19 AM
=0D
Jim, I think the answer to your question might depend on what you mean=
by "routine". The following code shows Fortran calling a C++ class member=
function through an intermediate function declared as extern "C" . =
I'm not sure how to call class member functions directly from Fortran. Do=
es any one know for example how to use MFC classes from Fortran? =0D
=0D
-David=0D
=0D
Here's the code:=0D
=0D
=0D
PROGRAM lfmsvc=0D
ML_EXTERNAL cpp=0D
REAL(8) :: c =3D 0.5, T =3D 298.15=0D
CALL cpp(c, T)=0D
PRINT*,'main complete'=0D
READ*=0D
STOP=0D
END=0D
=0D
=0D
// header isotherm.h=0D
=0D
#include <math.h>=0D
const double R =3D 8.314;=0D
class CLangmuir {=0D
protected:=0D
double dQ0, dk0, dH;=0D
public:=0D
CLangmuir (double Q0, double k0, double H);=0D
double q (double c, double T);=0D
double c (double q, double T);=0D
};=0D
=0D
CLangmuir::CLangmuir (double Q0, double k0, double H) =0D
{=0D
dQ0 =3D Q0;=0D
dk0 =3D k0;=0D
dH =3D H;=0D
};=0D
=0D
double CLangmuir::q (double c, double T)=0D
{=0D
double k;=0D
k =3D dk0*exp(dH/(R*T));=0D
return dQ0*k*c/(1+k*c);=0D
};=0D
=0D
double CLangmuir::c (double q, double T)=0D
{=0D
double k;=0D
k =3D dk0*exp(dH/(R*T));=0D
return q/(dQ0*k);=0D
}=0D
// Main .cpp=0D
#include <iostream.h>=0D
extern "C" void cpp(double* c, double* T)=0D
{=0D
CLangmuir Langmuir(1.0, 0.01, 10000);=0D
double q;=0D
cout << *c << endl << *T << endl;=0D
q =3D Langmuir.q(*c, *T); =0D
cout << "Langmuir called from Fortran 95" << endl;=0D
cout << " q =3D " << q << endl;=0D
}=0D
=0D
Using MSVC compile with:=0D
=0D
cl /c cppname.cpp=0D
=0D
lf95 fname.for cppname.obj -winconsole -ml MSVC -staticlink=0D
=0D
=0D
=0D
----------=0D
From: Jim Kress=0D
Sent: Monday, February 08, 1999 3:07 PM=0D
To: [address removed]
Subject: [LF] Call C++ routines from LF95=0D
=0D
I am working with another person who is developing a set of C++ routines fo=
r=0D
some data processing. Is there anything special he needs to do with his C+=
+=0D
routines to enable me to call them from LF95?=0D
=0D
Jim=0D
=0D
=0D
Jim, I think the answer to your question might depend on what you mean=
by "routine". The following code shows Fortran calling a C++ class member=
function through an intermediate function declared as extern "C" . =
I'm not sure how to call class member functions directly from Fortran. Do=
es any one know for example how to use MFC classes from Fortran? =0D
=0D
-David=0D
=0D
Here's the code:=0D
=0D
=0D
PROGRAM lfmsvc=0D
ML_EXTERNAL cpp=0D
REAL(8) :: c =3D 0.5, T =3D 298.15=0D
CALL cpp(c, T)=0D
PRINT*,'main complete'=0D
READ*=0D
STOP=0D
END=0D
=0D
=0D
// header isotherm.h=0D
=0D
#include <math.h>=0D
const double R =3D 8.314;=0D
class CLangmuir {=0D
protected:=0D
double dQ0, dk0, dH;=0D
public:=0D
CLangmuir (double Q0, double k0, double H);=0D
double q (double c, double T);=0D
double c (double q, double T);=0D
};=0D
=0D
CLangmuir::CLangmuir (double Q0, double k0, double H) =0D
{=0D
dQ0 =3D Q0;=0D
dk0 =3D k0;=0D
dH =3D H;=0D
};=0D
=0D
double CLangmuir::q (double c, double T)=0D
{=0D
double k;=0D
k =3D dk0*exp(dH/(R*T));=0D
return dQ0*k*c/(1+k*c);=0D
};=0D
=0D
double CLangmuir::c (double q, double T)=0D
{=0D
double k;=0D
k =3D dk0*exp(dH/(R*T));=0D
return q/(dQ0*k);=0D
}=0D
// Main .cpp=0D
#include <iostream.h>=0D
extern "C" void cpp(double* c, double* T)=0D
{=0D
CLangmuir Langmuir(1.0, 0.01, 10000);=0D
double q;=0D
cout << *c << endl << *T << endl;=0D
q =3D Langmuir.q(*c, *T); =0D
cout << "Langmuir called from Fortran 95" << endl;=0D
cout << " q =3D " << q << endl;=0D
}=0D
=0D
Using MSVC compile with:=0D
=0D
cl /c cppname.cpp=0D
=0D
lf95 fname.for cppname.obj -winconsole -ml MSVC -staticlink=0D
=0D
=0D
=0D
----------=0D
From: Jim Kress=0D
Sent: Monday, February 08, 1999 3:07 PM=0D
To: [address removed]
Subject: [LF] Call C++ routines from LF95=0D
=0D
I am working with another person who is developing a set of C++ routines fo=
r=0D
some data processing. Is there anything special he needs to do with his C+=
+=0D
routines to enable me to call them from LF95?=0D
=0D
Jim=0D
=0D
=0D