MATLAB: Do I get an “Options file is invalid” error when I try to build an S-Function in R2016b

simulink coder

I am trying to use the S-function target to generate an S-function from my subsystem. However, I encounter the following build error:
### Compiling foo_sf.c
C:\PROGRA~1\MATLAB\R2016b\bin\mex -c -v -win64 OPTIMFLAGS="/Od /Oy-" -f
"C:\Users\johndoe\AppData\Roaming\MathWorks\MATLAB\R2016b\mex_C_win64.xml" foo_sf.c
Error: An options file for MEX was found, but the value for 'COMPILER'
was not set. This could mean that the value is not specified
within the options file, or it could mean that there is a
syntax error within the file.
C:\PROGRA~1\MATLAB\R2016b\BIN\MEX.PL: Error: Options file is invalid.

Best Answer

The above error message indicates that the build process invoked mex.pl (Perl script from an obsolete workflow), instead of mex.bat. This can happen of the PATHEXT environment variable has the .PL extension listed before the .BAT extension. In this case, the output from the getenv('PATHEXT') command may look as given below:
>> getenv('PATHEXT')
ans =
PL;.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
In order for the build process to succeed, please overwrite the PATHEXT enviroment variable for your MATLAB session so that the .PL extension is not included (or placed somewhere behind '.BAT'), by using the following command:
>> setenv('PATHEXT','COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC');
In order to automate the execution of the above command at the start of every MATLAB session, add it to a startup.m file as shown in the following documentation page: