Ralt
Ralt

Reputation: 2124

Unable to build mex file

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?

Upvotes: 1

Views: 446

Answers (1)

Rody Oldenhuis
Rody Oldenhuis

Reputation: 38032

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.

Upvotes: 1

Related Questions