Change Language Standard Used for Code Generation
A C/C++ language standard is a set of rules and guidelines that define C/C++ syntax, libraries, and features. These standards ensure that C/C++ code will work consistently across different platforms and compilers. 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) |
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 certain additional 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 the C++11 (ISO) language standard, the code generator can use certain additional 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
{}
Because C/C++ language standards are generally forward compatible, you can compile the
code generated by MATLAB
Coder using language standards that are newer than C99 and C++11. To instruct your
compiler to compile the generated code using a different language standard, update your
build information using the "addCompileFlags"
name-value argument to the
coder.updateBuildInfo
function. For example,
to instruct your compiler to use the C++20 language standard when compiling generated C++
code, add this line to your MATLAB
function:
coder.updateBuildInfo("addCompileFlags","-std:c++20");