Compile LAPACK and BLAS as DLL on Windows

BLAS (Basic Linear Algebra Subprograms) is a library that provides standard routines for basic vector and matrix operations.

LAPACK is a library that provides functions for solving systems of linear equations, matrix factorizations, solving eigenvalue and singular value problems. LAPACK library have dependency to BLAS library because of the LAPACK functions using BLAS functions to work.

Both of those libraries are written in Fortran77 and no binaries provided for windows. The sources are downloadable from netlib site. So you can compile and use their libraries. However makefile and make solutions for compiling under windows are cumbersome.

So I recommend compiling directly from the sources using a FORTRAN compiler. There is a free and open source FORTRAN compiler for Windows which is the port of gnu FORTRAN compiler for Windows. You could also use Cygwin and tools in cygwin but your dynamic library loader will have dependency to cygwin dlls. MinGW is a collection of tools that allows you to produce native Windows programs. G77, make, gcc are the common tools provided by Mingw.

Here are the steps to go to compilation

  • Download most current version of LAPACK from Netlib and extract the sources
  • Download almost all of the Mingw tools and extract the tools
  • Copy dlamch.f and slamch.f from INSTALL directory SRC directory
  • Set path to have mingw binaries
    • set PATH=c:\mingw\bin\;%PATH%
  • Go to root directory of the extracted folder and compile using g77
  • First compile BLAS. –shared option is needed in order to functions to be exposed from the dll. -O generates optimised code. -o filename is the output file
    • g77 –shared -o blas.dll BLAS\SRC\*.f –O
  • Compile LAPACK with BLAS dependency
    • g77 –shared -o lapack.dll src\*.f blas.dll -O

Here is the output:

c:\\lapack-3.1.1\\copy INSTALL\\dlamch.f SRC\\dlamch.f
       1 file(s) copied.
c:\\lapack-3.1.1\\copy INSTALL\\slamch.f SRC\\slamch.f
       1 file(s) copied.
c:\\lapack-3.1.1\\set PATH=c:\\tools\\mingw\\bin\\;%PATH%

c:\\lapack-3.1.1\\g77 --shared -o blas.dll BLAS\\SRC\\*.f -O

c:\\lapack-3.1.1\\g77 --shared -o lapack.dll src\\*.f blas.dll -O

Hope this helps.

