- 1 year ago
Calling Lapack DLL from gfortran
To call LAPACK DLL functions from a Fortran program compiled with gfortran, you need to follow these steps:
Obtain the LAPACK DLL: Download the LAPACK library for Windows in DLL format. You can obtain it from the official LAPACK website or other reliable sources.
Create a Fortran wrapper: Write a Fortran wrapper program that contains the declarations and calls to the LAPACK routines you want to use. This wrapper program will act as an interface between your Fortran code and the LAPACK DLL.
Here's an example wrapper program that demonstrates calling the LAPACK function
DGESV
for solving a system of linear equations:fortranprogram lapack_example
implicit none
external DGESV
integer, parameter :: N = 3
integer :: n, lda, ldb, info
integer, allocatable :: ipiv(:)
real(kind=8), allocatable :: a(:,:), b(:,:)
n = N
lda = N
ldb = N
allocate(a(lda,n), b(ldb,n), ipiv(n))
a = reshape([1.0, 2.0, -1.0, 4.0, 1.0, -3.0, -2.0, 3.0, 8.0], [lda, n])
b = reshape([7.0, 19.0, 16.0], [ldb, n])
call DGESV(n, 1, a, lda, ipiv, b, ldb, info)
deallocate(a, b, ipiv)
end program lapack_example
Note that the external declaration
external DGESV
tells the Fortran compiler that theDGESV
routine will be provided by an external library (LAPACK DLL).Compile and link the program: Use gfortran to compile and link your Fortran program, specifying the LAPACK DLL as an additional library to be linked. The command might look something like this:
shellgfortran -o lapack_example.exe lapack_example.f90 -llapack
Replace
lapack_example.f90
with the name of your Fortran source file, and ensure that the-llapack
option matches the actual name of the LAPACK DLL file you obtained.Run the program: Execute the compiled executable, and it will call the LAPACK functions via the DLL.
Make sure that the LAPACK DLL and any associated dependencies are in the same directory as your executable or in a directory listed in the system's PATH
environment variable.
Note: The specific steps and options may vary depending on your operating system and environment. Be sure to refer to the documentation for your specific Fortran compiler and LAPACK library for any additional requirements or considerations.