Building a MATLAB mex file in Visual Studio gives “LNK2019 unresolved external symbol _mexPrintf referenced in function mexFunction”?

▼魔方 西西 提交于 2019-12-02 01:05:24
Amro

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!