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.









Thanks for the explanation on how to compile LAPACK for windows, very useful.
9 February 2008, 5:13 pmIt would be great if you could help with creating the C interface (CBLAS) as well. Thanks.
2 April 2008, 8:48 pmHi,
2 April 2008, 9:50 pmI 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 ?
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 =//
7 October 2008, 4:04 amI would appreciate some help.
it looks like you haven’t installed all the tools.
7 October 2008, 9:35 amIf you did, make sure to add mingw/bin to your path directory.
I’m getting the same error as Elael…
7 October 2008, 5:05 pmCan you tell me wich are the tools you mention? Should i download all the over than 20 files in the mingw page?
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
7 October 2008, 6:27 pmCan.
I downloaded and extracted all the packages, yet i get the same error…
7 October 2008, 8:03 pmI 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.
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.
7 October 2008, 9:00 pmThis 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
9 October 2008, 8:29 pmFor 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?
P/Invoke Interop Assistant project in CLR Interop might be the project you are looking for.
10 October 2008, 2:37 amRegards
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
11 October 2008, 10:10 amcd SRC
g77 -c *.f -O3
cd ..
g77 -shared -o lapack.dll SRC/*.o blas.dll -O3