Building Dynamic-Link Libraries

When you first create a DLL, create a new project, and select Fortran Dynamic-Link Library as the project type.

To debug a DLL, you must use a main program that calls the library routines (or references the data). From the Project Property Pages dialog box, choose the Debugging category. A dialog box is available for you to specify the executable for a debug session.

To build the DLL from the Microsoft integrated development environment (IDE):

  1. A Fortran DLL project is created like any other project, but you must specify Dynamic-Link Library as the project type.

  2. Add files to your Fortran DLL project. Include the DLL Fortran source that exports procedures or data as a file in your project.

  3. If your DLL exports data, consistently specify the project settings options in the Fortran Data compiler option category for both the DLL and any image that references the DLL's exported data. In the Fortran Data compiler option category, specify the appropriate values for Common Element Alignment (common block data) and Structure Member Alignment (structures in a module). This sets the /align option, which specifies whether padding is needed to ensure that exported data items are naturally aligned.

    For example, in the case of a common block containing four-byte variables, you might:

    • Open the appropriate solution and select the project in the Solution View.

    • From the Project>Properties, select the Fortran category.

    • Select Data.

    • In the Common Element Alignment box, select 4 Bytes.

  4. If you need to specify linker options, use the Linker category of the Project Property Pages dialog box.

  5. Build your Fortran DLL project.

    The IDE automatically selects the correct linker instructions for loading the proper run-time library routines (located in a DLL themselves). Your DLL is created as a multithread-enabled library. An import library (.LIB) is created for use when you link images that reference the DLL.

To build the DLL from the command line:

  1. If you build a DLL from the command line or use a makefile, you must specify the /dll option. For example, if the Fortran DLL source code is in the file f90arr.f90, use the following command line:

    ifort /dll f90arr.f90
    

    This command creates:

    • A DLL named f90arr.dll.

    • An import library, f90arr.lib, that you must link with applications that call your DLL.

    If you also specify /exe:file or /link /out:file, the file name you specify is used for a .DLL rather than an .EXE file (the default file extension becomes projectname.DLL instead of projectname.EXE)

    The /dll option selects, as the default, the DLL run-time libraries to support multithreaded operation.

  2. If your DLL will export data, the procedures must be compiled and linked consistently. Consistently use the same /align option for the DLL export procedure and the application that references (imports) it. The goal is to specify padding to ensure that exported data items are naturally aligned, including common block data items and structure element alignment (structures in a module).

  3. If you need to specify linker options, place them after the /link option on the ifort command line.

  4. Build the application. For example, if your DLL exports a common block containing four-byte variables, you might use the following command line (specify the /dll option):

ifort /align:commons /dll dllfile.for

The /dll option automatically selects the correct linker instructions for loading the proper run-time library routines (located in a DLL themselves). Your DLL is created as a multithread-enabled library.

The DLL Build Output

When a DLL is built, two library files are typically created:

  • An import library (.LIB), which the linker uses to associate a main program with the DLL.

  • The .DLL file containing the library's executable code.

Both files have the same basename as the library project by default.

For a build from the command line, your library routines are contained in the file projectname.DLL located in the default directory for your project, unless you specified another name and location. Your import library file is projectname.LIB, located in the default directory for your project.

For a build from the Microsoft* Visual Studio* integrated development environment, both the library routines and the import library file are located in the output directory of the project configuration.

Note

If the DLL contains no exported routines or data, the import library is not created.

Checking the DLL Symbol Export Table

To make sure that everything that you want to be visible shows up in the export table, look at the export information of an existing DLL file by using QuickView in the Windows Explorer File menu or the following DUMPBIN command:

  DUMPBIN /exports file.dll
For more complete information about compiler optimizations, see our Optimization Notice.