24 Comments

  1. H M Sauro says:

    Thanks for the explanation on how to compile LAPACK for windows, very useful.

  2. G.Ratzlaff says:

    It would be great if you could help with creating the C interface (CBLAS) as well. Thanks.

  3. Can says:

    Hi,
    I haven’t tried but since it is coming from the same source(NETLIB), changing the fortran compiler to gcc (providing corresponding options) should work.
    Doesn’t it ?

  4. Elael says:

    I’ve got the following error:

    “g77: installation problem, cannot exec `C:\MinGW\bin\\..\libexec\gcc\mingw32\3.4
    .5\collect2.exe’: Invalid argument”

    I don’t know what is causing this error =//
    I would appreciate some help.

  5. Can says:

    it looks like you haven’t installed all the tools.
    If you did, make sure to add mingw/bin to your path directory.

  6. Eve says:

    I’m getting the same error as Elael…
    Can you tell me wich are the tools you mention? Should i download all the over than 20 files in the mingw page?

  7. Can says:

    Here are the tools that I was mentioning that I have.

    binutils-2.17.50-20060824-1.tar.gz
    gcc-ada-3.4.5-20060117-1.tar.gz
    gcc-core-3.4.5-20060117-1.tar.gz
    gcc-g++-3.4.5-20060117-1.tar.gz
    gcc-g77-3.4.5-20060117-1.tar.gz
    gcc-java-3.4.5-20060117-1.tar.gz
    gcc-objc-3.4.5-20060117-1.tar.gz
    w32api-3.11.tar.gz

    Hope this helps
    Can.

  8. Eve says:

    I downloaded and extracted all the packages, yet i get the same error…
    I tryed out almost everything… it’s 6 hours nonstop work on that.
    My Collect2.exe file is dated 24/04/2008 and is 87.552 byte.
    My g77.exe file is dated 24/04/2008 and is 93.184 byte.

  9. Can says:

    Sorry to hear that,

    Did you update the path as well?
    set PATH=c:\mingw\bin\;%PATH%

    If so I will try to reproduce it myself. It has been a while since I haven’t compiled lapack.
    I will write back, if anything more is needed.

    Regards.

  10. HG Bui says:

    This way is very useful but the problem is how to calling it (blas.dll, lapack.dll) in c# environment. I’ve try with DllImport but not successful
    For example:
    [DllImport("blas.dll")]
    private static extern float sdot(ref int N,ref float[] X,ref int incX,
    ref float[] Y,ref int incY);
    Do you have any idea about that?

  11. Can says:

    P/Invoke Interop Assistant project in CLR Interop might be the project you are looking for.
    Regards

  12. Matt says:

    perhaps collect2.exe cannot deal with the number of files in lapack/src

    This compiles fine (not tested though)

    g77 -shared -o blas.dll BLAS/SRC/*.f -O3
    cd SRC
    g77 -c *.f -O3
    cd ..
    g77 -shared -o lapack.dll SRC/*.o blas.dll -O3

  13. Willard says:

    If anyone is still interested, I was able to compile it on my laptop after encountering the ‘collect2.exe’: Invalid argument”’ error. It seems that the command line is too long due to the temporary directory “%USERPROFILE%\Local Settings\Temp” which expanded gives “C:\Document and Settings\\Local Settings\Temp”.

    So change the TMP enviroment variable to something shorter (I created a directory called C:\Temp and pointed TMP to it).

    Then you can enter the following:
    ‘g77 –shared -o lapack.dll src\*.f blas.dll -O’

    Unfortunately, I ran into some linker errors – to resolve these I copied the dlamch.f and slamch.f files from the INSTALL subdirectory to the SRC subdirectory and rerun the g77 command above.

    I’ve tried the gesv(…) command in a program with the boost numeric bindings and everything seems to be working…

  14. tbeu says:

    LAPCK 3.1.1 compiles withour a single warning, however there are plenty of warnings with LAPACK 3.2.1 In LAPACK 3.2.1 blas.dll is built, but not lapack.dll. My compiler output is here.

  15. Singh says:

    Thanks for step by step procedure. I have the LAPACK and BLAS DLLs now but I don’t know how to use them in my Fortran code. I don’t know how to link them with my program where I want to calculate the eigenvalues. Can you please guide me?

    I want to calculate eigenvalues of a matrix. How do I call LAPACK functions (subroutines) to solve for eigenvalues using these DLLs?

    Thanks.

  16. Singh says:

    Oh, I forgot, I am using GNU compilers (g77 or g95 or gfortran)

  17. Martin Schroeder says:

    @Singh:

    Here is a example for a wrapper in C++:
    http://monsoon.harvard.edu/people/shaw/programs/dsyev.h

  18. j says:

    I had the same issue as tbeu. lapack.dll was not created.

  19. Eric says:

    hi,
    tbeu, j, I have the exact same problem : it might come from the fact that lapack 3.2.1 doesn’t compile any more with g77… but I don’t know what is the correct command line !
    If anyone knows, that would be great !

    Another question, is there a similar howto for making a single dll but with gotoblas and lapack ? that would be even better !!!

    Thank you

  20. Yin says:

    @Eric,

    MinGw seems to only have a fortran77 compiler.

    The latest source needs a F90 compiler.

    Best,
    Yin

  21. metator says:

    @HG Bui,

    One of the possible problems regarding the dll import might be related to name mangling. If you compile the dlls using the command provided, the compiler will add an underscore to the end of function names (if you have Windows SDK installed or Visual Studio you can check this by executing this command: “dumpbin /exports blas.dll”. It’ll print a list of the function names that you can use in C#). To avoid that behavior, compile using the following command:

    $ g77 -shared -fno-underscoring -fno-second-underscore -o blas.dll BLAS/SRC/*.f -O

    The same goes for lapack.dll.

    Regards,

    Mach

  22. metator says:

    Just for the record,

    I successfully compiled (with some warnings related to ASSIGN) BLAS version 3.2.1 using gfortran compiler (you can get it at: http://gcc.gnu.org/wiki/GFortran), though i did not test it thoroughly. Compiling LAPACK throws this error: “CreateProcess: No such file or directory”. I don’t know the reason for this and would appreciate if someone could post some advise to solve this.

    It is also possible to compile a 64 bit version of BLAS using MinGW-w64 (http://sourceforge.net/projects/mingw-w64/) which already includes gfortran. Again, trying to compile LAPACK will throw the same error mention above.

    Regards,

    Mach

  23. Nick says:

    The “CreteProcess: No such file or directory” sounds like trying to run a 64-bit exe on a 32-bit windows box.

  24. Jhcho says:

    @metator

    I have been troubled with the same problem with gfortran,
    but finally succeeded to compile LAPACK using gmake.

    1. Copy INSTALL/make.inc.gfortran file and make make.inc file.
    2. Goto SRC folder and gmake (gmake compiles INSTALL/dlamch.f, slamch.f automatically.)
    3. gfortran -shared -o lapack64.dll *.o ../INSTALL/dlamch.o ../INSTALL/slamch.o ../BLAS/SRC/blas64.dll -O

    Additionally, using dllwrap and lib, def file and lib file(for c++) could be made.
    (See http://www.stanford.edu/~vkl/code/libs.html)

Leave a Reply