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

Access Fortran common block in DLL from C

Kipling__Michael
Beginner
994 Views
I am trying to access a common block exported from a Fortran routine which resides in a DLL from a C routine. The problem seems to be with the leading underscore appended to the name. I have tried using STDCALL and C attributes and playing with how the common block is named in the C code with no success. I still receive the linker error for an unresolved external symbol.

The following sample code illustrates the problem. The C code is in the file c1.1 .

void MyCFunc()
{
int i1;
float r1;
double d1;

extern struct {
int var_i;
float var_r;
double var_d;
} CMN_BLK;

F2();

i1 = CMN_BLK.var_i;
r1 = CMN_BLK.var_r;
d1 = CMN_BLK.var_d;
}


SUBROUTINE F2
!DEC$ ATTRIBUTES DLLEXPORT :: F2
!DEC$ ATTRIBUTES DLLEXPORT :: CMN_BLK

INTEGER :: var_i
REAL :: var_r
REAL*8 :: var_d

COMMON /CMN_BLK/ var_i, var_r, var_d

var_i = 100
var_r = 200.0
var_d = 300.0d0

RETURN
END SUBROUTINE F2



c1.obj : error LNK2001: unresolved external symbol _CMN_BLK

0 Kudos
1 Solution
Steven_L_Intel1
Employee
994 Views
You need to tell the C compiler that the struct is DLLIMPORTed. You do this by adding __declspec(dllimport) after "extern".

View solution in original post

0 Kudos
4 Replies
Steven_L_Intel1
Employee
995 Views
You need to tell the C compiler that the struct is DLLIMPORTed. You do this by adding __declspec(dllimport) after "extern".
0 Kudos
Kipling__Michael
Beginner
994 Views
You need to tell the C compiler that the struct is DLLIMPORTed. You do this by adding __declspec(dllimport) after "extern".

Thanks Steve, that works fine.
0 Kudos
Andy_L_1
Beginner
994 Views

Steve Lionel (Intel) wrote:

You need to tell the C compiler that the struct is DLLIMPORTed. You do this by adding __declspec(dllimport) after "extern".

i want to access  data from the common block 

but my program are write in object pascal ,

what should i do

0 Kudos
mecej4
Honored Contributor III
994 Views

You will need to ascertain the external interfaces that your Pascal compiler supports, and examine whether it is feasible to call a Fortran DLL from object pascal, or whether it would be better to use C code as an intermediary.

0 Kudos
Reply