尝试使用mex为MATLAB编译多个c ++文件时出错

时间:2013-04-22 14:05:20

标签: c++ matlab compiler-errors header-files mex

我在Ubuntu下使用 MATLAB ,并希望使用 mex 编译一组带有头文件的2个 c ++ 文件。我展示了一个基本的例子和我得到的错误。

此代码从c ++函数生成文本“hello”,该函数从mexFunction开始,并在MATLAB中使用mex编译,(mex mexTryAlex.cpp):

#include <mex.h>
#include <iostream>
using namespace std;

void newfunc(){
    cout<<"hello\n";   
}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) 
{ 
    newfunc();
}

它正常工作。现在我尝试使用带有mex的多个文件和头文件。我创建了一个头文件try.h

#ifndef try_h
#define try_h
void newfunc();
#endif

然后是新函数的文件try.cpp

#include <mex.h>
#include <iostream>
#include <try.h>
using namespace std;

void newfunc(){
    cout<<"hello\n";   
}

这3个文件无法使用mex进行编译:

>> mex  mexTryAlex.cpp try.cpp try.h

Warning: You are using gcc version "4.4.3-4ubuntu5)".  The version
     currently supported with MEX is "4.3.4".
     For a list of currently supported compilers see: 
     http://www.mathworks.com/support/compilers/current_release/

try.cpp:4:17: error: try.h: No such file or directory
mex: compile of ' "try.cpp"' failed.
??? Error using ==> mex at 208
Unable to complete successfully.

使用-I选项的另一种尝试:

>> mex  -I mexTryAlex.cpp try.cpp try.h
Warning: You are using gcc version "4.4.3-4ubuntu5)".  The version
     currently supported with MEX is "4.3.4".
     For a list of currently supported compilers see: 
     http://www.mathworks.com/support/compilers/current_release/

mexTryAlex.cpp:1:17: error: mex.h: No such file or directory
mexTryAlex.cpp:7: error: ‘mxArray’ has not been declared
mexTryAlex.cpp:7: error: ISO C++ forbids declaration of ‘mxArray’ with no type
mexTryAlex.cpp:7: error: expected ‘,’ or ‘...’ before ‘*’ token
mex: compile of ' "mexTryAlex.cpp"' failed.
??? Error using ==> mex at 208
Unable to complete successfully.

如何编译这些文件?

1 个答案:

答案 0 :(得分:1)

使用

修复了错误
#include "try.h"

而不是

 #include <try.h>  

在源文件中。