Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28922 Discussions

Passing values from C++ to Fortran

cfann61
Beginner
930 Views

I've been working on the stdcall example I found on the web at: http://www.neurophys.wisc.edu/comp/docs/notes/not017.htmland I was able to get the C++ to call the Fortran subroutine, but the values from C++ aren't passing to Fortran and vice versa. I've made the code a bit simpler and here's what I've got.

// This illustrates how a Fortran routine and function may be

// called from a main program in C++

#include

float n=10;

float nSq;

extern "C" void __stdcall FR1(float *,float * nSq);

void main()

{

FR1(&n,&nSq);

}

and:

SUBROUTINE

FR1(N,M)

!DEC$ ATTRIBUTES STDCALL, ALIAS:'_FR1@8'::FR1

! COMPUTES THE SQUARE OF N, RETURNS IN M

REAL

M,N

M=N*N

RETURN

END

The value of N when I get into the Fortran subroutine is a random number(i.e. 6.06...E-39). The values of N and M when I get back into the C++ are 10 and 0, respectively. Any help would be appreciated.

Thanks

0 Kudos
7 Replies
Lorri_M_Intel
Employee
930 Views

The "STDCALL" attribute changes the default passing mechanism for numeric argument from "by reference" to "by value". Your C program is passing by reference, so the Fortran program is interpreting the address of these values as the actual values.

Fix is easy; change the attributes to:

!DEC$ ATTRIBUTES STDCALL, REFERENCE, ALIAS:'_FR1@8'::FR1

and it should work for you.

- Lorri

0 Kudos
Steven_L_Intel1
Employee
930 Views
Or my preference:

!DEC$ ATTRIBUTES STDCALL,REFERENCE,DECORATE,ALIAS:"FR1" :: FR1
0 Kudos
cfann61
Beginner
930 Views


Or my preference:

!DEC$ ATTRIBUTES STDCALL,REFERENCE,DECORATE,ALIAS:"FR1" :: FR1


Is there any advantage of one over the other, or is it just a preference?
0 Kudos
cfann61
Beginner
930 Views
Changed Fortran to:









SUBROUTINE





FR1(N,M)

!DEC$ ATTRIBUTES STDCALL,REFERENCE,DECORATE,ALIAS:'FR1'::FR1



! COMPUTES THE SQUARE OF N, RETURNS IN M

















REAL





M,N

M=N*N







RETURN



END







Still not passing value of N.

0 Kudos
Steven_L_Intel1
Employee
930 Views
It worked fine for me when I tried it.
0 Kudos
joerg_kuthe
Novice
930 Views
Since you pass pointers and because you also declared FR1 to be STDCALL conform, don't you have to specify explicitely that you pass N and M by reference?

SUBROUTINE FR1(N,M)

!DEC$ ATTRIBUTES STDCALL, ALIAS:'_FR1@8'::FR1

!DEC$ ATTRIBUTES REFERENCE :: N,M

! COMPUTES THE SQUARE OF N, RETURNS IN M

REAL M,N

...

Hope that helps.

Jrg Kuthe
www.qtsoftware.de

0 Kudos
Jugoslav_Dujic
Valued Contributor II
930 Views

Quoting - joerg_kuthe

Since you pass pointers and because you also declared FR1 to be STDCALL conform, don't you have to specify explicitely that you pass N and M by reference?


No, the REFERENCE attribute for the FR1 means that all the arguments are passed by reference by default (unless explicitly overriden).
0 Kudos
Reply