Change Language Standard Used for Code Generation
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:
In the MATLAB® Coder™ app, in the Generate Code step, select a Language radio button.
In a code configuration object, set the
TargetLang
property.
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:
In the MATLAB Coder app, in the project build settings, on the Custom Code tab, set the Language standard parameter.
In a code configuration object, set the
TargetLangStandard
parameter.
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:
Variable scoping
The
INFINITY
andNAN
macrosThe
isinf
andisnan
functionsBoolean literals
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:
Enumeration classes
Explicitly defaulted special member functions
Uniform initialization syntax using
{}
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:
In the MATLAB Coder app, in the project build settings, set the Build Configuration parameter to
Specify
. If you do not specify a toolchain, the app locates one and populates the Toolchain parameter. In the Project-specific toolchain options table, add the compiler flags at the end of theC Compiler
orC++ Compiler
field.At the command line, create a
coder.CodeConfig
orcoder.EmbeddedCodeConfig
object. Open the Configuration Parameters dialog box. On the Hardware tab, set the Build Configuration parameter toSpecify
. If you do not specify a toolchain, the code generator locates one and populates the Toolchain parameter. Open the build configuration settings and add the compiler flags to the C Compiler or C++ Compiler parameter. See Specify Configuration Parameters in Command-Line Workflow Interactively.At the command line, create a
coder.CodeConfig
orcoder.EmbeddedCodeConfig
object. Set theBuildConfiguration
property to'Specify'
. If you do not specify a toolchain, the app locates one and sets theToolchain
property. Modify theCustomToolchainOptions
property to include the compiler flags. See Custom Toolchain Registration.In your MATLAB code, call the
coder.updateBuildInfo
function with name-value argument"addCompileFlags"
or use another method to customize the post-code-generation build process. See Build Process Customization.
See Also
coder.CodeConfig
| coder.EmbeddedCodeConfig
| coder.MexCodeConfig