MATLAB run cpp file (from configuration to run)

Install the compiler

Baidu experience: How to install and configure MinGW-w64 C/C++ compiler in MATLAB

  1. Additional features-access to additional features-select "MinGW-w64" C/C++ compiler (search in the search box)
  2. Choose the appropriate version to download and install (choose the compiler of the corresponding MATLAB version, choose the compiler version 32-bit and 64-bit)
    R2015b through R2017a____GCC 4.9.2_____http://tdm-gcc.tdragon.net
    R2017b and R2018a_______GCC 5.3______ http:// mingw-w64.org
    R2018b and later__________GCC 6.3______http://mingw-w64.org
  3. Configure environment variables (corresponding to step 7 in Baidu experience)
  4. Check whether the configuration is successful (enter the cmd window, enter "gcc -v" and click the Enter key, if no error is reported, it is successful) The
    cmd
    following display is successful
    Success graph
    5. MATLAB settings, enter the matlab command line input
setenv('MW_MINGW64_LOC','C:\TDM-GCC-64')
mex -setup

Among them, "C:\TDM-GCC-64" is the installation directory of MinGW. This method needs to run this command every time you start MATLAB to use MinGW.
Insert picture description here

test

1. Create a new file in MATLAB, enter the following content, and save it as helloworld.cpp

#include "mex.h"   
    void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])   
    {
    
      
    int i;   
    i=mxGetScalar(prhs[0]); //get input parameter  
    if(i==1)   
      mexPrintf("hello,world!\n");   
    else   
      mexPrintf("大家好!!!!\n");   
    }  

2. Command line input
Sample test

Annotation

The mexFunction function is the main interface for MATLAB to call C++ programs, and the function header is shown below

/*
*	nlhs 输出参数数目
*	plhs 指向输出参数的指针
*	nrhs 输入参数数目
*/
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])

For specific code meaning, please refer to: MATLAB calls C++ program

Guess you like

Origin blog.csdn.net/root_zhb/article/details/109174908
run