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
Create an Embedded Coder code generation configuration object.
cfg = coder.config('lib', 'ecoder', true);
Set the properties listed in the table to the values for improved MISRA compliance.
Property Value for Improved MISRA Compliance CastingMode
'Standards'
CppNamespace
Any valid namespace name for C++
DataTypeReplacement
'CoderTypedefs'
DynamicMemoryAllocation
'Off'
EnableRuntimeRecursion
false
EnableSignedLeftShifts
false
EnableSignedRightShifts
false
GenerateDefaultInSwitch
true
ParenthesesLevel
'Maximum'
RuntimeChecks
false
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 theTargetLang
property is'C++'
, thencoder.setupMISRAConfig
sets theCppNamespace
property to a default character vector,'Codegen'
. Modify this value to a namespace name that is particular to your project.Pass the configuration object to
codegen
by using the-config
option.codegen myFunction -config cfg ...
Configure MATLAB Coder App Settings
On the Generate Code page, set Build type to
Source Code
,Static Library
,Dynamic Library
, orExecutable
.Click More Settings.
Click MISRA Compliance.
The app displays the settings that might impact MISRA compliance.
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. 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
coder.EmbeddedCodeConfig
| coder.setupMISRAConfig
Related Topics
- Control Signed Left Shifts in Generated Code
- Control Data Type Casts in Generated Code
- Specify Data Types Used in Generated Code
- Change the Language Standard
- Code Generation for Recursive Functions
- Configure Build Settings
External Websites
1 MISRA and MISRA C are registered trademarks of HORIBA MIRA Ltd., held on behalf of the MISRA Consortium.