我在使用Windows 7 x64的matlab 2014a上尝试使用mex函数时遇到了一些错误。任何mex的最简单示例都会生成如下所列的错误。我使用Windows 7.1 sdk编译器,vc ++ 2010编译器和vc ++编译器,错误并没有改变。使用此配置时,任何人都遇到过这个问题吗?我会为此提供一些帮助。
C:\Program Files\MATLAB\R2014a\extern\include\mex.h(37) : error C2143: syntax error : missing ';' before '*'
C:\Program Files\MATLAB\R2014a\extern\include\mex.h(37) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
C:\Program Files\MATLAB\R2014a\extern\include\mex.h(37) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
C:\Program Files\MATLAB\R2014a\extern\include\mex.h(42) : error C2146: syntax error : missing ';' before identifier 'f'
C:\Program Files\MATLAB\R2014a\extern\include\mex.h(42) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
C:\Program Files\MATLAB\R2014a\extern\include\mex.h(42) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
C:\Program Files\MATLAB\R2014a\extern\include\mex.h(75) : error C2065: 'mxFunctionPtr' : undeclared identifier
C:\Program Files\MATLAB\R2014a\extern\include\mex.h(75) : error C2146: syntax error : missing ')' before identifier 'f'
C:\Program Files\MATLAB\R2014a\extern\include\mex.h(75) : error C2513: 'const char **' : no variable declared before '='
C:\Program Files\MATLAB\R2014a\extern\include\mex.h(75) : error C2059: syntax error : ')'
C:\Program Files\MATLAB\R2014a\extern\include\mex.h(120) : error C2061: syntax error : identifier 'mxArray'
C:\Program Files\MATLAB\R2014a\extern\include\mex.h(171) : error C2065: 'mxArray' : undeclared identifier
C:\Program Files\MATLAB\R2014a\extern\include\mex.h(172) : error C2065: 'pa' : undeclared identifier
C:\Program Files\MATLAB\R2014a\extern\include\mex.h(172) : error C2182: 'mexMakeArrayPersistent' : illegal use of type 'void'
C:\Program Files\MATLAB\R2014a\extern\include\mex.h(172) : fatal error C1903: unable to recover from previous error(s);
停止编译
编辑1:
如下所示Sugested,所有错误都改为:
error C2014: preprocessor command must start as first
error C2061: syntax error : identifier 'mxArray'
error C2065: 'nrhs' : undeclared identifier
error C3861: 'mexErrMsgIdAndTxt': identifier not found
error C3861: 'mexErrMsgIdAndTxt': identifier not found
error C2065: 'prhs' : undeclared identifier
error C2065: 'prhs' : undeclared identifier
error C3861: 'mxGetN': identifier not found
error C3861: 'mxGetN': identifier not found
error C3861: 'mexErrMsgIdAndTxt': identifier not found
error C2065: 'prhs' : undeclared identifier
error C3861: 'mxGetN': identifier not found
error C2065: 'prhs' : undeclared identifier
error C3861: 'mxGetPr': identifier not found
error C2065: 'prhs' : undeclared identifier
error C3861: 'mxGetPr': identifier not found
error C2065: 'prhs' : undeclared identifier
error C3861: 'mxGetM': identifier not found
error C2065: 'prhs' : undeclared identifier
error C3861: 'mxGetM': identifier not found
error C2065: 'plhs' : undeclared identifier
error C2065: 'mxREAL' : undeclared identifier
error C3861: 'mxCreateDoubleMatrix': identifier not
error C2065: 'plhs' : undeclared identifier
error C3861: 'mxGetPr': identifier not found
fatal error C1075: end of file found before the left
brace '{' at 'pukkernelMex.cpp(3)' was matched
编辑2 - pukkernel的内容:
#include <stdio.h>
#include <math.h>
extern "C" { #include "mex.h" }
void pukKernel(double *A, double *B, int rowsA, int rowsB, int columns, double *out)
{
double w = 1.0;
double s = 1.0;
double cte1 = 1/s;
double cte2 = sqrt(pow(2,(1/w))-1);
for (int pattA = 0; pattA < rowsA; pattA++) {
for (int pattB = 0; pattB < rowsB; pattB++) {
double norma = 0;
for (int c = 0; c < columns; c++) {
norma += pow(A[pattA+(c)*rowsA] - B[pattB+(c)*rowsB], 2);
}
norma = sqrt(norma);
double de = 2*norma;
double aux = cte1 * de * cte2;
out[pattA+(pattB)*rowsA] = 1 / pow((1+pow(aux,2)), w);
}
}
}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
if (nrhs != 2) {
mexErrMsgIdAndTxt("Test:pukkernel:nrhs.",
"Two inputs required.");
}
if (nlhs != 1) {
mexErrMsgIdAndTxt("Test:pukkernel:nlhs",
"One output required.");
}
if (mxGetN(prhs[0]) != mxGetN(prhs[1])) {
mexErrMsgIdAndTxt("Test:pukkernel:differentDimensions",
"Input matrixes must have same dimensions.");
}
// Input
int columns = mxGetN(prhs[0]);
double *A = (double *) mxGetPr(prhs[0]);
double *B = (double *) mxGetPr(prhs[1]);
int rowsA = mxGetM(prhs[0]);
int rowsB = mxGetM(prhs[1]);
// Output
plhs[0] = mxCreateDoubleMatrix(rowsA, rowsB, mxREAL);
double *outMatrix = mxGetPr(plhs[0]);
pukKernel(A, B, rowsA, rowsB, columns, outMatrix);
}