Change Language Standard Used for Code Generation - MATLAB & Simulink (original) (raw)

A C/C++ language standard is a set of rules and guidelines that define the C/C++ syntax, libraries, and features. Adhering to these standards generates C/C++ code that works consistently across different platforms and compilers. You can generate code compatible with various language standards by specifying a target language standard. However, to enforce the specified language standard, you must manually configure compiler flags.

Generate Code Compatible with a Specified Language Standard

By default, the code generator selects a language standard based on the target language that you specify when generating code. To change the target language, use one of these approaches:

If the target language is C, the default language standard is C99 (ISO). If the target language is C++, the default language standard is C++11 (ISO). To use a language standard other than the default, use one of these approaches:

This table shows the combinations of target language and language standard that are available for code generation. Verify that your compiler supports the language standard that you specify. If you specify a language standard that your compiler does not support, compiler errors can occur.

Target Language Target Language Standard Language Standard Used by the Code Generator
C, C++ C89/C90 (ANSI) ISO®/IEC 9899:1990
C, C++ C99 (ISO) ISO/IEC 9899:1999
C++ C++03 (ISO) ISO/IEC 14882:2003
C++ C++11 (ISO) ISO/IEC 14882:2011(E)
C++ C++14 (ISO) ISO/IEC 14882:2014
C++ C++17 (ISO) ISO/IEC 14882:2017
C++ C++20 (ISO) ISO/IEC 14882:2020

If the target language is C++ and you select a C language standard, the code generator uses the C math libraries available in the selected C standard and C++03 language features.

If you generate C code and select the C99 (ISO) language standard, the code generator can use C language features in the generated code that are not available in the C89/C90 (ANSI) language standard, including:

If you generate C++ code and select a language standard newer than C++03 (ISO), the code generator can use C++ language features in the generated code that are not available in the C++03 (ISO) language standard, including:

Enforce the Specified Language Standard

When you specify a target language standard, the code generator generates code that is compatible with that standard. The code generator does not enforce the selected language standard. To enforce the language standard, you must specify the compiler flag manually in the build settings. For example, this table shows the compiler flags for GCC compiler version 8.x that enforce the language standards supported by the MATLAB Coder software.

Language Standard Compiler Flags
C89/C90 (ANSI) -ansi -pedantic -Wno-long-long
C99 (ISO) -std=c99 -pedantic
C++03 (ISO) -std=c++03 -pedantic
C++11 (ISO) -std=c++11 -pedantic
C++14 (ISO) -std=c++14 -pedantic
C++17 (ISO) -std=c++17 -pedantic
C++20 (ISO) -std=c++20 -pedantic

To add a compiler flag to the build information object, use one of these approaches:

See Also

coder.CodeConfig | coder.EmbeddedCodeConfig | coder.MexCodeConfig