Setup mex with MSVC on Windows

June 30, 2020

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.

  1. Change the editing permission of the following two xml files.

    MATLABROOT/bin/win64/mexopts/msvc2019.xml
    MATLABROOT/bin/win64/mexopts/msvcpp2019.xml
    
  2. Edit both xml files in the following way.

    1. Find the block of lines as
      <and>
          <envVarExists name="ProgramFiles(x86)" />
          <fileExists name="$$\Microsoft Visual Studio\Installer\vswhere.exe" />
          <cmdReturns name="&quot;$$\\vswhere.exe&quot; -version &quot;[16.0,17.0)&quot; -products Microsoft.VisualStudio.Product.Community -property installationPath -format value" />
          <cmdReturns name="set &quot;vcroot=$$&quot;&amp;for /f &quot;delims= &quot; %a in ('type &quot;$$\VC\Auxiliary\Build\Microsoft.VCToolsVersion.default.txt&quot;') do @if exist &quot;$$\VC\Tools\MSVC\%a\bin\HostX64\x64\cl.exe&quot; 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="&quot;$$\\vswhere.exe&quot; -version &quot;[16.0,17.0)&quot; -products Microsoft.VisualStudio.Product.BuildTools -property installationPath -format value" />
          <cmdReturns name="set &quot;vcroot=$$&quot;&amp;for /f &quot;delims= &quot; %a in ('type &quot;$$\VC\Auxiliary\Build\Microsoft.VCToolsVersion.default.txt&quot;') do @if exist &quot;$$\VC\Tools\MSVC\%a\bin\HostX64\x64\cl.exe&quot; call echo %vcroot%" />
      </and>
      
    2. Find the block of lines as
      <and>
          <envVarExists name="ProgramFiles(x86)" />
          <fileExists name="$$\Microsoft Visual Studio\Installer\vswhere.exe" />
          <cmdReturns name="&quot;$$\\vswhere.exe&quot; -version &quot;[16.0,17.0)&quot; -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="&quot;$$\\vswhere.exe&quot; -version &quot;[16.0,17.0)&quot; -products Microsoft.VisualStudio.Product.BuildTools -property installationPath -format value" />
      </and>
      
    3. Find the block of lines as
      <and>
          <envVarExists name="ProgramFiles(x86)" />
          <fileExists name="$$\Microsoft Visual Studio\Installer\vswhere.exe" />
          <cmdReturns name="&quot;$$\\vswhere.exe&quot; -version &quot;[16.0,17.0)&quot; -products Microsoft.VisualStudio.Product.Community -property installationPath -format value" />
          <cmdReturns name="set &quot;vcroot=$$&quot;&amp;for /f &quot;delims= &quot; %a in ('type &quot;$$\VC\Auxiliary\Build\Microsoft.VCToolsVersion.default.txt&quot;') do @if exist &quot;$$\VC\Tools\MSVC\%a\bin\HostX64\x64\cl.exe&quot; 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="&quot;$$\\vswhere.exe&quot; -version &quot;[16.0,17.0)&quot; -products Microsoft.VisualStudio.Product.BuildTools -property installationPath -format value" />
          <cmdReturns name="set &quot;vcroot=$$&quot;&amp;for /f &quot;delims= &quot; %a in ('type &quot;$$\VC\Auxiliary\Build\Microsoft.VCToolsVersion.default.txt&quot;') do @if exist &quot;$$\VC\Tools\MSVC\%a\bin\HostX64\x64\cl.exe&quot; call echo %vcroot%\VC\Tools\MSVC\%a" />
      </and>
      
    4. Find the block of lines as
      <and>
          <envVarExists name="ProgramFiles(x86)" />
          <fileExists name="$$\Microsoft Visual Studio\Installer\vswhere.exe" />
          <cmdReturns name="&quot;$$\\vswhere.exe&quot; -version &quot;[16.0,17.0)&quot; -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="&quot;$$\\vswhere.exe&quot; -version &quot;[16.0,17.0)&quot; -products Microsoft.VisualStudio.Product.BuildTools -property installationPath -format value" />
          <fileExists name="$$\VC\Auxiliary\Build\vcvarsall.bat" />
          <dirExists name="$$"/>
      </and>
      

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

Discussion

I'm a postdoc at Duke University. Follow me on Github and Bitbucket. Hopefully, you will find my code useful. You are also welcome to either email me or post comments below to discuss on these posts.