MATLAB Examples

Compile and Debug Generated C Code with Microsoft® Visual Studio®

This example shows how to generate code from a model and produce a Visual Studio Solution. For the base example, see rtwdemo_counter.


About This Example

Building this example model generates a Visual Studio Solution. These configuration settings control this code generation:

  • Target - Select Configuration Parameters > Code Generation > System target file as grt.tlc Create Visual C/C++ Solution file for Simulink Coder.
  • Template makefile - Select Configuration Parameters > Code Generation > Template makefile as RTW.MSVCBuild.

When you build the model with this configuration, Simulink Coder generates code in a Visual Studio Solution. Add this solution to a Visual Studio C/C++ project to integrate generated code with your custom Visual Studio code.

Before You Start

  • This example builds code for Microsoft Visual Studio running on Microsoft Windows® platforms.
  • This example works with Microsoft Visual Studio, but not with Visual Studio Express.
  • Simulink Coder uses the GRT code format, which is intended for rapid prototyping.
  • Embedded Coder uses the ERT code format, which is intended for production deployment.

Example Steps

  1. Open the example model rtwdemo_counter_msvc. In the Command Window, type: open_system('rtwdemo_counter_msvc');
  2. To generated debug output in the generated Visual Studio Solution, set the Configuration Parameters > Code Generation > Make command parameter to make_rtw DEBUG_BUILD=1. (Omit this step if debug output is not required in the solution.)
  3. To generate code, double click the Generate Code Using Simulink Coder button in the model or at the command prompt, type: rtwbuild('rtwdemo_counter_msvc');
  4. After the build process is complete, you can see a Visual Studio Solution was generated and placed in the msvc folder under the rtwdemo_counter_msvc_grt_rtw folder.
  5. In Microsoft Visual Studio, open the rtwdemo_counter_msvc.sln solution file.

Use the solution to build and debug the generated code in Visual Studio.