laurentkneip / opengv

OpenGV is a collection of computer vision methods for solving geometric vision problems. It is hosted and maintained by the Mobile Perception Lab of ShanghaiTech.
Other
1.02k stars 353 forks source link

Matlab MEX Error: "mxCreateNumericArray" Static Type Issues #53

Open danielRuckser opened 6 years ago

danielRuckser commented 6 years ago

Within MATLAB R2017b it is not possible to build the MEX files as explained. A quick successful workaround is at the end of this comment!

Used Hardware and Software: -> Mac Book Pro (Retina, 15inch, Mid 2014) -> macOS High Sierra, Version 10.13.1 -> Xcode Version 9.1 (9B55) -> MATLAB R2017b, Version 9.3.0.713579

I built the binaries with "cmake" as described in the reference.

Then the MATLAB MEX commands were as follows: 1) mex -setup C++ 2) cd /Applications/opengv/matlab 3) mex -I../include -I../third_party -I../third_party/eigen3 -L../build/lib -lopengv opengv.cpp

Output of MATLAB was:

Building with 'Xcode Clang++'. Error using mex /Applications/opengv/matlab/opengv.cpp:693:17: error: no matching function for call to 'mxCreateNumericArray_730' plhs[0] = mxCreateNumericArray(2, dims, mxDOUBLE_CLASS, mxREAL); ^~~~~~~~ /Applications/MATLAB_R2017b.app/extern/include/matrix.h:1131:30: note: expanded from macro 'mxCreateNumericArray'

define mxCreateNumericArray mxCreateNumericArray_730

                         ^~~~~~~~~~~~~~~~~~~~~~~~

/Applications/MATLAB_R2017b.app/extern/include/matrix.h:802:1: note: candidate function not viable: no known conversion from 'int [2]' to 'const size_t ' (aka 'const unsigned long ') for 2nd argument mxCreateNumericArray_730(size_t ndim, const size_t *dims, mxClassID classid, mxComplexity flag);

There were a lot of similar error messages like this one. Reason for this is the function "mxCreateNumericArray" that is called several times in "opengv.cpp" (that is located in opengv/matlab).

I assume an upward compatibility issue with more recent MATLAB versions like mine (R2017b). You can find the relevant MATLAB file in "MATLAB_R2017b.app/extern/include", it is named "matrix.h".

In this file the there are two relevant functions: mxCreateNumericArray_730(size_t ndim, const size_t *dims, mxClassID classid, mxComplexity flag) and mxCreateNumericArray_700(int ndim, const int *dims, mxClassID classid, mxComplexity flag).

Suitable pre-processor macros are also implemented in "matrix.h" to call "mxCreateNumericArray_730 " every time "mxCreateNumericArray" is called (without "_7xx" in the name). As easily seen these two functions have different input argument types. "int" & "const int" in the one case (which are signed types) and "size_t" & "const size_t" in the other case (which are unsigned types).

MATLAB is therefore unable to do a proper type conversion on its own. As a quick workaround in order to prevent you from changing many lines of code I recommend adding following line in the top of the file "opengv.cpp" (that is located in opengv/matlab): #define mxCreateNumericArray mxCreateNumericArray_700.

That will force MATLAB to use the (older) function "mxCreateNumericArray_700" with proper input argument types. After doing that everything seems to work fine.

RobbyPee commented 6 years ago

Thanks for this, it solved some build issues I was having as well.

I added the #define to the mex command:

mex -I../include -I../third_party -I../third_party/eigen3 -L../build/lib -lopengv opengv.cpp -DmxCreateNumericArray=mxCreateNumericArray_700

which saves having to modify any code (even if it's a minor modifcation)

cristinabustos16 commented 4 years ago

I solved this compilation problem using this flag "mex -DMX_COMPAT_32 ..."

zhaomoudou commented 3 years ago

I solved this compilation problem using this flag "mex -DMX_COMPAT_32 ..."

It works for me. But when I run edgesDemo.m, I got an error: cannot find 'imPad' in edgesDetect.

zhaomoudou commented 3 years ago

I solved this compilation problem using this flag "mex -DMX_COMPAT_32 ..."

It works for me. But when I run edgesDemo.m, I got an error: cannot find 'imPad' in edgesDetect.

solved