MATLAB Answers

How to setup MATLAB-Compiler to use MSVC2017 Build Tools

138 views (last 30 days)
I was able to sucessfully setup a system using the MATLAB 2018a compiler with Visual Studio 2017 Professional. However on another machine I only run the Visual Stiudio 2017 Build Tools which have all the files needed to compile but only lack the IDE. It seems this compiler is not yet supported by the MATLAB compiler. Is there a way / patch that allows usage of the build tools?
Thanks in advance!

  0 Comments

Sign in to comment.

Accepted Answer

Stanislav Tereschenko
Stanislav Tereschenko on 6 Oct 2018
Edited: Stanislav Tereschenko on 8 Oct 2018
Hi!
I had the same problem to configure the C++ compiler provided by MSVC 2017 build tools with MATLAB R2018b. The solution was quite simple. If you look inside the file located here:
MATLABROOT/bin/win64/mexopts/msvcpp2017.xml
you will see, that MATLAB uses the entries in the registry to search for installed MSVC compilers. Therefore I only added a text entry to
HKLM\SOFTWARE\WOW6432Node\Microsoft\VisualStudio\SxS\VS7
with the name "15.0" and value "C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools". After adding this entry, restart MATLAB and repeat the configuration of the mex compiler.
You can use the attached file to add this entry to the registry.
Best Regards!
Stas

  4 Comments

Show 1 older comment
Sven Merk
Sven Merk on 26 Oct 2018
I validated the answer by setting up a new Virtual machine with Win10, VS2017-Build-tools and MATLAB 2018b.
Natively,
mex -setup
fails.
After applying the provided .reg file, the compiler is found. Thank you again for your solution.
Stefan Lietzau
Stefan Lietzau on 7 Nov 2018
To find out the install location of VS2017 Build Tools, you can use the tool vswhere which is included with any Visual Studio version. To find any version of Visual Studio 2017 run:
"C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe" -products * -version "[15.0, 16.0)"
To only list the installation path, add -property installationPath at the end of the command.
Best regards,
Stefan
Marcus Herbst
Marcus Herbst on 15 Jan 2020
Accordingly, adding the following entry to the registry made mex run with Visual C++ Build Tools 2015 as well:
[HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\VisualStudio\SxS\VS7]
"14.0"="C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\"
Thank you very much for your straightforward solution!
Best regards,
Marcus

Sign in to comment.

More Answers (2)

Robert
Robert on 12 Jun 2019
Edited: Robert on 12 Jun 2019
R2017b could not find the compiler. Had installed VS Community earlier, then installed VS Pro then removed Community. Needed to manualy change the registry entry referenced above to
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\

  1 Comment

Karl Erik Thoresen
Karl Erik Thoresen on 28 Dec 2019
Or,
C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\

Sign in to comment.


Simon Weßel
Simon Weßel on 23 Mar 2020
Do you know a workflow to integrate the build tools of the current Visual Studio version (2019) into MATLAB/Simulink?

  1 Comment

Bram De Cooman
Bram De Cooman on 25 Mar 2020
I was able to use the build tools of Visual Studio 2019 by changing
MATLABROOT/bin/win64/mexopts/msvcpp2019.xml
as follows:
  • Locate the part in the xml file within the 'locationFinder' tags (including subtags named VCROOT,SDKROOT,etc.)
  • Within some of these subtags you will notice 3 blocks surrounded by 'and' tags like here:
<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.Enterprise -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>
  • Each of these blocks tries to find the installation path of Visual Studio 2019 Enterprise, Professional or Community edition. Hence, we will copy such a block and replace Microsoft.VisualStudio.Product.X by Microsoft.VisualStudio.Product.BuildTools. Note that although the blocks under different subtags look similar, each block will be slightly different, so make sure to copy the correct blocks!
  • Leave the subtags without 'and' tags alone, these correspond to registry entries that should already be set up fine as is (provided the BuildTools are correctly installed)
  • Save the file (might need administrator privileges to do so) and relaunch Matlab. If you now type
mex -setup cpp
The Microsoft Visual C++ 2019 compiler will show up correctly.
I had no further issues with using the compiler after this procedure and I hope Matlab will provide native support for this in future versions!

Sign in to comment.