MATLAB Answers

Mex compilation error in Windows

48 views (last 30 days)
Wajahat
Wajahat on 7 Sep 2012
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.

  4 Comments

Show 1 older comment
Wajahat
Wajahat on 8 Sep 2012
Here is the code. Please note that there is nothing wrong with the code. It compiles perfectly in Linux with gcc configured with matlab. This error only appears when I try to compile in windows with VC++ configured in matlab.
/*
* See licensing information in bttc.h
*/
#include "bttc.h"
#include "mex.h"
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] ) { int *faces, n; double *img, threshold; int size;
/* Check parameters. */
if (nrhs < 2)
mexErrMsgTxt("At least two input arguments required.");
if ((nlhs < 1) || (nlhs > 2))
mexErrMsgTxt("Function must have one or two outputs.");
if (!mxIsDouble(prhs[0]))
mexErrMsgTxt("Image parameter must be double matrix." );
if (!mxIsDouble(prhs[1]))
mexErrMsgTxt("Threshold parameter must be double." );
/* Get parameters. */
img = (double*)mxGetData( prhs[0] );
threshold = mxGetScalar( prhs[1] );
/* Checks. */
const mwSize *dim = mxGetDimensions(prhs[0]);
if (dim[0] != dim[1])
mexErrMsgTxt("Image must be square.");
size = dim[0];
if (((size-1)<=0) || (((size-1) & (size-2)) != 0))
mexErrMsgTxt("Image side must be in the form of 2^m+1.");
/* Actual algorithm. */
faces = bttc( &n, img, size, size, threshold );
if (faces == NULL) {
mexErrMsgTxt("bttc failed to run.");
return;
}
if (nlhs == 1) {
int dims[2];
dims[0] = n;
dims[1] = 6;
plhs[0] = mxCreateNumericArray( 2, dims, mxDOUBLE_CLASS, mxREAL );
double *dat = (double*)mxGetData(plhs[0]);
int i, j;
for (i=0; i<n; i++)
for (j=0; j<6; j++)
dat[ j*n + i ] = (double) faces[ 6*i + j ]+1;
}
else {
int nvert;
double *vert;
int *face, *v;
int i, j, k, f;
/* Do face meshing. */
nvert = 0;
vert = malloc( n * 3*3*sizeof(double) ); /* Worst case, every vertex is unique. */
face = malloc( n * 3*sizeof(int) );
for (i=0; i<n; i++) {
for (j=0; j<3; j++) {
v = &faces[ 6*i + 2*j ];
f = 0;
for (k=0; k<nvert; k++) {
if ((v[0]==(int)vert[3*k+0]) &&
(v[1]==(int)vert[3*k+1])) {
f = 1;
break;
}
}
/* Already exists. */
if (!f) {
vert[ 3*nvert+0 ] = v[0];
vert[ 3*nvert+1 ] = v[1];
vert[ 3*nvert+2 ] = img[ size*v[1] + v[0] ];
k = nvert;
nvert++;
}
/* Set the face id. */
face[ 3*i+j ] = k;
}
}
double *dat;
int dims[2];
/* Save the vertex array. */
dims[0] = nvert;
dims[1] = 3;
plhs[0] = mxCreateNumericArray( 2, dims, mxDOUBLE_CLASS, mxREAL );
dat = (double*)mxGetData(plhs[0]);
for (i=0; i<nvert; i++) {
dat[ 0*dims[0] + i ] = vert[ 3*i + 0 ]+1.;
dat[ 1*dims[0] + i ] = vert[ 3*i + 1 ]+1.;
dat[ 2*dims[0] + i ] = vert[ 3*i + 2 ];
}
/* Save the faces. */
dims[0] = n;
plhs[1] = mxCreateNumericArray( 2, dims, mxDOUBLE_CLASS, mxREAL );
dat = (double*)mxGetData(plhs[1]);
for (i=0; i<n; i++)
for (j=0; j<3; j++)
dat[ j*dims[0] + i ] = (double)face[ 3*i + j ]+1;
/* Clean up. */
free(face);
free(vert);
}
/* Clean up. */
free(faces);
}
end
Wajahat
Wajahat on 8 Sep 2012
You can even try to compile the whole package yourself if you download it from: https://github.com/bobbens/libbttc/zipball/bttc-1.0
unzip into a folder, change Matlab's working directory to that folder and type:
mex bttc_m.c bttc.c
Jan
Jan on 9 Sep 2012
With "relevant part" I meant the relevant part only. Most of all it would be friendly to mention, which is the "line 32".

Sign in to comment.

Accepted Answer

Jan
Jan on 9 Sep 2012
Edited: Jan on 9 Sep 2012
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".

  3 Comments

Wajahat
Wajahat on 9 Sep 2012
Great, it worked. Simply naming my .c files to .cpp it compiles with VC++2010 configured with MATLAB. Thanks a lot Jan.
Walter Roberson
Walter Roberson on 9 Sep 2012
Jan, variables are allowed mixed in with code in the C99 standard.
Jan
Jan on 9 Sep 2012
@Walter: Thanks, I've added this detail. MSVC2010 replies TRUE for some comparisons with NaN's, what clearly breaks IEEE754. I'm afraid, that standards from 1989 and 1999 are meaningless, when the manufacturers of compilers do not care about them in 2012. Frustrating.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 8 Sep 2012
Unlike MATLAB, in c you need to declare variables. It looks like it's telling you that you didn't declare them as int, char, etc.

  2 Comments

Wajahat
Wajahat on 8 Sep 2012
Yeah, I know and all the variables are defined correctly and this is the reason that the same code compiles perfectly in Linux with matlab configured with gcc.
Even when I configure Matlab in Windows with Mingw (gcc) using Gnumex, it still works. I am just curious what is wrong that it does not compile with VC++ Visual Studio 2010 configured in Matlab.
Whereas I can compile other files, e.g. sample files in matlab with VC++.
Image Analyst
Image Analyst on 8 Sep 2012
You paid a lot for the compiler (which includes support for it), so I'd call them and get your money's worth. They'll help you figure it out.

Sign in to comment.

Sign in to answer this question.


Translated by