2017-03-27 45 views
2

我正在運行需要cpp編譯器的腳本。我在Windows和Ubuntu上都使用MATLAB。在windows上,用:在Ubuntu OS的Matlab 2017a上編譯開關編譯器

MEX configured to use 'MinGW64 Compiler (C++)' for C++ language compilation. 

我沒問題。

在Ubuntu上,我有:

MEX configured to use 'g++' for C++ language compilation. 

,當我嘗試編譯我.cpp文件,我得到這個錯誤:

Error using mex 
/home/christosandsoren/17gr10409/deformable_models/texture_functions/build_km_tree.cpp: In function ‘void mexFunction(int, mxArray**, int, const mxArray**)’: 
/home/christosandsoren/17gr10409/deformable_models/texture_functions/build_km_tree.cpp:380:9: error: cannot convert ‘const size_t* {aka const long unsigned int*}’ to ‘const 
int*’ in assignment 
    dim = mxGetDimensions(prhs[0]); 
     ^
/home/christosandsoren/17gr10409/deformable_models/texture_functions/build_km_tree.cpp:402:68: error: cannot convert ‘int*’ to ‘const size_t* {aka const long unsigned 
int*}’ for argument ‘2’ to ‘mxArray* mxCreateNumericArray(size_t, const size_t*, mxClassID, mxComplexity)’ 
    plhs[0] = mxCreateNumericArray(2, dtree, mxDOUBLE_CLASS, mxREAL); 
                    ^


Error in compile_mex_functions (line 3) 
mex build_km_tree.cpp % based on Euclidean distance 

我安裝的MinGW-W64,由sudo apt-get install mingw-w64但我仍然得到相同的結果。

+0

看來你在Windows上使用32位(儘管MinGW64名稱),並在Linux上使用64位。由於'size_t'是32位或64位寬,具體取決於系統。 – Jonas

+1

您應該將'dtree'定義爲'size_t *'而不是'int *'。 – Jonas

+0

謝謝你的回覆, 我做了你建議的改變,我擺脫了最後一個錯誤。雖然: 錯誤使用mex build_km_tree.cpp:函數'void mexFunction(int,mxArray **,int,const mxArray **)': build_km_tree.cpp:380:34:錯誤:無法轉換'const size_t在assignment' * {又名const的長期無符號整型*}」到 'const int的*' 還是有的... 情況下你需要它: '無效mexFunction(INT nlhs,mxArray * plhs [], int nrhs,const mxArray * prhs [])' –

回答

3

的問題是,您使用int,而不是mwSize爲你維數組:

const int *dim; // image dimensinos 
int dtree[2]; // tree dimensions 

這些應該是:

const mwSize *dim; // image dimensinos 
mwSize dtree[2]; // tree dimensions 

從MathWorks公司的描述是:

mwSize is a type that represents size values, such as array dimensions. Use this function for cross-platform flexibility. By default, mwSize is equivalent to int in C.

When using the mex -largeArrayDims switch, mwSize is equivalent to size_t in C.

所以問題是在一個平臺上使用0123是合法的和其他人可能是size_t*。但是,總是正確使用mwSize*,因爲這是便攜式解決方案。

作爲一個方面說明我會寫第一行是這樣的:

mwSize const* dim; // image dimensinos 

這種方式是恕我直言,簡單閱讀,link to examples

3

約拿在上面的回答中指出,這是更好地使用mwSize。

在64位和32位系統之間切換時也可能導致同樣的問題。 eg this thread

在這種情況下,你也可以嘗試編譯MEX與32位兼容標誌:

mex -DMX_COMPAT_32 file.cpp 

該解決方案在我的情況下工作。