简体   繁体   中英

Switch compiler on Matlab 2017a in Ubuntu OS

I am running a script that needs a cpp compiler. I'm using MATLAB on both Windows and Ubuntu. On windows, with:

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

i have no problem.

On Ubuntu, i have:

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

and when i try to compile me .cpp files i get this error:

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

I installed mingw-w64, by sudo apt-get install mingw-w64 but i still get the same result.

The problem is that you use int and not mwSize for you dimension arrays:

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

These should be:

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

The description from MathWorks is:

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.

So the issue is that on one platform it is legal to use int* and on others it could be size_t* . However, it always correct to use mwSize* , since this is the portable solution.

As a side note I would write the first line like this:

mwSize const* dim; // image dimensinos

This way is IMHO simpler to read, link to examples

As Jonas pointed out in the answer above, it is better to use mwSize.

The same problem might also be caused while shifting between 64 and 32 bit systems. eg this thread

In that case you can also try compiling mex with 32-bit compatibility flag :

 mex -DMX_COMPAT_32 file.cpp

This solution worked in my case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM