Troubleshoot C++ Standard Template Library (STL) Compilation Errors for Real-Time Application
To include a C++ project, I wrap the source into an S-Function block that is compatible
with the Simulink®
Desktop Real-Time™ code generation target, sldrt.tlc
. The C++ project uses the
C++11 Standard Template Library (STL) interfaces, such as std::vector
,
std::stack
, and std::complex
. I can successfully
compile the project in Connected IO mode. But, when I compile in
Run in Kernel mode, I get compilation errors such as:
<complex> file not found <vector> file not found
What This Issue Means
The compiler generates these errors because the C++ standard template library is not compatible with real-time code.
In real-time code, there is a requirement to always finish a single time step on time, before the next one is due to start. This requirement produces time-deterministic behavior for the real-time code and produces more or less fixed execution time.
By comparison, the C++ standard template library frequently uses features that are not time-deterministic. The most notable examples are dynamic memory allocation and exceptions. While dynamic memory allocation is (with some limitations) supported by the real-time kernel in a real-time deterministic way, exceptions are not. So, it is not possible to use code that can throw exceptions.
This issue causes most of the STL headers to be unavailable with the Simulink Desktop Real-Time code generation target. These interfaces throw exceptions. You cannot run code that uses the STL in the real-time kernel.
Try This Workaround
To eliminate the compiler errors, modify your project not to use the C++ standard template library.
If you are not able to modify your project not to use the C++ standard template library, you can use the Simulink Desktop Real-Time code generation target in Connected IO mode.
For Connected IO mode, compile the S-function as you would for Simulink. Then, run the model. The real-time requirements cannot be enforced, but the real-time misses are reported as they occur.