2

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

此代码从从 mexFunction 开始并在 MATLAB 中使用 mex 编译的 c++ 函数生成文本“hello”(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.

我怎样才能让这些文件编译?

4

1 回答 1

1

该错误已通过使用修复

#include "try.h"

代替

 #include <try.h>  

在源文件中。

于 2013-04-22T14:44:46.500 回答