How to Call Matlab Functions from C++

前端 未结 2 1202
耶瑟儿~
耶瑟儿~ 2020-12-25 15:47

I want to call MATLAB function in my C++ project.

I\'m using Matlab R2010a and Visual Studio 2010

First I created a simple matlab function:

f         


        
相关标签:
2条回答
  • 2020-12-25 15:47

    maybe it is too late but for the future.

    Include foo.h.

    Add C/C++-General-Additional Include Directories the way to the matlab headers (C:\Program Files (x86)\MATLAB\R2009b\extern\include).

    Add foo.lib, mclmcrrt.lib and mclcommain.lib for Linker in Additional Dependencies.

    For linker in Additional Library Directories show the way to your matlab libs(C:\Program Files (x86)\MATLAB\R2009b\extern\lib\win32\microsoft for 32bit ver (matlab and VS versions should be the same. I had to install the second Matlab 32bit version.)).

    I added the way to the foo.lib in my system path.

    Before using your library foo.dll, you should initialize MCR and library function.

    mclInitializeApplication(NULL,0);
    fooInitialize(); 
    

    After using don't forget:

    mclTerminateApplication();
    fooTerminate();
    

    And some demonstration code, looks like:

    int num = 1;
    double numbrIn = 1.5;
    std::cout<<"now we have " << numbrIn << std::endl;
    mwArray array_in(num, 1, mxDOUBLE_CLASS, mxREAL);
    array_in.SetData(&numbrIn,num);
    mwArray array_out;
    foo(1, array_out, array_in);
    array_out.GetData(&numbrIn, num);
    std::cout<<"now we have " << numbrIn << std::endl;
    
    0 讨论(0)
  • 2020-12-25 16:02

    The files foo.h and foo.lib will be required to compile your application. The foo.dll file will need to be shipped with your resulting application, usually in the same directory.

    If you put the foo.h file in the same directory as your source files, you won't need to do anything special to #include "foo.h". You can also add the direct path to foo.lib in the external linker dependencies.

    If you want to store these files outside of your project folder and/or re-use these files in other applications, you can read up on VC++ Directories, Projects and Solutions.

    Edit: You probably also need to add the MATLAB libraries to your include and library paths. Check out the MathWorks support solution Why do I receive the error 'Could not find include file "mclmcrrt.h"' when trying to compile a stand-alone application?

    0 讨论(0)
提交回复
热议问题