Search code examples
rwindowsfortrangfortranlapack

Calling Lapack DLL from gfortran


I have installed R on Windows, which creates a file C:\programs\R\R-3.5.2\modules\x64\lapack.dll . The Fortran compiler used to build R is gfortran. How can I call Lapack routines from gfortran? I tried

gfortran C:\programs\R\R-3.5.2\modules\x64\lapack.dll xlin.f

where xlin.f is the driver and get the error message

C:...\ccwsB76i.o:xlin.f:(.text+0x120): undefined reference to `sgesv_'


Solution

  • You can always check using the gendef program (available as part of MinGW-w64 installations) what functions/subroutines a DLL contains. Apparently "modules\x64\lapack.dll" in the installation of R for Windows is not the library that you want. The correct one is "bin\x64\Rlapack.dll"!

    But this is not the end of the story. At least in case of installation of R 3.6.1, that library contains only double precision variants of the LAPACK routines. So, I needed to transform your program to use REAL*8 and DGESV. But then this worked:

    > set "PATH=C:\Program Files\R\R-3.6.1\bin\x64;%PATH%"
    > set "PATH=C:\msys64\mingw64\bin;%PATH%"
    > gfortran xlin.f "C:\Program Files\R\R-3.6.1\bin\x64\Rlapack.dll"
    > a.exe
       1.0000000597179521
       1.0000000618499254
       1.0000000465843075
    

    which I was able to reproduce in Linux with the default LAPACK library.