How to setup MATLAB-Compiler to use MSVC2017 Build Tools

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!

1 Comment

Hi dear, I am configuring Cube servo 3, Quarter. I installed window 2022, msvc2019 compiler does not support. What should I do? Please help

Sign in to comment.

 Accepted Answer

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

6 Comments

Hi,
thank you a lot for your help.
Unfortunately in the meantime I installed the Community Edition of VS2017 to get around this issue. To check your solution, I uninstalled it and installed the Buildtools again. Now the compiler magically works without applying your changes. So I'm not able to test your solution on my machine anymore.
If anyone else can confirm your solution, I'll be happy to mark this answer as accepted!
Best Regards,
Sven
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.
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
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
Hi,
I don't have "SxS\VS7" in registry in the path you noted here. Would you please advise how to add MSVC 2022 for Matlab?
Thanks

Sign in to comment.

More Answers (3)

Do you know a workflow to integrate the build tools of the current Visual Studio version (2019) into MATLAB/Simulink?

3 Comments

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!
Thanks @Bram De Cooman! Like this I was perfectly able to configure MSVC Build Tools 2019 as compiler in MATLAB 2020b and MATLAB 2021b. See msvc-2019-build-tools-for-matlab-2020b.zip or msvc-2019-build-tools-for-matlab-2021b.zip, most likely, the XMLs can also be applied to later MATLAB versions.
Thank you for sharing the xml files for 2021b - It worked on 2021b on Windows 10.

Sign in to comment.

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\

2 Comments

Or,
C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\
Can you please provide any attachment of directory . I am trying my hands on 2017b but still after updating the registery I am not able to locate MSVC after mex -setup -v command?

Sign in to comment.

I have the 2019a version of MATLAB. I also have Visual Studio Community 2019 installed. However, mex can not find the said compiler. How can I fix this?

4 Comments

VS Community should work out of the box. However, the combination of 2019 Matlab and VS might be a problem. Maybe 2019 Matlab does not recognize VS2019 in general. In that case, you could go to the folder in Stanislavs answer and copy the file for VS2017 and adjust everything accordingly.
I do have a file from my R2022b installation, but I don't know if it works for your Matlab. Please change the file extension to .xml after download, but I was not allowed to upload it with the original file extension. Please let us know if it works for you.
This is my first time attempting to use the VS C++ compiler with Matlab.
I have made the manual registery entry.
I have also added the msvcpp2019.xml file to mexopts.
Upon the suggestion of a response under a different, I also manually created the system variables VCROOT, LIB, PATH, LIBPATH, and VCINTSALLDIR.
I think I've tested all fixes I could find online. I have also ensured that the VS 2019 c++ compiler is compatible with 2019a Matlab.
Is there anything left that I could test or troubleshooting I could do?
maybe you also need the msvc2019 file. You'll find it attached here.
Thank you. I'll try this as well.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!