Setup mex with MSVC on Windows
Recently, while I was trying to support
metismex on Windows, the
mex setup on Windows confused me for a very long time when my MSVC
was installed through BuildTools. The direct setup in Matlab for mex,
i.e., mex -setup C
fails to locate compiler path. It turned out
that the Matlab search path only include ‘Community’, ‘Professional’,
and ‘Enterprise’ versions, while the ‘BuildTools’ version is left
out. However, ‘BuildTools’ version is much lighter in disk cost than
other versions and is more preferred if IDE is not needed.
When I ran mex -setup
, the following error message appeared,
>> mex -v -setup
Verbose mode is on.
%
% many lines are removed
%
... Looking for compiler 'Microsoft Visual C++ 2019 (C)' ...
... Looking for environment variable 'ProgramFiles(x86)' ...Yes ('C:\Program Files (x86)').
... Looking for file 'C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe' ...Yes.
... Executing command '"C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe" -version "[16.0,17.0)" -products Microsoft.VisualStudio.Product.Enterprise -property installationPath -format value' ...No.
... Looking for environment variable 'ProgramFiles(x86)' ...Yes ('C:\Program Files (x86)').
... Looking for file 'C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe' ...Yes.
... Executing command '"C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe" -version "[16.0,17.0)" -products Microsoft.VisualStudio.Product.Professional -property installationPath -format value' ...No.
... Looking for environment variable 'ProgramFiles(x86)' ...Yes ('C:\Program Files (x86)').
... Looking for file 'C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe' ...Yes.
... Executing command '"C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe" -version "[16.0,17.0)" -products Microsoft.VisualStudio.Product.Community -property installationPath -format value' ...No.
Did not find installed compiler 'Microsoft Visual C++ 2019 (C)'.
Error using mex
No supported compiler or SDK was found. For options, visit http://www.mathworks.com/support/compilers/R2016a/maci64.html.
To be more precise, my MATLAB version is R2020a and MSVC version
is 2019. Although MATLAB with versions earlier than R2019b does not
support Visual Studio 2019 version 16, adding the following xml files
to folder MATLABROOT/bin/win64/mexopts/
could resolve the issue. If
any reader succeed, please let me know.
Here, we list a few steps in the modification.
-
Change the editing permission of the following two
xml
files.MATLABROOT/bin/win64/mexopts/msvc2019.xml MATLABROOT/bin/win64/mexopts/msvcpp2019.xml
-
Edit both
xml
files in the following way.- Find the block of lines as
<and> <envVarExists name="ProgramFiles(x86)" /> <fileExists name="$$\Microsoft Visual Studio\Installer\vswhere.exe" /> <cmdReturns name=""$$\\vswhere.exe" -version "[16.0,17.0)" -products Microsoft.VisualStudio.Product.Community -property installationPath -format value" /> <cmdReturns name="set "vcroot=$$"&for /f "delims= " %a in ('type "$$\VC\Auxiliary\Build\Microsoft.VCToolsVersion.default.txt"') do @if exist "$$\VC\Tools\MSVC\%a\bin\HostX64\x64\cl.exe" call echo %vcroot%" /> </and>
and attach the following block right below it
<and> <envVarExists name="ProgramFiles(x86)" /> <fileExists name="$$\Microsoft Visual Studio\Installer\vswhere.exe" /> <cmdReturns name=""$$\\vswhere.exe" -version "[16.0,17.0)" -products Microsoft.VisualStudio.Product.BuildTools -property installationPath -format value" /> <cmdReturns name="set "vcroot=$$"&for /f "delims= " %a in ('type "$$\VC\Auxiliary\Build\Microsoft.VCToolsVersion.default.txt"') do @if exist "$$\VC\Tools\MSVC\%a\bin\HostX64\x64\cl.exe" call echo %vcroot%" /> </and>
- Find the block of lines as
<and> <envVarExists name="ProgramFiles(x86)" /> <fileExists name="$$\Microsoft Visual Studio\Installer\vswhere.exe" /> <cmdReturns name=""$$\\vswhere.exe" -version "[16.0,17.0)" -products Microsoft.VisualStudio.Product.Community -property installationPath -format value" /> </and>
and attach the following block right below it
<and> <envVarExists name="ProgramFiles(x86)" /> <fileExists name="$$\Microsoft Visual Studio\Installer\vswhere.exe" /> <cmdReturns name=""$$\\vswhere.exe" -version "[16.0,17.0)" -products Microsoft.VisualStudio.Product.BuildTools -property installationPath -format value" /> </and>
- Find the block of lines as
<and> <envVarExists name="ProgramFiles(x86)" /> <fileExists name="$$\Microsoft Visual Studio\Installer\vswhere.exe" /> <cmdReturns name=""$$\\vswhere.exe" -version "[16.0,17.0)" -products Microsoft.VisualStudio.Product.Community -property installationPath -format value" /> <cmdReturns name="set "vcroot=$$"&for /f "delims= " %a in ('type "$$\VC\Auxiliary\Build\Microsoft.VCToolsVersion.default.txt"') do @if exist "$$\VC\Tools\MSVC\%a\bin\HostX64\x64\cl.exe" call echo %vcroot%\VC\Tools\MSVC\%a" /> </and>
and attach the following block right below it
<and> <envVarExists name="ProgramFiles(x86)" /> <fileExists name="$$\Microsoft Visual Studio\Installer\vswhere.exe" /> <cmdReturns name=""$$\\vswhere.exe" -version "[16.0,17.0)" -products Microsoft.VisualStudio.Product.BuildTools -property installationPath -format value" /> <cmdReturns name="set "vcroot=$$"&for /f "delims= " %a in ('type "$$\VC\Auxiliary\Build\Microsoft.VCToolsVersion.default.txt"') do @if exist "$$\VC\Tools\MSVC\%a\bin\HostX64\x64\cl.exe" call echo %vcroot%\VC\Tools\MSVC\%a" /> </and>
- Find the block of lines as
<and> <envVarExists name="ProgramFiles(x86)" /> <fileExists name="$$\Microsoft Visual Studio\Installer\vswhere.exe" /> <cmdReturns name=""$$\\vswhere.exe" -version "[16.0,17.0)" -products Microsoft.VisualStudio.Product.Community -property installationPath -format value" /> <fileExists name="$$\VC\Auxiliary\Build\vcvarsall.bat" /> <dirExists name="$$"/> </and>
and attach the following block right below it
<and> <envVarExists name="ProgramFiles(x86)" /> <fileExists name="$$\Microsoft Visual Studio\Installer\vswhere.exe" /> <cmdReturns name=""$$\\vswhere.exe" -version "[16.0,17.0)" -products Microsoft.VisualStudio.Product.BuildTools -property installationPath -format value" /> <fileExists name="$$\VC\Auxiliary\Build\vcvarsall.bat" /> <dirExists name="$$"/> </and>
- Find the block of lines as
For convenience, I list two updated xml
files for download:
msvc2019.xml and
msvcpp2019.xml.
The above updates should be valid across different Windows systems and most MATLAB versions. If there is major difference, and you find it impossible to update accordingly, please comment below or send me an Email.
Yingzhou Li