Packaging a Fortran Function within a DLL

Before you can call a Fortran function from an IMLPlus program, you must first package the function within a DLL. The steps required to accomplish this task are highly dependent on the software development tools used. The example shown here assumes the use of Compaq Visual Fortran Professional Edition Version 6.5.

IMPORTANT: Since this example was written, Compaq Visual Fortran has been discontinued. The replacement product is Intel® Visual Fortran. Please read the article Migrating from Compaq Visual Fortran.

To create a DLL that exports a Fortran function

  1. Start Compaq Visual Fortran.
  2. On the File menu, click New.

    Visual Fortran will display the New dialog box.

  3. Click the Projects tab.
  4. In the list, select Fortran Dynamic-Link Library.
  5. In the Project name box, type My_Fortran_Functions
  6. Click OK.

    Visual Fortran will display the wizard for creating a Win32 Dynamic-Link Library project.

  7. Select the option An empty DLL application.
  8. Click Finish.

    Visual Fortran will display the New Project Information dialog box.

  9. Click OK.

    Visual Fortran will create the project.

  10. On the Project menu, click Add To Project, and then click New.

    Visual Fortran will display the New dialog box.

  11. Click the Files tab.
  12. In the list, select Fortran Free Format Source File.
    (The older Fixed Format style was identified as obsolescent in Fortran 95.)
  13. In the File name box, type Functions
  14. Click OK.
  15. In the Functions.f90 file, type the following source code:
    ! Create a function that computes y = c*x for vector x and scalar c
    ! Return 0 if the computation succeeds, otherwise return 1
      integer(4) function My_Fortran_Function( n, c, x, y ) result ( rc )
        !DEC$ ATTRIBUTES DLLEXPORT :: My_Fortran_Function
        !DEC$ ATTRIBUTES C :: My_Fortran_Function
    
        implicit none
        integer(4) n
        real(8)    c
        real(8)    x(n), y(n)
        integer(4) i
    
        if ( n < 1 ) then
            rc = 1
            return
        end if
    
        do i = 1, n
            y(i) = c * x(i)
        end do
        rc = 0
        return
      end function
    
  16. On the Build menu, click Rebuild All.

Upon successful compilation and linking, the file My_Fortran_Functions.dll should exist in the project's Debug directory.

In the example above, the compiler directive

!DEC$ ATTRIBUTES DLLEXPORT :: My_Fortran_Function

exports the function from the DLL and the directive

!DEC$ ATTRIBUTES C :: My_Fortran_Function

specifies that the function uses the C calling convention. If you are using development tools other than Compaq Visual Fortran, you may need to use different compiler directives. Please refer to the documentation for your Fortran package.

When developing a Fortran function for use with IMLPlus, bear in mind the following:

The DllFunction class in IMLPlus follows the C convention, so you must exercise care when calling Fortran functions. In the example above, the ATTRIBUTES C directive informs the Fortran compiler that the function will be called using the C calling convention.

The ATTRIBUTES C directive also causes the function name to be exported in lower case. Depending on your Fortran compiler and the options you specify, the compiler may export the function name in upper case, lower case, or it may "decorate" the name. To discover the exact name of a function exported from a DLL, you can use Microsoft's Dependency Walker utility program. You can download Dependency Walker from the following web site:

www.dependencywalker.com