아래와 같이 따라해보니 간단한 mex파일이 생성되었다.

http://stackoverflow.com/questions/26220193/building-a-matlab-mex-file-in-visual-studio-gives-lnk2019-unresolved-external-s

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 , (이름입력후 ok를 누른 후) then set the (application) 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.

--------------------------------------------

위의 instructions 은 아래이다. (http://stackoverflow.com/questions/16716821/how-to-build-mex-file-directly-in-visual-studio/16718578#16718578)

After some experimenting with guidance from this page mentioned in the question, it seems like starting with an empty C++ project the following settings in the project's Property Pages are necessary and sufficient to build a working .mexw64 from Visual Studio 2010:

Configuration properties -> General:
    Set Target Extension to .mexw64
    Set Configuration Type to Dynamic Library (.dll)

Configureation poperties -> VC++ Directories:
    Add $(MATLAB_ROOT)\extern\include; to Include Directories

Configuration properties -> Linker -> General:
    Add $(MATLAB_ROOT)\extern\lib\win64\microsoft; to Additional Library Directories

Configuration properties -> Linker -> Input:
    Add libmx.lib;libmex.lib;libmat.lib; to Additional Dependencies

Configuration properties -> Linker -> Command Line:
    Add /export:mexFunction to Additional Options

$(MATLAB_ROOT) is the path to Matlab's root folder, eg. C:\Program Files\MATLAB\R2013a.

So far this has only been tried from a solution created from scratch and built for Matlab 2013a 64-bit. I assume that to build for 32-bit one only needs to change both occurrences of 64 to 32. I will update the post when I have confirmed that this works for an existing solution.

EDIT: As expected this works for projects added to existing solutions. Remember to set the new project to be dependent on the project that creates the library.

Edit 2: Following this question I can confirm that the above steps work in Visual Studio 2012 and 2013 too.

Posted by uniqueone
,