Calling MATLAB from C++ errors: unresolved external symbol

我怕爱的太早我们不能终老 提交于 2019-12-05 09:05:10
Amro

Before you start, make sure you have a supported compiler installed. That would be Visual C++ and possibly Windows SDK if you are using VS Express edition on a 64-bit Windows. Next you need to configure MATLAB by running these steps at least once:

>> mex -setup
>> mbuild -setup

Now given the following simple function:

MyAdd.m

function c = MyAdd(a,b)
    c = a + b;
end

We want to build a C++ shared library using the MATLAB Compiler mcc:

>> mcc -N -W cpplib:libMyAdd -T link:lib MyAdd.m -v

This will produce a couple of files including a header file, a DLL, and an import library:

libMyAdd.h
libMyAdd.dll
libMyAdd.lib

Next we create a C++ program to test the above library:

MyAdd_test.cpp

#include "libMyAdd.h"

int main()
{
    // initialize MCR and lib
    if (!mclInitializeApplication(NULL,0))  {
        std::cerr << "could not initialize the application" << std::endl;
        return -1;
    }
    if(!libMyAddInitialize()) {
        std::cerr << "Could not initialize the library" << std::endl;
        return -1;
    }

    try {
        // create input
        double a[] = {1.0, 2.0, 3.0, 4.0};
        double b[] = {5.0, 6.0, 7.0, 8.0};
        mwArray in1(2, 2, mxDOUBLE_CLASS, mxREAL);
        mwArray in2(2, 2, mxDOUBLE_CLASS, mxREAL);
        in1.SetData(a, 4);
        in2.SetData(b, 4);

        // call function
        mwArray out;
        MyAdd(1, out, in1, in2);

        // show result
        std::cout << "in1 + in2 = " << std::endl;
        std::cout << out << std::endl;

        double c[4];
        out.GetData(c, 4);
        for(int i=0; i<4; i++) {
            std::cout << c[i] << " " << std::endl;
        }

    } catch (const mwException& e) {
        std::cerr << e.what() << std::endl;
        return -2;
    } catch (...) {
        std::cerr << "Unexpected error thrown" << std::endl;
        return -3;
    } 

    // cleanup
    libMyAddTerminate();   
    mclTerminateApplication();

    return 0;
}

We could compile this program right from inside MATLAB using:

>> mbuild MyAdd_test.cpp libMyAdd.lib -v
>> !MyAdd_test

We could also compile it ourselves using Visual Studio. We start by creating a native console application, then set the project settings as follows:

  • From the menu, select "Project > Properties" and apply the settings to "All configurations" (both debug and release targets)
  • Under C/C++ properties, set the "Additional Include Directories" by adding both the directory containing the generated header file libMyAdd.h, in addition to the directory containing MATLAB's header files:

    $matlabroot\extern\include
    
  • Similarly under "Linker", set the "Additional Library Directories". That would be the same directory as before containing libMyAdd.lib, as well as in my case:

    $matlabroot\extern\lib\win32\microsoft
    

    Then go to "Linker > Input" and add the following inside "Additional Dependencies":

    libMyAdd.lib
    mclmcrrt.lib
    
  • Finally under "Debugging > Environment", you might want to extend the PATH environment variable to include the directory containing the generated libMyAdd.dll file. That way you can directly hit F5 to compile run the program directly from inside VS. This will be something like:

    PATH=%PATH%;C:\path\to\output\folder
    

If you do this kind of thing often, you could create a property sheet once, which could then be reused in other VC++ projects. See this answer for an example.

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