How can I integrate custom C/C++ code library into Simulink when it contains unsupported constructs?

I have a custom C/C++ library which I want to include into my Simulink model through the Simulation Target Configuration Parameters. I want to call functions from this library using Stateflow or C Caller blocks.
However, my code contains constructs that are not directly supported in Simulink, such as C++ classes, conflicting data type names between my code and Simulink, C++14 and later constructs, etc. This results in multiple errors thrown by parser during model compilation.
How can I setup my code to avoid these errors but still have the full functionality of my custom library?

 Accepted Answer

To prevent these errors from being thrown, the implementation of the libraries and all the non-supported data types and constructs need to be separated from Simulink, by effacing them behind an interface. This interface will create layer of separation resulting in Simulink deferring to the compiler during model compilation, which avoids the parsing errors. Two files are needed: an interface header file; and an interface implementation file.
Interface header file:
This is the header file to be included in the 'Simulation Target' section of the Configuration Parameters window. It includes no other header files. It only declares functions and data types that are used directly in Simulink, such as in the C Caller block or as Simulink signals (inputs and outputs of the custom functions).
If this header file declares C structs or enums used directly in Simulink, then an equivalent definition will have to be made in Simulink. The 'Simulink.importExternalCTypes' function should help with this. Here is the documentation page for it: https://www.mathworks.com/help/simulink/slref/simulink.importexternalctypes.html
The interface header file should be the only connection between Simulink and the rest of the code, meaning Simulink will be unaware of any type and function definitions made in other header files and will simply defer to the compiler and so not throw the parsing errors. For simplicity, It is recommended that a new file be written instead of having the existing header file modified.
 
Interface .C/.CPP implementation:
This is the .C/.CPP file that will define the functions declared in the interface header file. It should be included in the 'Source files' under 'Additional build information' in the Simulation Target' section of the Configuration Parameters window. This file should include all the necessary header files from the custom code that contain function and data type definitions. These will not be visible to Simulink, as long as they are only used in the implementation of the functions declared in the interface header file.
It is recommended that this file also be created from scratch. The functions defined here should call the other custom functions from the library. If needed, they should adjust the output data types to only use what is declared in the interface header file, or simple C data types, such as int, and arrays of those data types.
 
Code contains C++14 and later constructs:
If the code contains C++14 and later constructs, these will not only not be supported by Simulink, but they will also not be expected by the compiler, which can be set to either C++03 or C++11. To resolve this, ensure that a compatible compiler is used, such as 'Microsoft Visual C++ 2019', and set the compiler flag to the needed C++ version. If using Simulink versions of R2021b or earlier, this can be only set from the MATLAB Command Window using the following command:
set_param(myModel, 'SimCustomCompilerFlags', '-std:c++17');
Starting in R2022a, this can be set directly in the 'Simulation Target' section of the Configurations Parameter window, under 'Compiler Flags':
 

More Answers (0)

Products

Release

R2022a

Community Treasure Hunt

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

Start Hunting!