MATLAB: Do I receive compiler syntax errors when building a shared library using MATLAB Compiler 4.0 (R14)

MATLAB Compiler

I have the following MATLAB file:
function area = circleArea(rad1)
area = pi*rad1*rad1;
I am using a supported Microsoft Visual C++ compiler and compile the MATLAB code into a C shared library as follows:
mcc -W lib:circlelib -T link:lib circleArea -v
However I receive the following errors:
circlelib.c(113) : error C2143: syntax error : missing ')' before 'constant'
circlelib.c(113) : error C2143: syntax error : missing '{' before 'constant'
circlelib.c(113) : error C2059: syntax error : '<Unknown>'
circlelib.c(113) : error C2059: syntax error : ')'
D:\APPLICATIONS\MATLAB\R2006A\BIN\MEX.PL: Error: Compile of 'circlelib.c' failed.
Error: An error occurred while shelling out to mbuild (error code = 1).
Unable to build executable.
??? Error executing mcc, return status = 1.
This error is only generated when using a Microsoft Visual C++ compiler; the library does successfully compile when using the LCC compiler.

Best Answer

These errors are compiler-specific. They may be caused by a conflict between Microsoft Visual Studio (MSVC) preprocessor symbols and the names of the input arguments to the MATLAB code. In this case, the input argument named "rad1" is the culprit. To work around this, the argument must be renamed.
A useful way of debugging preprocessor syntax errors is to use the "/P" MSVC compiler switch, which sends the preprocessor output to a file. For more information on this flag, refer to MSDN. To activate this when compiling the MATLAB code, perform the following steps:
1. Execute the following command at the MATLAB command prompt:
mbuild -setup
and choose one of the supported Microsoft Visual C++ compilers as specified in the following document:
This will copy a default compiler options file to the user preferences directory as returned by the MATLAB command PREFDIR. For more information, type the following command at the MATLAB command prompt:
web([docroot,'/toolbox/compiler/f6-36769.html'])
2. Execute the command
edit ([prefdir '\compopts.bat'])
3. In this file, under the "Compiler parameters" section, modify the flag "DLLCOMPFLAGS" by appending the "/P" option. The modified flag would look similar to the following:
set DLLCOMPFLAGS=-c -Zp8 -G5 -GX -W3 -nologo -DMSVC -DIBMPC -DMSWIND /P
4. Now recompile the library:
mcc -W lib:circlelib -T link:lib circleArea -v
This will generate a file circlelib.i that contains all the preprocessed code. You should be able to locate the snippet of code that contains the function signature for the circleArea function:
bool __cdecl mlfCircleArea(int nargout, mxArray** area, mxArray* 0x0420)
{
return mclMlfFeval(_mcr_inst, "circleArea", nargout, 1, 1, area, 0x0420);
}
The input argument "rad1" in this case has been replaced with an address.