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

What is the scope of a file unit number?

OP1
New Contributor III
1,530 Views

What is the scope of a unit number when you have a main program and DLLs? Let's say, the main program connects unit 123 to an external file, then calls the DLL. Will unit 123 be known to the DLL? If yes, does this still hold with a main program written in a language other than Fortran?

Thanks,

Olivier

0 Kudos
5 Replies
Arjen_Markus
Honored Contributor II
1,530 Views
The LU numbers in a DLL are independent of the LU numbers in the main program - at least that is the case
with Intel Fortran under Windows (I know it works differently under Linux: there the shared object and the main program share the LU numbers). You can change this behaviour by linking against a non-default
version of the runtime library. I forgot how to make that work but it can be done.

If your main program is _not_ Fortran then it will not know of any files you open in the DLL or and vice
versa. (Perhaps there may be a platform-dependent solution to make the two components share that
information, but I doubt it)

Regards,

Arjen
0 Kudos
Steven_L_Intel1
Employee
1,530 Views
First of all, unit numbers are a Fortran concept that does not extend to other languages.

The issue with DLLs is how you link them and any program that links with them. In order for everything to work correctly, there must be one and only one copy of the Fortran run-time library present. That means, for a Fortran EXE linking to a Fortran DLL, both the DLL and the EXE must link to the shared (DLL) copy of the run-tilme library. This is the default for DLLs but not for EXEs. So make sure that your Fortran EXE's project property Fortran > Libraries > Use Runtime Library is set to the same DLL type as the DLL's property.

If you don't do this, then it will appear that the EXE and DLL have distinct sets of unit numbers and you may get unpredictable results with allocations and other things.
0 Kudos
Jugoslav_Dujic
Valued Contributor II
1,530 Views
Quoting arjenmarkus
The LU numbers in a DLL are independent of the LU numbers in the main program - at least that is the case with Intel Fortran under Windows

Well, no, not really (at least not for the purpose of reply to the original question :) ).

Long answer: LUs are identifiers of file descriptors, which are static in the module (.exe or .dll) which does the actual opening of the file. Thus, if you have an .exe and a .dll, each linked with static version of the run-time library, each of them will have its own copy of the descriptor, and thus could not "share" the LU number; thus, the Arjen is tentatively correct.

However, if both .exe and .dll are linked with dll version of the RTL (libifcorert.dll), there shall be only one copy of the descriptor -- the one in libifcorert.dll. Thus, whenever an .exe or .dll READs, WRITEs or CLOSEs the LUN, it will go through the code -- and the descriptor -- residing in libifcorert.

Short answer: to enable unit sharing, link both the dll and exe with DLL version of the RTL (Settings/Fortran/External Procedures in the IDE).

(The same consideration goes for ALLOCATE-ing and DEALLOCATE-ing memory in different binary modules: it will work as long as you use the dll RTL)

0 Kudos
OP1
New Contributor III
1,530 Views

I see. Thanks for these good answers!

Olivier

0 Kudos
Arjen_Markus
Honored Contributor II
1,530 Views
I was not (sufficiently or at all) aware of the ALLOCATE/DEALLOCATE aspect of DLLs.
But that does make sense. Good to know this

Regards,

Arjen
0 Kudos
Reply