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

Passing values from C to Fortran

Roop
Beginner
503 Views

Hello again Fortran users,

I've been working with mixed language programming for a short while now, and am using Visual Studio 2005 with the Intel Fortran compiler. My goal is to assign values to variables in C++, pass those values to a fortran subroutine, execute some equations, and pass the new variable values back to C. I've started with a simple program, just to test if I am passing values correctly. My c++ program is calling to a fortran static library.

In CPROG.cpp:

[cpp]#include "stdafx.h"
#include "CPROG.h"

#include 
#include 

using namespace std;

extern "C" 
	{
		void __cdecl FORTRANSUBR();
	}

int main()
{
	VCOM.XX.VMA = 0;
	cout << "VMA = ";
	cin >> VCOM.XX.VMA;	// I want this value passed into Fortran
	cout << "\n" << VCOM.XX.VMA << " is the value for VMA in (C).\n";		
	FORTRANSUBR();

	return 0;
}[/cpp]

In CPROG.h:

[cpp]extern "C"
{
	extern struct vcomblock_type
	{
		struct
		{
			float   VMA;		// Edited down for simplicity; this is the 481 location in Fortran
		} XX;

	} VCOM;

}[/cpp]

In FORTRANPROG.F90:

[fxfortran]	Subroutine FORTRANSUBR
!   !---------------

	Include 'FORTRANINC.inc'
!	!...

    	print *, VMA, " is the value for VMA in (Fort)."

	RETURN
	END[/fxfortran]

In FORTRANINC.inc:

[fxfortran]!.............local...............

	Common /VCOM/ xx
	EQUIVALENCE (XX( 481),VMA  ) 	! This being the 481 location in this fortran file; Edited down for simplicity.[/fxfortran]

My output looks like the following:

VMA = 2

2 is the value for VMA in (C).

0.0000000E+00 is the value for VMA in (Fort).

I have the Linker > Additional Library Directories/Dependencies set correctly to the fortran .lib file. Is there a reason why my value isn't passing correctly? Thanks in advance for your thoughts.

0 Kudos
1 Reply
Roop
Beginner
503 Views

I've resolved this problem;

Earlier in my program, I declared a dependency to VMA as a 'double' instead of 'float'. Fortran would not read the value as it was expecting a REAL (4).

0 Kudos
Reply