Problem compiling mex files using visual studio.

4 views (last 30 days)
I'm currently using a patch to compile mex files using visual studio 2010 ( http://www.mathworks.com/support/solutions/en/data/1-D5W493/?solution=1-D5W493).
I was able to compile many files but I am having trouble trying to compile an implementation of a decision tree ( http://www.cs.uiuc.edu/homes/dhoiem/software/counter.php?Down=boostDt.zip)
The errors that the mex compiler is giving to me do not make any sense. Does someone had any problem like that?
>> mex treevalc.c
treevalc.c
treevalc.c(26) : warning C4101: 'cid' : unreferenced local variable
treevalc.c(92) : error C2143: syntax error : missing ';' before 'type'
treevalc.c(93) : error C2143: syntax error : missing ';' before 'type'
treevalc.c(94) : error C2143: syntax error : missing ';' before 'type'
treevalc.c(95) : error C2143: syntax error : missing ';' before 'type'
treevalc.c(97) : error C2143: syntax error : missing ';' before 'type'
treevalc.c(98) : error C2143: syntax error : missing ';' before 'type'
treevalc.c(99) : error C2143: syntax error : missing ';' before 'type'
treevalc.c(101) : error C2143: syntax error : missing ';' before 'type'
treevalc.c(102) : error C2065: 'n' : undeclared identifier
treevalc.c(102) : error C2065: 'n' : undeclared identifier
treevalc.c(102) : error C2065: 'nsplits' : undeclared identifier
treevalc.c(102) : error C2065: 'n' : undeclared identifier
treevalc.c(103) : error C2065: 'n' : undeclared identifier
treevalc.c(107) : error C2065: 'ncatsplit' : undeclared identifier
treevalc.c(107) : error C2065: 'n' : undeclared identifier
treevalc.c(107) : error C2109: subscript requires array or pointer type
treevalc.c(108) : error C2065: 'catsplit' : undeclared identifier
treevalc.c(108) : error C2065: 'n' : undeclared identifier
treevalc.c(108) : error C2109: subscript requires array or pointer type
treevalc.c(111) : error C2143: syntax error : missing ';' before 'type'
treevalc.c(112) : error C2143: syntax error : missing ';' before 'type'
treevalc.c(116) : error C2143: syntax error : missing ';' before 'type'
treevalc.c(118) : error C2065: 'numdata' : undeclared identifier
treevalc.c(119) : error C2143: syntax error : missing ';' before 'type'
treevalc.c(122) : error C2143: syntax error : missing ';' before 'type'
treevalc.c(123) : error C2065: 'n' : undeclared identifier
treevalc.c(123) : error C2065: 'n' : undeclared identifier
treevalc.c(123) : error C2065: 'numdata' : undeclared identifier
treevalc.c(123) : error C2065: 'n' : undeclared identifier
treevalc.c(124) : error C2065: 'var' : undeclared identifier
treevalc.c(124) : warning C4047: 'function' : 'int *' differs in levels of indirection from 'int'
treevalc.c(124) : warning C4024: 'treevalc' : different types for formal and actual parameter 1
treevalc.c(124) : error C2065: 'cut' : undeclared identifier
treevalc.c(124) : warning C4047: 'function' : 'double *' differs in levels of indirection from 'int'
treevalc.c(124) : warning C4024: 'treevalc' : different types for formal and actual parameter 2
treevalc.c(124) : error C2065: 'left_child' : undeclared identifier
treevalc.c(124) : warning C4047: 'function' : 'int *' differs in levels of indirection from 'int'
treevalc.c(124) : warning C4024: 'treevalc' : different types for formal and actual parameter 3
treevalc.c(124) : error C2065: 'right_child' : undeclared identifier
treevalc.c(124) : warning C4047: 'function' : 'int *' differs in levels of indirection from 'int'
treevalc.c(124) : warning C4024: 'treevalc' : different types for formal and actual parameter 4
treevalc.c(124) : error C2065: 'ncatsplit' : undeclared identifier
treevalc.c(124) : warning C4047: 'function' : 'int *' differs in levels of indirection from 'int'
treevalc.c(124) : warning C4024: 'treevalc' : different types for formal and actual parameter 5
treevalc.c(124) : error C2065: 'catsplit' : undeclared identifier
treevalc.c(124) : warning C4047: 'function' : 'double **' differs in levels of indirection from 'int'
treevalc.c(124) : warning C4024: 'treevalc' : different types for formal and actual parameter 6
treevalc.c(125) : error C2065: 'all_attributes' : undeclared identifier
treevalc.c(125) : error C2065: 'numatt' : undeclared identifier
treevalc.c(125) : error C2065: 'n' : undeclared identifier
treevalc.c(125) : error C2109: subscript requires array or pointer type
treevalc.c(125) : error C2065: 'tmp_id' : undeclared identifier
treevalc.c(125) : warning C4133: 'function' : incompatible types - from 'int *' to 'double *'
treevalc.c(125) : error C2198: 'treevalc' : too few arguments for call
treevalc.c(126) : error C2065: 'node_ids' : undeclared identifier
treevalc.c(126) : error C2065: 'n' : undeclared identifier
treevalc.c(126) : error C2109: subscript requires array or pointer type
treevalc.c(126) : error C2065: 'tmp_id' : undeclared identifier
treevalc.c(130) : error C2065: 'catsplit' : undeclared identifier
treevalc.c(130) : warning C4022: 'free' : pointer mismatch for actual parameter 1
treevalc.c(131) : error C2065: 'ncatsplit' : undeclared identifier
treevalc.c(131) : warning C4022: 'free' : pointer mismatch for actual parameter 1
C:\PROGRA~1\MATLAB\R2009B\BIN\MEX.PL: Error: Compile of 'treevalc.c' failed.
  4 Comments
Hadok
Hadok on 19 Dec 2012
the code is as follow. It is a implementation of a boosted decision tree.
#include "mex.h"
#include <stdlib.h>
#include <stdio.h>
/**
* Return the decision tree node corresponding to the given value set
*
* var[n]: the attribute ids for node n
* cut[n]: the threshold value for node n
* left_child[n]: the node id of the left child of node n, 0 if node n is terminal
* right_child[n]: the node id of the right child of node n, 0 if node n is terminal
* ncatsplit[c]: the number of values resulting in a left branch
* catsplit[c]: the values that would result in a left branch
* attributes: the attribute (variable) values for each feature
**/
void
treevalc(int* var, double* cut, int* left_child, int* right_child,
int* ncatsplit, double** catsplit,
double* attributes,
int* node_id) {
int currnode = 0;
int nextnode;
int currvar;
double currval;
int cid, v;
int numvals;
double* vals;
/* printf("init nodes: %d %d \n", left_child[currnode], right_child[currnode]); */
/* until reached terminal node */
while ((left_child[currnode] != 0) && (right_child[currnode] != 0)) {
/*printf("currnode: %d\n", currnode);*/
nextnode = -1;
currvar = abs(var[currnode])-1;
currval = attributes[currvar];
/* decision based on thresholded float value */
if (var[currnode] > 0) {
/*printf("currvar: %d\n", currvar);*/
/* branch left */
if (currval < cut[currnode]) {
nextnode = left_child[currnode];
}
/* branch right */
else {
nextnode = right_child[currnode];
}
}
/* decision based on discrete value */
else {
numvals = ncatsplit[(int)cut[currnode]-1];
vals = catsplit[(int)cut[currnode]-1];
for (v = 0; v < numvals; v++) {
if (currval == vals[v]) {
nextnode = left_child[currnode];
break;
}
}
if (nextnode == -1) {
nextnode = right_child[currnode];
}
}
currnode = nextnode-1;
/* printf("curr node: %d \n", currnode);*/
}
*node_id = currnode+1;
}
/**
* plhs = {var, cut, left_child, right_child, catsplit(cell array), attributes(numatt, numdata)}
*
*/
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
if (nrhs != 6) {
printf("Error: wrong number of input arguments: %d.\n", nlhs);
printf("Syntax: node_ids = treevalc(var, cut, left_child, right_child, catsplit, attributes)\n");
}
int* var = (int*)mxGetPr(prhs[0]);
double* cut = mxGetPr(prhs[1]);
int* left_child = (int*)mxGetPr(prhs[2]);
int* right_child = (int*)mxGetPr(prhs[3]);
/* get catsplit variables */
int nsplits = mxGetNumberOfElements(prhs[4]);
int* ncatsplit = malloc(sizeof(int) * nsplits);
double** catsplit = malloc(sizeof(double*) * nsplits);
int n = 0;
for (n = 0; n < nsplits; n++) {
mxArray* catsplit_cell_mx = mxGetCell(prhs[4], n);
if (catsplit_cell_mx == 0) {
printf("null cell");
}
ncatsplit[n] = mxGetNumberOfElements(catsplit_cell_mx);
catsplit[n] = (double*)mxGetPr(catsplit_cell_mx);
}
int numatt = mxGetM(prhs[5]);
int numdata = mxGetN(prhs[5]);
/* printf("num data = %d num att = %d\n", numdata, numatt);*/
double* all_attributes = mxGetPr(prhs[5]);
plhs[0] = mxCreateDoubleMatrix(numdata, 1, mxREAL);
double* node_ids = mxGetPr(plhs[0]);
int tmp_id;
for (n = 0; n < numdata; n++) {
treevalc(var, cut, left_child, right_child, ncatsplit, catsplit,
&all_attributes[numatt*n], &tmp_id);
node_ids[n] = (double)(tmp_id);
/* printf("final node id: %d\n", tmp_id); */
}
free(catsplit);
free(ncatsplit);
}
Hadok
Hadok on 19 Dec 2012
line 91 and 92 are:
int* var = (int*)mxGetPr(prhs[0]);
where prhs is the parameter of the function
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])

