MATLAB and C / C ++. How to choose various C / C ++ specifications

I have the following problem with compiling a C ++ file from a third-party library in mex using the Visual C ++ 2010 compiler (cl.exe).

The compiler complains about several lines in the following expression:

plhs[i] = mxCreateNumericMatrix(nclass, 1, mxDOUBLE_CLASS, 0); 

with:

error C2664: 'mxCreateNumericMatrix_730': cannot convert parameter 4 from 'int' to 'MxComplexity'

the reason is that mxCreateNumericMatrix accepts an enum type as input argument 4 , called mxComplexity , which is defined as typedef enum mxComplexity {mxREAL=0, mxCOMPLEX}; . In other words, the compiler complains that it cannot implicitly convert from int to an enumeration type.

Interestingly, however, the library in question should be easy to compile without having to change anything in it.

So my question is: instead of adding an explicit cast to every line where this happens, is there a way to tell mex , cl.exe (or gcc if I did this on Unix) that I want to use an implicit C-style type conversion?

Note 1: Unfortunately, I do not know in which C ++ standard the library was written.

Note 2: In case this matters, this is the configuration that I have for mex (which is configured by default MATLAB, after running mex -setup ):

  CompilerExecutable: 'cl' CompilerFlags: '/c /Zp8 /GR /W3 /EHs /D_CRT_SECURE_NO_DEPRECATE /D_SCL_SECURE_NO_DEPRECATE /D_SECURE_SCL=0 /DMATLAB_MEX_FILE /nologo /MD' OptimizationFlags: '/O2 /Oy- /DNDEBUG' DebugFlags: '/Z7' LinkerExecutable: 'link' LinkerFlags: '/dll /export:%ENTRYPOINT% /LIBPATH:"%LIBLOC%" libmx.lib libmex.lib libmat.lib /MACHINE:X64 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /manifest /incremental:NO /implib:"%LIB_NAME%.x" /MAP:"%OUTDIR%%MEX_NAME%%MEX_EXT%.map"' LinkerOptimizationFlags: '' LinkerDebugFlags: '/debug /PDB:"%OUTDIR%%MEX_NAME%%MEX_EXT%.pdb"' 
+4
source share
2 answers

C ++ does not allow implicit conversion from int to enum values ​​(never! Not in C ++ 98, 2003, 0x), unlike C, so you need to use the actual enumeration values ​​for such a parameter.

Or you can compile as C, since the code they wrote is not C ++ (due to the above).

Note 1: Unfortunately, I do not know which C ++ standard for the library is written

It doesn't matter, C ++ standards are backward compatible, and this code definitely doesn't use anything new in C ++ 0x, so your compiler is fine. Changing the C ++ compilation mode will not allow you to perform the above conversion implicitly.

+2
source

First find out what language you are using. Are you using C, or are you using C ++?

This is one area where they are incompatible.

+1
source

Source: https://habr.com/ru/post/1337692/


All Articles