MATLAB: Do I get compiler errors in the shipped simulink.c file when doing code gen of an S function using the Simulink Coder

codegencompileerrorsimulink c++simulink coder

I have a model with a Level 2 S-Function. I am trying to generate code for that model but I am getting the following compiler errors regarding the shipped "simulink.c" file. Why is this? When I using "mex" I am able to build the S-Function without any errors.
 
C:\PROGRA~1\MATLAB\R2014a\simulink\include\simulink.c(106) : error C2059: syntax error : 'type'
C:\PROGRA~1\MATLAB\R2014a\simulink\include\simulink.c(218) : error C2100: illegal indirection
C:\PROGRA~1\MATLAB\R2014a\simulink\include\simulink.c(218) : error C2440: 'initializing' : cannot convert from 'void *' to 'DimsInfo_T'
C:\PROGRA~1\MATLAB\R2014a\simulink\include\simulink.c(243) : error C2100: illegal indirection
C:\PROGRA~1\MATLAB\R2014a\simulink\include\simulink.c(243) : error C2440: 'initializing' : cannot convert from 'void *' to 'DimsInfo_T'
C:\PROGRA~1\MATLAB\R2014a\simulink\include\simulink.c(267) : error C2100: illegal indirection
C:\PROGRA~1\MATLAB\R2014a\simulink\include\simulink.c(267) : error C2440: 'initializing' : cannot convert from 'void *' to 'DimsInfo_T'
C:\PROGRA~1\MATLAB\R2014a\simulink\include\simulink.c(287) : error C2100: illegal indirection
C:\PROGRA~1\MATLAB\R2014a\simulink\include\simulink.c(287) : error C2440: 'initializing' : cannot convert from 'void *' to 'DimsInfo_T'

Best Answer

The issue is that the S-Function contained the following code at the end of the C file:
#include "simulink.c"
Instead, you should have the following:
#ifdef MATLAB_MEX_FILE /* Is this file being compiled as a MEX-file? */
#include "simulink.c" /* MEX-file interface mechanism */
#else
#include "cg_sfun.h" /* Code generation registration function */
#endif
The reason a build with MEX works is that the simulink.c file is required for that build process. However, when generating code using Simulink Coder the simulink.c is not used but rather a different header file "cg_sfun.h".