Generate C/C++ Code with Improved MISRA Compliance

The Motor Industry Software Reliability Association (MISRA®)[1] has established “Guidelines for the Use of the C Language in Critical Systems” (MISRA C®). When you generate code from MATLAB® code, if you have Embedded Coder®, you can configure certain code generation parameters to increase the likelihood of generating C and C++ code that comply with the MISRA C:2012 and MISRA C++:2008 guidelines.

The following describes the separate command line and app workflows.

Configure Code Generation Configuration Object Properties

  1. Create an Embedded Coder code generation configuration object.

    cfg = coder.config('lib', 'ecoder', true);
    

  2. Set the properties listed in the table to the values for improved MISRA compliance.

    PropertyValue for Improved MISRA Compliance
    CastingMode'Standards'
    CppNamespace

    Any valid namespace name for C++

    DataTypeReplacement'CoderTypedefs'
    DynamicMemoryAllocation'Off'
    EnableRuntimeRecursionfalse
    EnableSignedLeftShiftsfalse
    EnableSignedRightShiftsfalse
    GenerateDefaultInSwitchtrue
    ParenthesesLevel'Maximum'
    TargetLangStandard 'C99 (ISO)' for C, 'C++03 (ISO)' for C++

    To set these properties in one step, use coder.setupMISRAConfig.

    coder.setupMISRAConfig(cfg);

    Alternatively, set properties individually. For example:

    cfg.CastingMode = 'Standards';

    If the CppNamespace property is unset, and the TargetLang property is 'C++', then coder.setupMISRAConfig sets the CppNamespace property to a default character vector, 'Codegen'. Modify this value to a namespace name that is particular to your project.

  3. Pass the configuration object to codegen by using the -config option.

    codegen myFunction -config cfg ...

Configure MATLAB Coder App Settings

  1. On the Generate Code page, set Build type to Source Code, Static Library, Dynamic Library, or Executable.

  2. Click More Settings.

  3. Click MISRA Compliance.

    The app displays the settings that might impact MISRA compliance.

  4. To set all of these settings to the recommended values, click Set to Recommended Values.

    To modify an individual setting, click All Settings, and then search for the setting.

Additional Settings for MISRA C++ Compliance

By default, the C++ code that you generate uses a custom type definition for bool to maximize compatibility with certain precompiled libraries. This custom type definition can cause violations for MISRA C++:2008 Rule 5-0-13 and Rule 5-3-1.

To use the built-in bool type in generated C++ code, and avoid these violations, change the Hardware Board setting in the configuration object from MATLAB Host Computer. To see how to change this setting in the App, see Access Hardware Implementation Properties with a Dialog Box (MATLAB Coder). For example, for code generation and build on a 64-bit Windows® platform, change the Hardware Board to None - Select device below, and then set Device vendor to Intel and Device type to x86-64 (Windows64).

See Also

|

Related Topics

External Websites


[1] MISRA and MISRA C are registered trademarks of HORIBA MIRA Ltd., held on behalf of the MISRA Consortium.