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

Mixed-language Module programming in VS2008(FORTRAN & C)

morethanair
Beginner
801 Views
PROGRAM UseCDll
USE kernel32

IMPLICIT NONE

!DEC$
!DEC$ ATTRIBUTES ALIAS:'_idata@@3HA'::idata
integer idata
common / idata / idata

INTEGER i,ret

POINTER(p_USERFUNC,compute)
INTEGER(HANDLE)::dll_handle
INTEGER(BOOL)::free_status

INTERFACE
integer function compute () BIND(C)
USE,INTRINSIC :: ISO_C_BINDING
implicit none
END function compute
END INTERFACE

write (*,*) "Loading library..."
dll_handle = LoadLibrary ("cdll-external.dll"C)

write (*,*) "Getting routine address..."
p_USERFUNC = GetProcAddress (dll_handle,"compute"C)

print *,'before idata=',idata

ret=compute()

print *,'after idata=',idata

write(*,*) "Unloading library..."
free_status = FreeLibrary (dll_handle)

END PROGRAM UseCDll
----------------------------------------------------------------------------------

This is FORTRAN code that defines the external variable idata.
It was built well.

-----------------------------------------------------------------------------------

extern int idata;

extern "C" __declspec(dllexport) void compute()
{
idata=343;
return;
}

---------------------------------------------------------------------------

This isC DLL file that uses the external variable defined in FORTRAN.
I couldn't build this code. The error was LNK2001 that the compiler doesn't check the external variable idata in 'CDLL-external.obj'

Can someone suggest how I can use the external variable between FORTRAN and C?

Thank you
0 Kudos
4 Replies
Steven_L_Intel1
Employee
801 Views
You've complicated things by not only putting the C code in a DLL but also doing dynamic load. If you had not done dynamic load, you could DLLEXPORT the variable and then DLLIMPORT it in Fortran. Otherwise, you will still need to DLLEXPORT the variable from C, do a GetProcAddress of the variable global symbol which will actually be a pointer to the variable. I don't have time right now to build an example.

Do you really need the DLL and the dynamic load in your application?

Let me suggest an alternative which is to have routines in the DLL to get and set the variable. Alternatively, you could have a routine that returns a pointer to the variable.
0 Kudos
morethanair
Beginner
801 Views
Thank you for your reply.

I already tried to add this line '!DEC$ ATTRIBUTES DLLEXPORT::idata', but C compiler still doesn't know about 'idata'.

The total structure is like below.

VS2008 IDE ------------- Project A(FORTRAN) - Definean external variable idata 'common / idata / idata'
|
------- Project B(C DLL) - refer the external variable idata from 'Project A'

My question is that how the project B can know about the idata outside itself...

Thanks a lot.
0 Kudos
Steven_L_Intel1
Employee
801 Views
The dllexport has to be in the DLL, which is your C project.

I can tell you how to make this work with a linked DLL, but doing it with a loaded DLL is a bit tougher.
0 Kudos
morethanair
Beginner
801 Views
Thank you so much.

Can I get a simple example to use static C DLL from FORTRAN code?


0 Kudos
Reply