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

Mixing Fortran and C/C++

m_santos
Beginner
561 Views

Hello everybody!

Again I have some problems when I try to mixing codes written in fortran and C/C++.

I call a Fortran subroutine from a main program in C.

The passed arguments are structure type.

When I pass each argument separately, it works.

When I pass the structure, it doesn't work.

Should I do any change or declarationin my codes (Fortan and / or C/C++)?

Thanks in advance

MSantos

0 Kudos
8 Replies
Jugoslav_Dujic
Valued Contributor II
561 Views
m.santos:

Should I do any change or declarationin my codes (Fortan and / or C/C++)?



Most probably yes smiley [:-)]. But we cannot say anything useful unless you show us some code.
0 Kudos
m_santos
Beginner
561 Views

The codes:

in C

extern __stdcall INFERE_C(struct GnStruct *,struct CvStruct *
,struct MvStruct *,struct DvStruct *
,struct AvStruct *,struct CviStructure *
,struct MviStructure *,struct AviStructure *);

INFERE_C(&ctrlPj[0].Gn,ctrlPj[0].Cv,ctrlPj[0].Mv,ctrlPj[0].Dv,ctrlPj[0].Av,Cvi,Mvi,Avi);

in Fortran

!DEC$ ATTRIBUTES EXPORT, STDCALL :: INFERE_C
!DEC$ ATTRIBUTES ALIAS:'_INFERE_C@32':: INFERE_C

Subroutine infere_c(Gn,Cv,Mv,Dv,Av,Cvi,Mvi,Avi)

I hope it is enough

Thanks

0 Kudos
Jugoslav_Dujic
Valued Contributor II
561 Views
If Gn, Cv etc. aren't declared as arrays, you need REFERENCE attribute as well for INFERE_C (STDCALL implies VALUE, which is an ugly heritage from MS Fortran PowerStation days).

By the way, spelling out the entire ALIAS is often a bad idea; it tends to hide the problem rather than solve it. Instead, use:
!DEC$ ATTRIBUTES DECORATE, ALIAS: 'INFERE_C':: INFERE_C

In the debugger, you can always check the values of &ctrlPj[0].Gn on C side and LOC(Gn) on Fortran side upon entry -- if they don't match, you have a value/reference mismatch somewhere.


0 Kudos
m_santos
Beginner
561 Views

Unhappily I can rebuild my solution with this declaration.

In fact, my problem is related to an argument passed as value.

The arguments passed as reference have the same values in Fortran and C side.

Does anybody know why I have this problem?

Thanks

0 Kudos
Jugoslav_Dujic
Valued Contributor II
561 Views
If I may ask, why would you pass a structure by value at all? It's quite unusual. I know of one (1) case in e.g. entire Windows API where it's done that way, and even there the structure size is 32 bytes.
0 Kudos
m_santos
Beginner
561 Views

These codes are written and work in Visual Fortran 5.0 and C++ 6.0.

I am justtrying to convert them to Microsoft Visual Studio 2005 and Intel Fortran.

0 Kudos
Steven_L_Intel1
Employee
561 Views

The Intel compiler does not pass derived types by value the same way CVF did. It is an issue we are looking into. I recommend avoiding doing this if you can - pass them by reference.

0 Kudos
m_santos
Beginner
561 Views

I have done it.

Really is the best way.

Thanks for all.

MSantos

0 Kudos
Reply