Use Intel MKL LAPACK95 with Gfortran

1 minute read

Here’s how to build the Intel MKL LAPACK95 library for use with gfortran. You can also use Netlib LAPACK95 instead of Intel MKL LAPACK95.

This procedure assumes environment variable

echo $MKLROOT

is a non-blank value such as:

~/intel/mkl

If $MKLROOT is blank, try to find where Intel MKL is on your system (did you install Intel MKL yet?)

Build / Install: MKL LAPACK95

We install MKL (without sudo) to your project’s directory. Here we assume your project is in ~/myproject.

cd $MKLROOT/interfaces/lapack95

make libintel64 INSTALL_DIR=~/myproject/ interface=lp64 FC=gfortran

This installs to include/ and lib/ under your project directory. For repeatable builds, I like to avoid messing with system libraries. This is what Matlab and Anaconda do, that is, they put libraries under their own top-level directory.

NOTE: if you need to rebuild, delete recursively the include/ and lib/ directories under ~/myproject/, or it won’t completely rebuild.

alternative: Netlib LAPACK95

Download and compile LAPACK95. Since their Makefile is quite old, I suggest my slightly updated LAPACK95

linking to Intel MKL LAPACK95

Here are a few examples, depending on your preferred build setup.

Plain gfortran LAPACK95

Notice the extra -l in front of the name lib/intel64/libmkl_lapack95_lp64.a

gfortran mycode.f90 -o myexe -llib/intel64/libmkl_lapack95_lp64.a

GNU Make LAPACK95

Inside your Makefile, add to the end of the line where you list mycode.f90

-llib/intel64/libmkl_lapack95_lp64.a

CMake LAPACK95

An example CMakeLists.txt, using the FindLAPACK95.cmake under cmake/Modules of fortran-utils

cmake_minimum_required(VERSION 3.0)
project(MyProject Fortran)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/Modules/")

find_package(LAPACK95 REQUIRED)

add_executable(myexe myprogram.f90)
target_link_libraries(myexe PRIVATE ${LAPACK95_LIBRARIES})

Leave a Comment