The C/C ++ program calls the MATLAB program through the dynamic link library, and dynamically links the library matlab

Source: Internet
Author: User

The C/C ++ program calls the MATLAB program through the dynamic link library, and dynamically links the library matlab
C/C ++ program calls MATLAB program 1 through dynamic link libraryMATLAB compiler settings

You must set the corresponding C ++ compiler to compile the. m file to generate a library file that can be called by C ++.

In the MATLAB command line, enter: mex-setup; then enter: mbuild-setup, and select the installed VC compiler.

2 Compile the MATLAB program into a C/C ++ Dynamic Link Library

Suppose there is an Add. m file that needs to be compiled into a library file:

function C=Add(A,B)  C=A+B;end

2.1 compile the MATLAB program into a C Dynamic Link Library

Mcc compilation options:

>>mcc –W lib:libname file.m –T link:lib

Use the bundled command file:

>>mcc –B csharedlib:libname file.m

2.2 setMATLABProgram compiledC ++Dynamic Link Library

Mcc compilation options:

>>mcc –W cpplib:libname file.m –T link:lib

Use the bundled command file:

>>mcc –B cpplib:libname file.m

2.3 File Format

After successful compilation, some files are generated in the Add. m path:

3. CreateC/C ++Program callMATLABFunction

3.1Set the project Platform

Set the corresponding project platform based on the number of digits (32 or 64) of MATLAB.

3.2 Engineering Environment Settings

Copy the previously generated libAdd. dll, libAdd. h, and libAdd. lib files to the newly created C ++ project, and set project properties.

  • Project Properties -- VC ++ directory -- include directory:

  • Project Properties -- VC ++ directory -- library directory:

 

 

  • Project Properties -- linker -- input:
Libeng. liblibmat. liblibmex. liblibmx. libmclmcrrt. libmclmcr. liblibAdd. lib (the last one is generated by Add. m)

3.3 C ++ Program

#include <iostream>#include "libAdd.h"using namespace std;int main(){    if (!libAddInitialize())        return -1;    double a[4] = { 1,2,3,4 };    double b[4] = { 1,1,1,1 };    double c[4];    mwArray mwA(2, 2, mxDOUBLE_CLASS);    mwArray mwB(2, 2, mxDOUBLE_CLASS);    mwArray mwC(2, 2, mxDOUBLE_CLASS);    mwA.SetData(a, 4);    mwB.SetData(b, 4);    Add(1, mwC, mwA, mwB);    mwC.GetData(c, 4);    cout << "mwC=\n" << mwC << endl;    cout << "c=" << endl;    for (int i=0;i<4;i++)    {        if (i%2==0)        {            cout<<endl;        }        cout<<'\t'<<c[i];    }    cout<<endl;    libAddTerminate();    return 0;}

Output result:

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.