MATLAB Answers

How do I set up Microsoft Visual Studio 2017 for SLRT?

483 views (last 30 days)
How do I set up Microsoft Visual Studio 2017 for use with Simulink Real-Time?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 24 Jan 2020
Edited: MathWorks Support Team on 24 Jan 2020
Install Visual Studio 2017 with Windows 10 SDK and VC++ 2017 toolset:
1) Begin installing your edition of Visual Studio 2017 as normal or if you have already installed Visual Studio 2017, run the Visual Studio Installer and skip to step (3) in this section.
2) Once Visual Studio 2017 is installed, the buttons Update, Launch, and a hamburger menu will appear.
3) Click on the hamburger menu and choose Modify.
4) This will bring up the "Modifying" dialog box.
5) Click on the Individual components tab.
Check the box for Windows 10 SDK, VC++ 2017 toolset (v141 tools) and the following Visual Studio C++ core features.
- Windows 10 SDK (10.0.17134.0 or earlier) under SDKs, libraries, and frameworks.
- VC++ 2017 version 15.8 v14.15 under Compilers, build tools, and runtimes (latest v141 tools or VC++ 2017 15.8 v14.15 latest v141 tools if the given option is not available).
- Visual Studio C++ core features under Development Activities.
CAUTION: Do not install Windows 10 SDK (10.0.17763.0) or a later version. This is known to cause an error on the target upon loading the application (Unable to load DLL dependency: kernel32.dll.GetFileSizeEx).
Click the Modify button.
6) Visual Studio 2017 will now start adding the necessary components to its installation.
Set up MATLAB and Simulink Real-Time to Detect Visual Studio 2017 Compiler:
1) If using R2016b or R2017a, install patches that will allow MATLAB and Simulink to detect the Visual Studio 2017 compiler. All patches must be installed in the following order:
MEX-file compilation, MATLAB Compiler and MATLAB Compiler SDK:
Simulink Coder:
Simulink Real-Time:
2) Execute the following (note, this will remove all support packages and custom libraries, so they need to be reinstalled or added back to the path):
rehash toolboxcache
3) Restart MATLAB.
4) Execute
mex -setup
5) Select Microsoft Visual C++ 2017 (C)
6) Copy the attached timestwo.c file to an empty directory to make sure that you can mex. Then, execute
mex -v timestwo.c
7) If mex works without error, you can now use Simulink Real-Time with the Visual Studio 2017 compiler. If you are using Speedgoat libraries, they can now be installed. Make sure that you install the latest Speedgoat libraries.
Note:In R2016b and R2017a, do NOT execute "slrtsetCC setup" to select Visual Studio 2017 as the compiler for SLRT because this may create an unusable MEX setup. Only "mex -setup" is required in R2016b and R2017a. If "slrtsetCC setup" has been called, delete the HostEnvSettings.xml file in the directory returned by executing "xpcprefdir" in the Command Window.
Troubleshooting:If you encounter build issues with your Simulink Real-Time model, check whether you can build with increasingly complex models. For example, begin by
  1. Making the example 'vdp' model fixed-step and building it (check if any Simulink model can be built)
  2. Building the example 'xpcosc' model (check if any Simulink Real-Time model can be built)
  3. Building an example Speedgoat model (if applicable, check if any Speedgoat model can be built)
  4. Finally, building your own model (check if the build issue is model-specific).
This helps narrow down the cause of the issue.
Stephan on 29 Sep 2020
If you have multiple SDKs installed, you can easily choose an old SDK for Simulink Realtime. Simply add the desired SDK version to the argument list in the file C:\Program Files\MATLAB\R2020a\toolbox\slrt\slrt\slrt_setup_for_visual32.m:
args.mexOpts.batFileArgs = 'x86 10.0.16299.0';
Matlab must be restarted after the file has been modified and the folders slprj and *_slrt_rtw must be deleted once.

Sign in to comment.

More Answers (3)

Timothy on 11 Dec 2019
I have a question about step 5. As of 12/11/2019, "VC++ 2017 version 15.9 v14.16" is listed as "latest v141 tools" so should we choose the option indicated as "latest v141 tools" or "VC++ 2017 version 15.8 v14.15" regardless of whether it is indicated as "latest v141 tools".

Florian Löcken
Florian Löcken on 3 Nov 2017
Hello, I followed all of your instructions. Everything works fine, if I set the environment variable "'VCCMD_START_DIR','%cd%'" at the start of Matlab. At point seven: The first two examples build without any problems. If I use an example with Simulink functions from the speedgoat library, it means:
Unable to run 'rtwmakecfg' file in directory: 'C:\Program Files\MATLAB\R2017b\toolbox\rtw\targets\xpc\target\build\xpcblocks\thirdpartydrivers\sg_blocks\can' due to error: 'Can't extract compiler version number'
Caused by:
Can't extract compiler version number
I'll use the Matlab Version 2017b and this Compiler:
CompilerConfiguration with properties:
Name: 'Microsoft Visual C++ 2017 (C)'
Manufacturer: 'Microsoft'
Language: 'C'
Version: '15.0'
Location: 'C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\'
ShortName: 'MSVC150'
Priority: 'B7'
Details: [1×1 mex.CompilerConfigurationDetails]
LinkerName: 'link'
LinkerVersion: ''
MexOpt: 'C:\Users\root\AppData\Roaming\MathWorks\MATLAB\R2017b\mex_C_win64.xml'
Maybe you have an idea how to fix this problem.
  1 Comment
Sebastian on 17 Nov 2017
This error message turns out to be triggered by an issue in the Speedgoat library. While Speedgoat will fix it for a future version, there seems to be an easy way to work around the issue.
Although the Note in step 7 above doesn't mention R2017b, please close your MATLAB, try to remove the HostEnvSettings.xml file, then restart MATLAB und finally re-do the "mex -setup" to point MATLAB to your Visual Studio.
Are you now able to successfully build your model? If you still face any issues, please reach out to the Technical Support team and refer to this Answers thread.

Sign in to comment.

Phillip Siefkas
Phillip Siefkas on 16 Mar 2018
Running ver2017b on Windows 10 and VS 2017 Pro. followed the above direction (bug fixes say their not required for 2017b so I didn't run them) and 'mex -v timestwo.c' completes successfully. But I can't create a boot floppy (3.5" 1.44M) to setup my target computer. it creates BOOTSECT.RTT and then fails "Creation of Simulink Real-Time boot disk failed. Format and Try again" I've formatted the disk verified that its writable and has no bad sectors.
  1 Comment
Phillip Siefkas
Phillip Siefkas on 19 Mar 2018
AS it turns out the new RT kernel is larger than a 1.44MB floppy drive. so unless you have a 2.88MB 3.5" floppy you'll need to move to a different boot option. I used a 4GB USB stick and it seems to be working fine.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!