matlab编译运行cpp文件--helloworld

原文地址:https://blog.csdn.net/jkhere/article/details/8906274#commentsedit

#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");   
    }  
上面是一个简单的C++代码,保存成helloworld.cpp。简单解释一下:
  1. void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])   

是MEX的接口子程序,接口子程序是与MATLAB的接口,实现两个不同内存空间中的通信。

在matlab命令行中:

输入 mex helloworld.cpp,编译成功。

结果:

>> mex hello.c
使用 'MinGW64 Compiler (C)' 编译。
MEX 已成功完成。
>> hello(1)//必须传参,不传参matlab报错
hello,world!
>> hello(0)
大家好!!!!
>>

注意:在调用mex之前必须先设置好编译器

猜你喜欢

转载自blog.csdn.net/renlonggg/article/details/80653218
今日推荐