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

about common in the mixed programming with intel fortran and vs 2008

ollehyas
Beginner
280 Views
Hi, experts

I use Intel Fortran 11 and vs 2008 to implement mixed language programming. The main

program is wrirten with c++ and the subprogram with intel fortran. In the subprogram, I declare

the common block to be DLLEXPORT, but when in the subprogram, the common datas are changed, the

data in the main part don't change.

the main programis

struct AB

{

float a,b;

};

extern "C" {

void FF(void);

AB XX;

}

int main(int argc, _TCHAR* argv[])

{

FF();

printf("%f %f\\n",XX.a,XX.b);

return 0;

}

the subprogram is

subroutine FF

!DEC$ ATTRIBUTES DLLEXPORT::FF

!DEC$ ATTRIBUTES DLLEXPORT,ALIAS:"XX"::/XX/

common /XX/ A,B

real A,B

A=1

B=2

write(*,*)"ok"

end subroutine FF



I can successfully call the Fortran subroutine FF and output the "OK",
but variables in struct XX is 0.0000. How should I fix the problem?

0 Kudos
5 Replies
ollehyas
Beginner
280 Views

Is this problem too simple? No one want to answer?

0 Kudos
Steven_L_Intel1
Employee
280 Views
I don't see a "dllimport" in the C++ code.
0 Kudos
ollehyas
Beginner
280 Views
I don't see a "dllimport" in the C++ code.

But in my solution, it works without inclusion of the key word of "dllimport"

I build two projects in the same solution, one is main project and the other is for Fortran dll.
I modify the output directory path in the two project setting so that exe , dll and lib files can been
in the same directory lest copy these files.

as mentioned in my topic, in C++ main program, the call for fortran subroutine is successful.

Accoridng to your suggestion, I add __declspec(dllimport) in front of declaration for external function.
The same results are got

0 Kudos
anthonyrichards
New Contributor III
280 Views
Taking your code as a starting point, the following works for me:

1) The C main program:

// cmaintest.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
struct AB
{
float a,b;
};
extern "C" { __declspec(dllimport) void FF(void);}
extern "C" {__declspec(dllimport) AB XX;}
int main(int argc, _TCHAR* argv[])
{
XX.a=10;
XX.b=20;
printf("%f %f\n",XX.a,XX.b);

FF();

printf("%f %f\n",XX.a,XX.b);
return 0;
}

2) The Fortran DLL code:

! Dll1.f90
!
! FUNCTIONS/SUBROUTINES exported from Dll1.dll:
! Dll1 - subroutine
!
subroutine FF
! Expose subroutine FF and common block /XX/ to users of this DLL
!DEC$ ATTRIBUTES DLLEXPORT::FF
!DEC$ ATTRIBUTES DLLEXPORT:: /XX/
! Variables
common /XX/ A,B
real A,B
! Body of Dll1
A=1
B=2
write(*,*)"ok"
end subroutine FF

This solution builds and when run produces the following console output:


10.000000 20.000000
ok
1.000000 2.000000
Press any key to continue . . .
0 Kudos
ollehyas
Beginner
280 Views
Dear anthonyrichards,

you are right. I also find the right way under expert's suggestion.

The reason that confuse me is VS 2008 can't directly observe the shared COMMON.

I put the shared COMMON block XX in the debug windows,but shows error CXX0017.
the error misleads me to think the COMMON blcok XX is unaccessible. The misunderstanding
make me belieive there is a error in my progam.

0 Kudos
Reply