1

I've followed the MATLAB example for creating a mex file from here https://uk.mathworks.com/help/matlab/matlab_external/standalone-example.html

The source code it produces is as follows

#include "mex.h"

/* The computational routine */
void arrayProduct(double x, double *y, double *z, mwSize n)
{
    mwSize i;
    /* multiply each element y by x */
    for (i=0; i<n; i++) {
        z[i] = x * y[i];
    }
}

/* The gateway function */
void mexFunction( int nlhs, mxArray *plhs[],
                  int nrhs, const mxArray *prhs[])
{
    double multiplier;              /* input scalar */
    double *inMatrix;               /* 1xN input matrix */
    size_t ncols;                   /* size of matrix */
    double *outMatrix;              /* output matrix */

    /* check for proper number of arguments */
    if(nrhs!=2) {
        mexErrMsgIdAndTxt("MyToolbox:arrayProduct:nrhs","Two inputs required.");
    }
    if(nlhs!=1) {
        mexErrMsgIdAndTxt("MyToolbox:arrayProduct:nlhs","One output required.");
    }
    /* make sure the first input argument is scalar */
    if( !mxIsDouble(prhs[0]) || 
         mxIsComplex(prhs[0]) ||
         mxGetNumberOfElements(prhs[0])!=1 ) {
        mexErrMsgIdAndTxt("MyToolbox:arrayProduct:notScalar","Input multiplier must be a scalar.");
    }

    /* make sure the second input argument is type double */
    if( !mxIsDouble(prhs[1]) || 
         mxIsComplex(prhs[1])) {
        mexErrMsgIdAndTxt("MyToolbox:arrayProduct:notDouble","Input matrix must be type double.");
    }

    /* check that number of rows in second input argument is 1 */
    if(mxGetM(prhs[1])!=1) {
        mexErrMsgIdAndTxt("MyToolbox:arrayProduct:notRowVector","Input must be a row vector.");
    }

    /* get the value of the scalar input  */
    multiplier = mxGetScalar(prhs[0]);

    /* create a pointer to the real data in the input matrix  */
    inMatrix = mxGetPr(prhs[1]);

    /* get dimensions of the input matrix */
    ncols = mxGetN(prhs[1]);

    /* create the output matrix */
    plhs[0] = mxCreateDoubleMatrix(1,(mwSize)ncols,mxREAL);

    /* get a pointer to the real data in the output matrix */
    outMatrix = mxGetPr(plhs[0]);

    /* call the computational routine */
    arrayProduct(multiplier,inMatrix,outMatrix,(mwSize)ncols);
}

When I run the command mex arrayProduct.cpp (the name of my file), I get the following error:

Building with 'Microsoft Visual C++ 2017'. Error using mex LINK : error LNK2001: unresolved external symbol mexfilerequiredapiversion arrayProduct.lib : fatal error LNK1120: 1 unresolved externals

I'm using MATLAB 2015b 32-bit, with the Visual Studio 2017 C++ compiler. Is there some preliminary set-up required for making mex files that isn't mentioned in the MATLAB tutorial?

Rody Oldenhuis
  • 37,726
  • 7
  • 50
  • 96
Ralt
  • 2,084
  • 1
  • 26
  • 38
  • The [youngest supported compiler for MATLAB R2015b is MSVC Professional 2015](https://www.mathworks.com/content/dam/mathworks/mathworks-dot-com/support/sysreq/files/SystemRequirements-Release2015b_SupportedCompilers.pdf). However, even if this is part of the problem, a link error like the one you're getting is not the error I'd expect... – Rody Oldenhuis Nov 16 '17 at 15:32
  • Ah. [R2015b is the latest version with 32-bit support](https://en.wikipedia.org/wiki/MATLAB). Your compiler is probably MSVC 2017, 64-bit. – Rody Oldenhuis Nov 16 '17 at 15:33
  • @RodyOldenhuis currently using this fix to get 2015b to support the 2017 compiler https://www.mathworks.com/support/bugreports/1487958 – Ralt Nov 16 '17 at 15:34
  • ...that's a fix for a bug in R2016b/R2017a? – Rody Oldenhuis Nov 16 '17 at 15:35
  • Try to install .NET4 + SDK 7.1, select that in MATLAB, and re-run your `mex` command -- that *is* an officially supported compiler, and I expect that that solves your problem. – Rody Oldenhuis Nov 16 '17 at 15:36
  • @RodyOldenhuis I'll give it a go. Looks like it comes with its own issues https://uk.mathworks.com/matlabcentral/answers/233850-how-can-i-install-sdk-7-1-on-windows-10. I'll get back to you tomorrow. Thanks for the help for now buddy. – Ralt Nov 16 '17 at 15:41
  • Yes. I recently had to do the exact same, but for R2014a. .NET 4 refused to install for some reason, which [this answer](https://stackoverflow.com/a/33260090/1085062) solved for me. – Rody Oldenhuis Nov 16 '17 at 15:43
  • So I take it, this worked for you? Any further hick-ups? – Rody Oldenhuis Nov 20 '17 at 09:48

1 Answers1

1

The youngest supported compiler for MATLAB R2015b is MSVC Professional 2015. Also, R2015b is the latest version with 32-bit support. Your compiler is probably MSVC 2017, 64-bit.

Try to install .NET4 + SDK 7.1, select that in MATLAB, and re-run your mex command. That is an officially supported compiler for R2015b, and I expect that that solves your problem.

Note: for me, .NET4 refused to install because it detected a previously installed framework, but this answer resolved that problem for me.

Rody Oldenhuis
  • 37,726
  • 7
  • 50
  • 96