Sign in to comment.

Accepted Answer

Hadok
Hadok on 19 Dec 2012
Found the solution. The mex compiler was using a restrict C compiler, so I could not create variable in the middle of the function. Thx for the effort guys.
  3 Comments
Omar
Omar on 19 Jun 2013
Hadok, would you please explain what you eventually did to mex treevalc.c. Is it possible to change on VS10 compiler to resolve this problem? Thanks.
Rajen Bhatt
Rajen Bhatt on 12 Jan 2015
Jan and Hadok: Can you guys please elaborate how you have resolved this problem? I am trying to compile treevalc.c file on Windows MATLAB 2013b and it is giving me same errors which Hadok has described earlier. This is so crucial for my program that without it I can not move further. Your help will be much appreciated. If you want to share compiled mexw, that too is welcome !

Sign in to comment.

More Answers (2)

Jan
Jan on 19 Dec 2012
Edited: Jan on 19 Dec 2012
Some ideas which do not solve your actual problem:
if (nrhs != 6) {
printf("Error: wrong number of input arguments: %d.\n", nlhs);
You check nrhs but print nlhs.
Is printf defined or do you have to call mexPrintf?
Concerning your problem:
Is this compiled as C89? Then the declarations have to appear before any non-declaration code in each function. See last point of http://msdn.microsoft.com/en-us/library/vstudio/0afb82ta.aspx.
It is strange to convert the value of the input array to "int": This will crash, when the "int" has not as many bits as you expect. Better use "int32_T", if the Matlab array is a int32 - and be sure to check this explicitly:
if (!mxIsInt32(prhs[0])) { ...

Walter Roberson
Walter Roberson on 19 Dec 2012
Notice that the problem occurs at the very first place that uses mxArray . The problem is as-if mex.h is not complete.
Was mex.h supplied as part of the source code? If so then that mex.h would override mex.h expected to be supplied by the "mex" command.

Categories

Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!