Using Intel® MKL in MATLAB Executable (MEX) Files

Note: This document applies to Intel® MKL 2017 and MATLAB* R2015b running on Windows*.

HHow can I configure MATLAB to run the Intel MKL 2017 at start-up?
By default, MATLAB R2015b uses Intel MKL 11.1 BLAS. To upgrade to the Intel MKL 2017 BLAS, follow the instructions in the document Using Intel MKL with MATLAB.

How do I link to Intel MKL with my MATLAB executable (mex) file?To avoid conflicts with Intel MKL 11.1 BLAS used by MATLAB R2015b, please follow the instructions in the document Using Intel MKL with MATLAB when calling Intel MKL 2017 BLAS functions from the mex file. Then link the mex file to the custom DLL.

The instructions below refer to an example C-language mex file, m_dgetr.c, which calls the Intel MKL the Linear Algebra PACKage (LAPACK) routines dgetrf and dgetri functions as a way of computing invert matrix. Intel MKL is installed to the default location.

Choosing a Compiler
MATLAB provides a command which searches for supported compilers installed on your system.

>> mex -setup

The above command also allows you to select the compiler you wish to use. Click here to view a list of MATLAB supported compilers. For more information, visit the MATLAB support web site.

Static Linking

Use the following command to statically link the mex file to the Intel MKL static libraries:


>> mex -v mex_dgetr.c mkl_core.lib mkl_intel_thread.lib mkl_intel_lp64.lib libiomp5md.lib

Alternatively, mkl_rt.lib can be choose as a default dynamic link:

>> mex -v mex_dgetr.c mkl_rt.lib

If you started the Matlab from Intel® Compiler Command Line Windows (Programs>> Intel®   Software Development Tools >> Intel® Compiler [version] >> Build Environment for...), you may link the static libraries directly. Otherwise, you have to add the MKL path (Usually at C:\Program Files (x86)\IntelSWTools\compilers_and_libraries_version\windows\mkl) to the Matlab or system path, or simply type them before the *.lib files.

Refer to Intel MKL User's Guide for detailed instructions on statically/dynamically thread/sequential linking to Intel MKL.

Note: It is important to always dynamically link to the Intel MKL threading library, libiomp5md.lib, to the mex file.

Custom Library Builder

For applications which use only a few Intel MKL functions or are limited by code size, we provide a tool to create a smaller, customized dynamic library for distribution. Follow the instructions contained in the Intel MKL User's Guide, Building Custom Dynamic-link Libraries to create a new dynamic library, containing the functions you selected, with the Custom Library Builder. Use the following command line to link the mex file to your custom dynamic library:

>> mex -v mex_dgetr.c ${MKL}\tools\builder\mkl_custom.lib

MATLAB error message: "??? Error using ==> mex at 207 Unable to complete successfully."
When trying to build a mex file with Intel MKL, the MATLAB error message may be given:

??? Error using ==> mex at 207
Unable to complete successfully.

This error message occurs when MATLAB cannot find the Intel MKL libraries during the link step. Full paths to the location of the Intel MKL installation on the build system are required for the compiler to find the libraries and link to them. The default location for the latest version of Intel MKL was bundled with Intel Parallel Studio XE: C:\Program Files (x86)\IntelSWTools\compilers_and_libraries_version\windows\mkl\lib

MATLAB error message: "??? Invalid MEX-file 'filename.mexw64': The specified module could not be found."
When trying to run a mex file linked with an Intel MKL Custom Dynamic Library, the MATLAB error message is given:

??? Invalid MEX-file 'filename.mexw32': The specified module could not be found.

This error occurs when the Intel MKL runtime libraries are not found by MATLAB. To resolve the error, either copy your custom dynamic library to the same directory as the mex file or make sure the location of your custom dynamic library is listed in the system path.


The following example is an *.m file which calls the MKL dgetrf and dgetri functions to realize two ways of matrix inverse computation.

This example is based onIntel MKL 2017 and MATLAB R2015b and the compiler used in Matlab* is Visual Studio* 2015.
The following screen shows the computation time of using Matlab built-in ‘inv’ function and MKL dgetrf and dgetri functions.
For more complete information about compiler optimizations, see our Optimization Notice.


Ali R.'s picture


I am compiling with the command from jed p. I am compiling with the same command, with minor deference as to include ilp64. My command lines in matlab are as follows:

>>MY_LFLAGS = '';
>>MKLROOT = '/u/c/radhiali/intel/mkl';
>>SRC = ['mexDAFEM.c'];
>>MKL_LFLAGS = ['-Wl,--start-group ' MKLROOT '/lib/intel64/libmkl_intel_ilp64.a ' MKLROOT '/lib/intel64/libmkl_gnu_thread.a ' MKLROOT '/lib/intel64/libmkl_core.a -Wl,--end-group -lgomp -lpthread -lm -ldl'];
>> mex(['COPTIMFLAGS="' MY_OPTIM_FLAGS '"'], ['CFLAGS=" -fopenmp -m64 -I' MKLROOT '/include -fPIC"'], ['CLIBS="' MY_LFLAGS ' -fopenmp ' MKL_LFLAGS '"'], SRC);


It compiles fine but the matlab stops working, saying it has an internal error and must stop. I checked the code and compiled in my windows machine, works fine. Please advise as what should be done. Thank you and have a good day.


Ali Radhi

jed p.'s picture

OK... I got this to work.  Static linking was necessary... I think the first time I tried with static linking I had and env variable incorrectly set (perhaps MKLROOT since there are multiple MKL installs on the system).

MKL_LFLAGS = ['-Wl,--start-group ' MKLROOT '/lib/intel64/libmkl_intel_lp64.a ' MKLROOT '/lib/intel64/libmkl_core.a ' MKLROOT '/lib/intel64/libmkl_gnu_thread.a -Wl,--end-group -ldl -lpthread -lm'];

mex(['COPTIMFLAGS="' MY_OPTIM_FLAGS '"'], ['CFLAGS=" -fopenmp -m64 -I' MKLROOT '/include -fPIC"'], ['CLIBS="' MY_LFLAGS ' -fopenmp ' MKL_LFLAGS '"'], SRC);


jed p.'s picture

I am interested in this snippet of the post above:

"When linking to Intel MKL 10.3.x LAPACK and other functions (non-BLAS), we recommend linking Intel MKL 10.3.x to the mex file using either static linking or custom dynamic library linking. We expect to provide a more flexible and capable solution to resolve these compatibility issues in a future Intel MKL release."

I am trying to link to non-BLAS functions of MKL 11.3.0 from within Matlab 2014a (linux).  Neither static or dynamic linking seems to work for me.  Is there a "more flexible and capable solution" now (more than 4 years after this was apparently written)?

Thanks for any help!!!'s picture

I am using Matlab 2010b and would like to like in the composerxe-2011.4.191 mkl libraries using Linux. I get Windows information but not Linux. Can the mkl libriaries but used with linux?

ak.eas's picture

Matlab 2009b is shipped with libiomp5md.dll, so mex files should be linked with libiomp5md.lib

jasperjones's picture

A similar guide for Linux users would be greatly appreciated.

AmandaS (Intel)'s picture

For questions about designing software with MATLAB, please visit go to their product web site at

anonymous's picture

how to design an installable software using matlab

anonymous's picture

how to design an installable software using matlab

Add a Comment

Have a technical question? Visit our forums. Have site or software product issues? Contact support.