Autocoder settings to get variables declared in *_mr_private.h file vs in *_mr_data.cpp.
6 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I am upgrading a simulink/embedded coder project from R2020b to R2024b. Having successfully generated code in the new version, we examined some differences in the generated code files. Some data "myVar" is being duplicated in a *_mr_data.cpp file for a model reference "myModel", that is already declared in shared location const_params.cpp.

The data in the image displayed below for reference....
2020b example
myVar is a simulink parameter in a constant block in the toplevel model myModel.
when generating code we get the relevant lines
myModel_mr.h:
// Parameters (default storage)
struct P_myModel_mr_T_ {
(other vars, myVar not included)}
myModel_mr_private.h:
(no include)
extern myVar_bstruct *myVar; // Variable: myVar
myModel_mr_data.cpp:
#include "myModel_mr.h"
#include "myModel_mr_private.h"
// Block parameters (default storage)
P_myModel_mr_T myModel_mr_P{
// Variable: anotherVar (myVar not in file)
// Referenced by:
....
{ lots of data }
slprj/ert/_sharedutils/const_params.cpp:
extern const myVar_bstruct rtCP_pooled_(mangled);
const myVar_bstruct rtCP_pooled_(mangled) = {all the data (its alot)}
slprj/ert/_sharedutils/my_shared_bus.h:
// Simulink Bus: myVar_bstruct
typedef struct { other structs that make up myVar } myVar_bstruct;
2024b example
same setup with myVar and myModel
myModel_mr.h:
// Parameters (default storage)
struct P_myModel_mr_T_ {
myVar_bstruct myVar; (and other vars)}
myModel_mr_private.h:
#include "myModel_mr_types.h"
No listing
myModel_mr_data.cpp:
#include "myModel_mr.h"
(no mr_private.h include)
// Block parameters (default storage)
P_myModel_mr_T myModel_mr_P{
// Variable: myVar
// Referenced by:
....
{ duplicate myVar data (is also in const_params.cpp) and other vars}
slprj/ert/_sharedutils/const_params.cpp:
extern const myVar_bstruct rtCP_pooled_(mangled);
const myVar_bstruct rtCP_pooled_(mangled) = {all the data (its alot)}
slprj/ert/_sharedutils/my_shared_bus.h:
// Simulink Bus: myVar_bstruct
struct myVar_bstruct { other structs that make up myVar };
end example.
As you can see above there is a little bit of a difference of how the myVar data is getting declared in the autocode. The 2024b version is causing the myModel_mr_data.cpp file to go from 300k to 3MB with the duplicated data. (this may be small for some but its important in this use case)
Some of the settings i've already tried on the configuration of the model.
set_param('myModel', 'CodeInterfacePackaging', 'Nonreusable function')
set_param('myModel', 'ParameterStorageClass', 'Model default');
Was attempting this and matlab went poof, after the Headerfile field wasn't avail.
myVar.CoderInfo.StorageClass = 'Custom';
myVar.CoderInfo.CustomAttributes.HeaderFile = 'myModel_mr_private.h';
Anyinsight would be greatly appreciated...
0 Commenti
Risposta accettata
Ryan
il 25 Lug 2025
Modificato: Ryan
il 25 Lug 2025
Hi Adam,
I recommend following the guidelines provided in the following MathWorks documentation on generating shared data definitions:
This specifies you will need to configure the "myVar" property "DataScope" to be "Exported", and set the model configuration parameter "Shared code placement" to "Shared location".
Additionally, the storage class used by "myVar" will impact its code placement. Instead of "custom", I recommend using "ExportedGlobal". You can find more information about possible storage classes and their implications in the following MathWorks documentation:
0 Commenti
Più risposte (0)
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!