Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29583 Discussões

Calling C++ function from Fortran: problem with parameter passing

pavel_sorokin
Principiante
1.545 Visualizações
Hi,

I noticed that when I'm calling a C++ function from my Fortran code, the parameters lose their values.

The C++ function is declared as follows:
extern "C" void __cdecl
GerkCallback(double &inCurrentTime
double *fortranStateProbabilities,
double *fortranStateProbabilitiesDelta )
{
...
}

The Fortran code uses the function like this (I combined all the relevant code together):
REAL*8 T [REFERENCE]
EXTERNAL F
A = T
CALL F(A, YG, YGP)
As I debug this code, I see that A equals 0 before calling F, but when I get to the C++ function, the corresponding parameter inCurrentTime takes some random value. The other params are mangled as well. Both C++ and Fortran code are compiled with the _cdecl calling convention. Environment: VC++ .Net, Intel Fortran 7.1.

Pavel
0 Kudos
2 Respostas
Jugoslav_Dujic
Contribuidor valorado II
1.546 Visualizações
Hmmm... too little data to bring a definitive conclusion. For the start, are A, YG, and YGP double precision variables? (I assume they are -- even if they aren't, symptoms would be different).

Try watching LOC(A), LOC(YG) and LOC(YGP) on Fortran side, and addresses of Callback's dummy arguments on C++ side. Are they the same?

Jugoslav
pavel_sorokin
Principiante
1.546 Visualizações
I found the answer:

The variable A was typed implicitly, and apparently its type was not "REAL*8" (which is the same as double, which was expected by the C++ code). When I explicitly specified the type of A as REAL*8, the problem went away.
Responder