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

Need help, accessing module data stored in Fortran dll from C++ 6

m_singh
Beginner
472 Views
Hi,

Can experts and gurus guide me on this issue.

I am trying to share some data between the a VC++ program and a V Fortran dll. The data is declared in a module within the Fortran dll. The intent is that the main C++ program would fill in the data and call the Fortran dll, which will use that data and perform some analysis.

I used the dllexport directive for the variables in the module. But in my calling VC++ program I can not get access to the variables (unresolved external symbol).

Is this possible to do. Here is the relevent code. Can you give any pointers...

! Fortran dll

module mymodule
!DEC$ ATTRIBUTES DLLEXPORT::a
real a;
end module mymodule

subroutine FORTRANDLL

!DEC$ ATTRIBUTES DLLEXPORT::FORTRANDLL
use mymodule

end subroutine FORTRANDLL


! Main VC++ Calling program

// the moduledata.h file

extern "C" void __stdcall FORTRANDLL();
extern "C" float mymodule_mp_a,

// here is the calling function

void CMainFrame::OnToolsDosomething()
{

// *** the following code does not compile ***

mymodule_mp_a = 3.0f;

//******* Calling the fortran dll, this works
FORTRANDLL();
}

Thanks,
Mandeep
0 Kudos
4 Replies
digger
Beginner
472 Views
At first try to view how name of your variable seen in dll, use for that Dependency Walker utility.
If you want to use C name convention, name of variable must seen like '_varname'. I don't know what kind of names use your fortran project, but you may to transform name using ATTRIBUTES directive.
For example:


real module_var_a
!DEC$ ATTRIBUTES C, DLLEXPORT :: module_var_a


or

real module_var_a
!DEC$ ATTRIBUTES ALIAS:'_module_var_a', DLLEXPORT :: module_var_a


And last thing, name of variable in c code must be the same like name in fortran code (or if you using ALIAS like name exported from DLL).
0 Kudos
m_singh
Beginner
472 Views
You have a good point, I tried your idea, but still get an unresolved external.

module MYMODULE
real a;
!DEC$ ATTRIBUTES ALIAS:'_MYMODULE_mp_A', DLLEXPORT::a
end module MYMODULE

! Main VC++ Calling program

extern "C" float MYMODULE_mp_A ;

// here is the calling function

void Dosomething()
{

// *** the following code does not compile ***

MYMODULE_mp_A = 3.0f;
}

I used Depends.exe and saw that this variable is exported out as '_MYMODULE_mp_A', runing DUMPBIN on the dll, with /exports also confirms that.

I even opened the Lib file for this dll in a text editor and saw this variable in there. So, it is being exported out from the dll, but the calling program can not find it.

Any ideas will be appreciated.
Mandeep
0 Kudos
Steven_L_Intel1
Employee
472 Views
Get rid of the ALIAS in the Fortran code - you don't need it. I think you need a __pragma(dllimport) on the C declaration and to link against the export library created when the DLL was linked. Also, be sure you are using CVF 6.6B.

Steve
0 Kudos
m_singh
Beginner
472 Views
I modified the C declaration to,

extern "C" float __declspec(dllimport) MYMODULE_mp_A;

then all worked.

Thanks to digger and Steve
Mandeep
0 Kudos
Reply