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

Incorrect value when using shared libraries, COMMON, EQUIVALENCE and C

Michael_B_10
Beginner
421 Views

This example builds off the problem reported in https://software.intel.com/en-us/forums/topic/591342  (This problem is still there for this example)

The problem might also be related to the one reported at https://software.intel.com/en-us/forums/topic/543831 as it does basically the same thing except on windows.

Basically the example uses COMMON, EQUIVALENCE and calls a C function to assign a value for the variable using these intrinsics. I again build shared and static libraries (and shared and static modules). The output of the build and of the programs is in the file  libex_withC_output.txt. One build fails as expected (see above). The value of the fortran variable (predef1) is correct when building a static library and using static modules. But the value of predef1 is not correct when building with a shared library.

Output of the programs:

C:\autotest\libex\build>bin\Release\main-shared.exe
 x=           1
 *** In C  x= 1
 predef1           0           0  <===== WRONG!
 x=   3.000000
 predef2           0           0
 x,y=   3.000000       4.000000
 x,y=          10           2

C:\autotest\libex\build>bin\Release\mainS-shared.exe
 x=           1
 *** In C  x= 1
 predef1           0           0 <===== WRONG!
 x=   3.000000
 predef2           0           0
 x,y=   3.000000       4.000000
 x,y=          10           2

C:\autotest\libex\build>bin\Release\mainS-static.exe
 x=           1
 *** In C  x= 1
 predef1          10          10 <===== RIGHT!
 x=   3.000000
 predef2          10          10
 x,y=   3.000000       4.000000
 x,y=          10           2

I've attached the entire build for this simple example which displays the issue, the actual source with the cmake build is in libex.

Again, any help would be appreciated.

Scot

 

0 Kudos
2 Replies
mecej4
Honored Contributor III
421 Views

Look in the compiler installation directory for DLL.zip, the path will be something similar to Composer XE 2015\Samples\en_US\Fortran\DLL.zip.

I have not looked at your files, but a common problem is the omission of specifying (to the linker) that the data that is intended to be shared is to placed into a writeable shared section, with the result that changes in the consumer EXE and the DLL are not synchronized.

If at all possible, it is best to exchange all data by passing function/subroutine arguments, since the procedure for building is more natural and simpler in that case.

0 Kudos
IanH
Honored Contributor II
421 Views

Note that when the Fortran code is compiled as a DLL, the common block is not DLLEXPORT'ed.  Did you intend the preprocessor symbol BUILD_HDF5_DLL to also be defined?  If I fix that, things seem to work ok.

For reference the three lines that the morass of build noise collapsed down to for the C and Fortran libraries both being in DLLs was simply:

cl /LD /D BUILD_C_DLL D.c /Fec-library.dll
ifort /dll /DBUILD_DLL D_gen.F90 D.F90 H.F90 c-library.lib /Fef-library.dll
ifort main.F90 f-library.lib

Doing that simplification yourself would have made it much easier for others to help you!

Depending on configuration I also see warnings about a mismatch between function prototype and definition in the C code.  There's also a stray ISO_C_BINDING reference in the specification part of module D that achieves nothing, bar namespace pollution in anything using that module.

0 Kudos
Reply