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:
On the MATLAB® Coder™ tab of the toolstrip, click the Language button and select the output language.
In a code configuration object, set the
TargetLang
property.In the Code Configuration Settings dialog box, set the Language parameter.
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 specify a language standard other than the default, use the Language Standard 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 Code Configuration Settings dialog box, set the Build configuration parameter to
Specify
. If you do not specify a toolchain, the app locates one and populates the Toolchain parameter. Click Show settings to open the build configuration settings and add compiler flags to the C Compiler or C++ Compiler text box.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