MATLAB: Code Generation error for data type inside Stateflow Chart

Embedded Coder

Why do not the options specified in the Custom Code pane of the Simulation target allow my model to compile in Simulink?
I am calling a custom C function from a Stateflow chart. I am specifying the source file in the custom code section of the Simulation target. The model does not compile. I have the following C function declaration in the Source file pane.
 
real_T SquareMe(real_T xs);
However, if the same options are specified in the Custom Code pane of the Simulink Coder, it generates correct code. I am wondering why do the same options not work for Simulation target.

Best Answer

Stateflow requires having function declarations in a header file since they apply code generation in the background. A code will need its input or outputs defined during compilation. This is the reason #include becomes mandatory for Stateflow. If all usages of the variable from inside the Stateflow chart as well as its definition in Model Workspace are removed OR if there is no physical connection to and from the chart to the data types, be it custom type, simulation will work.
This header file should be included in the header file pane of the custom code section. Simulink Coder generates a single model.c file and having the function declaration in the source code section works. However, Stateflow generates one C file for the chart and some other C files all of which need the declaration of the custom function. Therefore having a header file is a best practice while declaring a custom C function. Please refer to the attached models for examples.