Install the compiler
Baidu experience: How to install and configure MinGW-w64 C/C++ compiler in MATLAB
- Additional features-access to additional features-select "MinGW-w64" C/C++ compiler (search in the search box)
- 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 - Configure environment variables (corresponding to step 7 in Baidu experience)
- 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
following display is successful
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.
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
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