2013-04-22 172 views
1

要编一套2个C++文件以及头文件,使用MEX。我展示了一个基本的例子和我得到的错误。错误尝试,我使用MATLAB <strong>Ubuntu的</strong>下并编译多个C++与MEX文件MATLAB

此代码生成文本 “你好” 从C++函数从mexFunction开始并使用在MEX MATLAB被编译,(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

只是一个普遍的建议:'使用命名空间标准;'不是最好的想法。只要使用'std :: cout',或者如果你使用'cout'并且想缩写它,使用'std :: cout;',但是打开整个std命名空间***会给你从长远来看令人头痛的问题:)只需在此处查看关于此主题的优点/缺点/讨论。 – 2013-04-22 15:04:28

回答

1

错误固定通过使用

#include "try.h" 

代替

#include <try.h> 
在源文件中