LF Fortran 95

Creating Import Libraries

When the /def option is specified, LIB is used to generate an import library. It is usually not necessary to use LIB to create an import library, because the import library is automatically generated by LINK whenever a DLL is created. If the user is creating a DLL with a 3rd party language system and an import library is not created, or if the user is provided with a DLL by a 3rd party without an import library, one can be generated using LIB /def. For more information on creating import libraries for mixed language applications, Building Import Libraries.

Two items are needed to generate an import library - a set of definitions and an object file containing the exported procedures.

Definitions may be in the form of a definition file or as arguments to the /EXPORT option. A definition file contains exported symbols as they appear in the DLL. These symbols can be listed using DUMPBIN /EXPORTS. Alternatively, a definition file can be generated from a DLL using the MAKEDEF utility. Note that the definition file that is used when creating the import library should only contain procedure names that appear in the object file, otherwise unresolved references will occur when the LIB command is executed.

If the object file that was used to create the DLL is available, an import library can easily be created using the object file and a definition file.

Example:

lib /def:mydll.def dllobj.obj /out:mydll.lib

In this example the file mydll.def contains an EXPORTS header, under which export symbols are listed as they appear when displayed with the DUMPBIN utility. The file dllobj.obj is the object file that was linked to make the DLL.

If no object file is available, a Fortran object file can be created from a Fortran `stub'. All that is required is that the user know the calling sequence for the DLL procedure. A stub procedure consists of a SUBROUTINE or FUNCTION statement, an argument list, declarations for any dummy arguments, a DLL_EXPORT statement, and an END statement. Note that the stub procedure name appearing in the DLL_EXPORT statement is case-sensitive, and should have the same case as the procedure exported from the DLL. The stub procedure is compiled into an object file using the -c and an appropriate -ml option. This object file can then be used by LIB to create the import library. When compiling the LF95 program that will call the DLL, make sure that the same -ml option is used as for the stub procedure. Note that -ml lf95 is not a valid option when making an import library.

Example stub procedure (called dllsub1.f90):

subroutine dllsub1(a,i,l) dllexport :: dllsub1 real :: a integer :: i logical :: l end subroutine

Example definition file (called dllsub1.def):

EXPORTS dllsub1

Example compile command:

lf95 -c -ml msvc dllsub1.f90

Example LIB command:

lib /def:dllsub1.def dllsub1.obj /out:mydll.lib

The above examples show how to create an import library from a Fortran stub for a DLL called mydll.dll, which contains a procedure called dllsub1 having three arguments. When compiling the LF95 main program which calls mydll.dll, the -ml msvc option must be used.

Note that depending on which target is specified when using the -ml option, LIB may generate a warning about multiply defined symbols. This warning can generally be disregarded.

Further examples of creating import libraries using /def and stub procedures exist in directories under the EXAMPLES\MIX_LANG directory.

See Also

LIB Reference