Simulink Real-Time Windows Target with C++ Standard Library (STL)

9 views (last 30 days)
Hello!
I have a C++ project that I want to wrap into a S-Function block that should be compatible with Simulink RTWin target (Matlab 2014b). The C++ project uses the C++11 STL (interfaces like std::vector, std::stack, std::complex, ...).
I've created a S-Function wrapper (using the "extern C" Interface) that I can compile in Simulink after providing all include and source files (Model Preferences -> Code Generation -> Custom Code). I am even able to successfully compile the model for Simulink Real-Time Target (xPC Target).
Now when I switch to the rtwin.tcl target, I am only able to compile the project in Normal mode. Compiling in External mode results in compilation errors:
< complex > file not found". Or < vector > file not found.
Obviously, the Simulink Coder does not include the STL. After comparing the resulting makefile with the one of Simulink Real-Time Target, I recognized that Simulink Coder invokes gmake (GNU Make) rather than Visual Studio 2013 (selected by mex -setup). Unfortunately, the included gmake does only include a few C-STL implementations and if I set the Target Language to C++ it includes reimplementations of a small subset of C++STL functions.
While searching the internet, I found out that a template makefile called rtwin-vc.tmf was available for compilation with Visual Studio in the past. But the file is not available in recent Matlab Versions.
Does anybody know if I can modify the makefile template to support the Visual Studio Toolchain, or how I can include and link to the C++11 STL using gmake?
In other MatlabCentral (<http://www.mathworks.com/matlabcentral/answers/97907-why-am-i-unable-generate-code-for-real-time-windows-target-3-3-r2009a-for-a-model-that-includes-th) Questions> I've found hints, that no Windows library functions are supported. But in that particular cases the question asker included "windows.h" which is not the case for my project. Or does the STL (e.g. like std::vector) include "windows.h" on Windows Systems?
I left out any compile logs in this question since I am able to compile everything for other targets and the only error message only contains information about missing STL headers. But I could add logs if necessary.
Thank you very much for your answers!

Accepted Answer

Jan Houska
Jan Houska on 3 Feb 2015
I'm afraid that what you are trying to do is not possible. The problem is not with the fact that you are not able to compile your project using the built-in compiler. The problem is that you are trying to use STL in 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 in effect requires time-deterministic behavior of the real-time code, i.e. more or less fixed execution time. On the contrary, STL 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 is the reason why most of the STL headers are not available with Real-Time Windows Target - they throw exceptions. You will not be able to run your code by the real-time kernel even if you were able to compile it by another compiler or add STL headers to the built-in compiler somehow. Not to mention the fact that the output binary format of the built-in compiler (which is Clang) and a Microsoft compiler is very different, and only the Clang format is understood and can be loaded by the kernel.
If you are not able to modify your project not to use STL, you still have an option to use Real-Time Windows Target in Normal Mode. For Normal Mode, you can compile your S-function as you normally do for Simulink. Then, you can run yuor model in a way where real-time requirements cannot be enforced, but real-time misses are reported when they happen.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!