MATLAB: Does the advanced C MEX-file example on using LAPACK and BLAS functions fail to compile and build correctly

_dgemmblasdgemmdgemm_documentationlapackMATLABmex

When I try to compile the example in the section on "Using LAPACK and BLAS Functions" in the "MATLAB External Interfaces/API" documentation, I receive the following error message that suggests that there might be a header file or something missing:
mex mex_dgemm.c
C:\MATLAB6p5\extern\lib\win32\microsoft\msvc60\libmwlapack.lib
mex_dgemm.c
mex_dgemm.c(24) : error C2065: 'dgemm' : undeclared identifier
C:\MATLAB6P5\BIN\WIN32\MEX.PL: Error: Compile of 'mex_dgemm.c' failed.
??? Error using ==> mex
Unable to complete successfully

Best Answer

This enhancement has been incorporated in Release 2007b (R2007b). For previous product releases, read below for any possible workarounds:
This error occurs because no prototype for dgemm exists in the sample code or in mex.h. Furthermore, when building the MEX-file, you need to link against the import library for the BLAS library, "libmwlapack.lib".
The code below is the modified version of the code that includes the prototype of the dgemm routine (The prototypes for other BAS and LAPACK routines can be found in blas.h and lapack.h, respectively, in the $matlabroot\extern\include folder, where $matlabroot is the MATLAB installation directory, obtained by typing 'matlabroot' at the MATLAB command prompt):
#include "mex.h"
/* Prototype for the DGEMM routine */
extern void dgemm(char*, char*, int*, int*, int*, double*,
double*, int*, double*, int*, double*, double*, int*);
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
double *A, *B, *C, one = 1.0, zero = 0.0;
int m,n,p;
char *chn = "N";
A = mxGetPr(prhs[0]);
B = mxGetPr(prhs[1]);
m = mxGetM(prhs[0]);
p = mxGetN(prhs[0]);
n = mxGetN(prhs[1]);
if (p != mxGetM(prhs[1])) {
mexErrMsgTxt("Inner dimensions of matrix multiply do not match");
}
plhs[0] = mxCreateDoubleMatrix(m, n, mxREAL);
C = mxGetPr(plhs[0]);
/* Pass all arguments to Fortran by reference */
dgemm (chn, chn, &m, &n, &p, &one, A, &m, B, &p, &zero, C, &m);
}
To compile the code with LCC, first run:
mex -setup
and choose the LCC compiler. The command to compile the MEX-file is:
mex dgem2.c D:\Applications\MATLAB701\extern\lib\win32\lcc\libmwlapack.lib
where $MATLAB is the root MATLAB directory. To run the MEX-file, execute:
A = magic(10);
B = rand(10);
out = mex_dgemm(A,B);