MATLAB: Mex compilation error in Windows

ccompilationgccgnumexlinuxMATLAB Compilermexvisual studio 2010windows

Hi
I have a piece of code which compiles without any error or warning in Linux with GCC but in windows with Visual C++2010 as the compiler, give a list of following errors. Any idea what is the reason:
bttc_m.c(32) : error C2143: syntax error : missing ';' before 'const'
bttc_m.c(33) : error C2065: 'dim' : undeclared identifier
bttc_m.c(33) : error C2109: subscript requires array or pointer type
bttc_m.c(33) : error C2065: 'dim' : undeclared identifier
bttc_m.c(33) : error C2109: subscript requires array or pointer type
bttc_m.c(35) : error C2065: 'dim' : undeclared identifier
bttc_m.c(35) : error C2109: subscript requires array or pointer type
bttc_m.c(43) : error C2143: syntax error : missing ';' before 'type'
bttc_m.c(73) : error C2143: syntax error : missing ';' before 'type'
bttc_m.c(74) : error C2143: syntax error : missing ';' before 'type'
bttc_m.c(75) : error C2065: 'i' : undeclared identifier
bttc_m.c(75) : error C2065: 'i' : undeclared identifier
bttc_m.c(75) : error C2065: 'i' : undeclared identifier
bttc_m.c(76) : error C2065: 'j' : undeclared identifier
bttc_m.c(76) : error C2065: 'j' : undeclared identifier
bttc_m.c(76) : error C2065: 'j' : undeclared identifier
bttc_m.c(77) : error C2065: 'dat' : undeclared identifier
bttc_m.c(77) : error C2065: 'j' : undeclared identifier
bttc_m.c(77) : error C2065: 'i' : undeclared identifier
bttc_m.c(77) : error C2109: subscript requires array or pointer type
bttc_m.c(77) : error C2065: 'i' : undeclared identifier
bttc_m.c(77) : error C2065: 'j' : undeclared identifier
bttc_m.c(116) : error C2143: syntax error : missing ';' before 'type'
bttc_m.c(117) : error C2143: syntax error : missing ';' before 'type'
bttc_m.c(119) : error C2065: 'dims' : undeclared identifier
bttc_m.c(119) : error C2109: subscript requires array or pointer type
bttc_m.c(120) : error C2065: 'dims' : undeclared identifier
bttc_m.c(120) : error C2109: subscript requires array or pointer type
bttc_m.c(121) : error C2065: 'dims' : undeclared identifier
bttc_m.c(121) : warning C4047: 'function' : 'const int *' differs in levels of indirection from 'int'
bttc_m.c(121) : warning C4024: 'mxCreateNumericArray_700' : different types for formal and actual parameter 2
bttc_m.c(122) : error C2065: 'dat' : undeclared identifier
bttc_m.c(122) : warning C4047: '=' : 'int' differs in levels of indirection from 'double *'
bttc_m.c(124) : error C2065: 'dat' : undeclared identifier
bttc_m.c(124) : error C2065: 'dims' : undeclared identifier
bttc_m.c(124) : error C2109: subscript requires array or pointer type
bttc_m.c(125) : error C2065: 'dat' : undeclared identifier
bttc_m.c(125) : error C2065: 'dims' : undeclared identifier
bttc_m.c(125) : error C2109: subscript requires array or pointer type
bttc_m.c(126) : error C2065: 'dat' : undeclared identifier
bttc_m.c(126) : error C2065: 'dims' : undeclared identifier
bttc_m.c(126) : error C2109: subscript requires array or pointer type
bttc_m.c(129) : error C2065: 'dims' : undeclared identifier
bttc_m.c(129) : error C2109: subscript requires array or pointer type
bttc_m.c(130) : error C2065: 'dims' : undeclared identifier
bttc_m.c(130) : warning C4047: 'function' : 'const int *' differs in levels of indirection from 'int'
bttc_m.c(130) : warning C4024: 'mxCreateNumericArray_700' : different types for formal and actual parameter 2
bttc_m.c(131) : error C2065: 'dat' : undeclared identifier
bttc_m.c(131) : warning C4047: '=' : 'int' differs in levels of indirection from 'double *'
bttc_m.c(134) : error C2065: 'dat' : undeclared identifier
bttc_m.c(134) : error C2065: 'dims' : undeclared identifier
bttc_m.c(134) : error C2109: subscript requires array or pointer type
bttc_m.c(176) : error C2143: syntax error : missing ';' before 'type'
bttc_m.c(177) : error C2143: syntax error : missing ';' before 'type'
bttc_m.c(179) : error C2065: 'dims' : undeclared identifier
bttc_m.c(179) : error C2109: subscript requires array or pointer type
bttc_m.c(180) : error C2065: 'dims' : undeclared identifier
bttc_m.c(180) : error C2109: subscript requires array or pointer type
bttc_m.c(181) : error C2065: 'dims' : undeclared identifier
bttc_m.c(181) : warning C4047: 'function' : 'const int *' differs in levels of indirection from 'int'
bttc_m.c(181) : warning C4024: 'mxCreateNumericArray_700' : different types for formal and actual parameter 2
bttc_m.c(182) : error C2065: 'dat' : undeclared identifier
bttc_m.c(182) : warning C4047: '=' : 'int' differs in levels of indirection from 'double *'
bttc_m.c(184) : error C2065: 'dat' : undeclared identifier
bttc_m.c(184) : error C2065: 'dims' : undeclared identifier
bttc_m.c(184) : error C2109: subscript requires array or pointer type
bttc_m.c(185) : error C2065: 'dat' : undeclared identifier
bttc_m.c(185) : error C2065: 'dims' : undeclared identifier
bttc_m.c(185) : error C2109: subscript requires array or pointer type
bttc_m.c(186) : error C2065: 'dat' : undeclared identifier
bttc_m.c(186) : error C2065: 'dims' : undeclared identifier
bttc_m.c(186) : error C2109: subscript requires array or pointer type
bttc_m.c(189) : error C2065: 'dims' : undeclared identifier
bttc_m.c(189) : error C2109: subscript requires array or pointer type
bttc_m.c(190) : error C2065: 'dims' : undeclared identifier
bttc_m.c(190) : warning C4047: 'function' : 'const int *' differs in levels of indirection from 'int'
bttc_m.c(190) : warning C4024: 'mxCreateNumericArray_700' : different types for formal and actual parameter 2
bttc_m.c(191) : error C2065: 'dat' : undeclared identifier
bttc_m.c(191) : warning C4047: '=' : 'int' differs in levels of indirection from 'double *'
bttc_m.c(194) : error C2065: 'dat' : undeclared identifier
bttc_m.c(194) : error C2065: 'dims' : undeclared identifier
bttc_m.c(194) : error C2109: subscript requires array or pointer type
bttc_m.c(197) : error C2065: 'dims' : undeclared identifier
bttc_m.c(197) : error C2109: subscript requires array or pointer type
bttc_m.c(198) : error C2065: 'dims' : undeclared identifier
bttc_m.c(198) : error C2109: subscript requires array or pointer type
bttc_m.c(199) : error C2065: 'dims' : undeclared identifier
bttc_m.c(199) : warning C4047: 'function' : 'const int *' differs in levels of indirection from 'int'
bttc_m.c(199) : warning C4024: 'mxCreateNumericArray_700' : different types for formal and actual parameter 2
bttc_m.c(200) : error C2143: syntax error : missing ';' before 'type'
bttc_m.c(201) : error C2065: 'tr' : undeclared identifier
bttc_m.c(201) : warning C4047: '=' : 'int' differs in levels of indirection from 'double *'
bttc_m.c(205) : error C2065: 'tr' : undeclared identifier
bttc_m.c(205) : error C2109: subscript requires array or pointer type
bttc_m.c(221) : error C2065: 't1' : undeclared identifier
bttc_m.c(221) : error C2065: 't0' : undeclared identifier
bttc_m.c(221) : warning C4244: '=' : conversion from 'float' to 'int', possible loss of data
bttc_m.c(222) : error C2065: 't1' : undeclared identifier
C:\PROGRA~2\MATLAB\R2012A\BIN\MEX.PL: Error: Compile of 'bttc_m.c' failed.
Error using mex (line 206)
Unable to complete successfully.

Best Answer

If you configure your compiler to compile C code conform to the C89-standards, the variables must be declared before the code. The declaration inside the code is a C++ or C99 style [EDITED, added C99, thanks Walter]:
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
int *faces, n;
double *img, threshold;
int size;
const mwSize *dim;
...
/* Checks. */
dim = mxGetDimensions(prhs[0]);
MSVC does accept the C++ and C99-style comments started by // in opposite to GCC, which is configured to the C89-style by Matlab. It seems like MSVC is stricter as GCC for the rule "declarations before statements". To test this, you can rename your file to "bttc_m.cpp".
Related Question