在 Ubuntu OS 中的 Matlab 2017a 上切换编译器

Switch compiler on Matlab 2017a in Ubuntu OS

本文关键字:编译器 2017a Matlab Ubuntu OS 中的      更新时间:2023-10-16

我正在运行一个需要cpp编译器的脚本。我在Windows和Ubuntu上使用MATLAB。在窗口上,具有:

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但我仍然得到相同的结果。

问题是你使用int而不是mwSize维度数组:

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

这些应该是:

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

MathWorks 的描述是:

mwSize 是表示大小值(如数组维度(的类型。使用此函数可实现跨平台灵活性。默认情况下,mwSize 等效于 C 中的 int。

使用 mex -largeArrayDims 开关时,mwSize 相当于 C 中的size_t。

所以问题是在一个平台上使用int*是合法的,而在另一些平台上可能是 size_t* .但是,使用总是正确的 mwSize* ,因为这是便携式解决方案。

作为旁注,我会这样写第一行:

mwSize const* dim; // image dimensinos

恕我直言,这种方式更易于阅读,链接到示例

正如乔纳斯在上面的答案中指出的那样,最好使用 mwSize。

在 64 位和 32 位系统之间切换时也可能导致相同的问题。 例如这个线程

在这种情况下,您还可以尝试使用 32 位兼容性标志编译 mex:

 mex -DMX_COMPAT_32 file.cpp

这个解决方案在我的情况下有效。