- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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?
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Is this problem too simple? No one want to answer?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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 . . .
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page