Select and Configure C or C++ Compiler - MATLAB & Simulink (original) (raw)

The build process requires a supported compiler. Compiler, in this context, refers to a development environment containing a linker and make utility, and a high-level language compiler. For details on supported compiler versions, see:

When creating an executable program, the build process must be able to access a supported compiler. The build process can find a compiler to use based on your default MEX compiler.

The build process also requires the selection of a toolchain or template makefile. The toolchain or template makefile determines which compiler runs, during the make phase of the build. For more information, see Configure Toolchain (ToolchainInfo) or Template Makefile Build Process.

To determine which templates makefiles are available for your compiler and system target file, see Compare System Target File Support Across Products.

For both generated files and user-supplied files, the file extension,.c or .cpp, determines whether the build process uses a C or a C++ compiler. If the file extension is .c, the build process uses C compiler to compile the file, and the symbols use the C linkage convention. If the file extension is .cpp, the build process uses a C++ compiler to compile the file, and the symbols use the C++ linkage specification.

Language Standards Compliance

The code generator produces code that is compliant with the following standards:

Language Supported Standard Other Names
C ISO®/IEC 9899:1990 C89/C90
ISO/IEC 9899:1999 C99
C++ ISO/IEC 14882:2003 C++03
ISO/IEC 14882:2011 C++11

Code that the code generator produces from these sources is ANSI® C/C++ compliant:

Also, the code generator can incorporate code from:

Note

Coding standards for these two sources are beyond the control of the code generator. These standards can be a source for compliance problems, such as code that uses C99 features not supported in the ANSI C, C89/C90 subset.

Programming Language Considerations

The code generator produces C and C++ code. Consider the following as you choose a programming language:

For C++ code generation examples with Stateflow®, see the sfcndemo_cppcount model orsf_cpp model.

C++ Language Support Limitations

If you set the Language configuration parameter toC++, but do not specify a C++ compiler through theToolchain configuration parameter, code generation produces an error.

The code generator does not produce C++ code for these products:

Simscape™ Driveline™
Simscape Multibody™ First Generation (Simscape Multibody Second Generation is supported)
Simscape Electrical™ Power Systems
Simulink Real-Time™

For ERT and ERT-based system target files with set to Nonreusable function, the following fields currently do not support the .cpp extension.

If you specify a file name with a .c extension or with no extension and specify C++ for the code generation language, the code generator produces a .cpp file.

Code Generator Assumes Wrap on Signed Integer Overflows

The code generator reduces memory usage and enhances generated code execution by assuming signed integer C operations wrap on overflow. A signed integer overflow occurs when the result of an arithmetic operation is outside the range of values that the output data type can represent. The C programming language does not define the results of such operations. Some C compilers aggressively optimize signed operations for in-range values at the expense of overflow conditions. Other compilers preserve the full wrap-on-overflow behavior. For example, the gcc and MinGW compilers provide an option to wrap on overflow reliably for signed integer overflows. The generated program image for a model can produce results that differ from model simulation results because the handling of overflows varies, depending on your compiler.

When you generate code, if you use a supported compiler with the default options configured by the code generator, the compiler preserves the full wrap-on-overflow behavior. If you change the compiler options or compile the code in another development environment, it is possible that the compiler does not preserve the full wrap-on-overflow behavior. In this case, the executable program can produce unpredictable results.

If this issue is a concern for your application, consider one or more of the following actions:

Choose and Configure Compiler

To view the installed compilers and select the default compiler, in the Command Window, type:

On a Windows computer, you can install supported compilers and select a default compiler.

On a UNIX® platform, the default compiler is GNU® gcc/g++ for GNU or Xcode for Mac.

Unless the build approach configuration selects a specific compiler, the code generator uses the default compiler for the build process.

Primarily, the specified system target file determines the compiler that the code generator requires:

If the generated code exceeds the 2 Gb static memory limit, a compiler error occurs. To resolve this issue:

Include S-Function Source Code

When the code generator builds models with S-functions, source code for the S-functions can be either in the current folder or in the same folder as their MEX-file. The code generator adds an include path to the generated makefiles whenever it finds a file named_`sfncname`_.h in the same folder as the S-function MEX-file. This folder must be on the MATLAB path.

Similarly, the code generator adds a rule for the folder when it finds a file_`sfncname`_.c (or.cpp) in the same folder as the S-function MEX-file is in.

See Also

Topics

External Websites