2

I'm using Visual Studio 2012 x64 to build and debug a MATLAB 2014a x64 mex file directly (without using the mex command in MATLAB). I followed the instructions in this question to set up a Visual Studio project named test1. I followed this tutorial to write a simple mex file, test1.cpp:

#include <math.h>
#include <matrix.h>
#include <mex.h>

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
  mexPrintf("Hello World!\n");
}

Building this solution gives me this message:

> 1>------ Build started: Project: test1, Configuration: Debug Win32
> ------ 1>     Creating library C:\PROJECTS\matlab\mex\test1\Debug\test1.lib
> and object
> C:\PROJECTS\matlab\mex\test1\Debug\test1.exp
> 1>test1.obj : error LNK2019: unresolved external symbol _mexPrintf
> referenced in function _mexFunction 1>MSVCRTD.lib(crtexe.obj) : error
> LNK2019: unresolved external symbol _main referenced in function
> ___tmainCRTStartup 1>C:\PROJECTS\matlab\mex\test1\Debug\test1.mexw64
> : fatal error LNK1120: 2 unresolved externals
> ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Are there configuration steps I'm missing here? I used the steps in this answer exactly, but maybe it's incomplete for more recent versions of MATLAB?

Community
  • 1
  • 1
Michael A
  • 4,391
  • 8
  • 34
  • 61
  • Have you set the linker search path to where `libmx.lib,libmex.lib` located? – lanpa Oct 06 '14 at 16:11
  • 1
    @lanpa I followed the steps in the linked answer, so I 1) added `$(MATLAB_ROOT)\extern\lib\win64\microsoft;` to Additional Library Directories in Configuration Properties -> Linker -> General, and 2) added `libmx.lib;libmex.lib;libmat.lib;` to Additional Dependencies in Configuration Properties -> Linker -> Input. Are there other steps I'm missing, based on your comment? – Michael A Oct 06 '14 at 16:16
  • @MA: did you actually write `$(MATLAB_ROOT)` literally? You are supposed to replace that with the full path to your MATLAB installation root directory – Amro Oct 06 '14 at 16:42
  • @Amro No, I used `C:\program files\MATLAB\R2014a`. The `$(MATLAB_ROOT)` is just shorthand that the answer I linked to uses. – Michael A Oct 06 '14 at 16:43
  • @Amro What did your answer say about making a DLL? It disappeared after a minute or so. Do I need to create an "MFC DLL" project in Visual Studio? – Michael A Oct 06 '14 at 16:44
  • 1
    I retracted my answer because I wasn't sure it was the case :( Just to confirm, you need to create a VS project that produces a DLL (regular kind, no need for MFC). So go to "File" menu, "new project", under "Visual C++ > Win32", select "Win32 console application", and in the wizard, specify "DLL" and start with an empty project – Amro Oct 06 '14 at 16:47
  • you also need to pay attention to the architecture (32 vs. 64 bit). That means you have to switch the platform configuration to "x64" (since you are running MATLAB x64) – Amro Oct 06 '14 at 16:54
  • @Amro It looks like targeting a DLL instead of a console application fixed the build problem, so if you make that an answer I'll accept it. MATLAB is also able to run the mex file, so I'm targeting the right build platform (x64). Thanks! – Michael A Oct 06 '14 at 17:02

1 Answers1

3

You need to create a Visual Studio project that produces DLLs (as opposed to console or GUI applications). MEX-files are basically shared libraries with a custom extension (*.mexw32 or *.mexw64 on Windows)

The error message indicates that the linker cannot find the entry-point function main() for an executable, which does not exist for dynamic libraries.

For what it's worth, you can run mex -v ... in MATLAB, that way it shows you the exact commands used to invoke the compiler and linker.


EDIT:

Step-by-step instructions:

  • create a new empty VS project to build DLLs (Visual C++ > Win32 > Win32 console application then set the type to DLL in the wizard)

  • Since we're working with MATLAB x64, we need to adjust project configuration to generate 64-bit binaries. From Build menu, select Configuration Manager. From the drop-down menu choose <New> to create a new platform configs, select x64 and accept.

  • follow the previous instructions you mentioned

  • add the C/C++ source code for the MEX-file

    #include "mex.h"
    void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
    {
        plhs[0] = mxCreateString("hello world");
    }
    
  • switch platform to "x64" in "Release" mode, and build the project. Now you should have a somefile.mexw64 MEX-file which you can call in MATLAB as a regular function.

Community
  • 1
  • 1
Amro
  • 123,847
  • 25
  • 243
  • 454