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
Visual Fortran will display the New dialog box.
My_Fortran_Functions
Visual Fortran will display the wizard for creating a Win32 Dynamic-Link Library project.
Visual Fortran will display the New Project Information dialog box.
Visual Fortran will create the project.
Visual Fortran will display the New dialog box.
Functions
! 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
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: