MATLAB Answers

Why can't I mex cpp with matlab?

5 views (last 30 days)
Adam Wei
Adam Wei on 19 Oct 2016
Commented: Quihao Zeng on 7 Mar 2018
I am running Matlab 2016a on Mac OS X El Capitan 10.11.6. And I have Xcode 6.4. When I input 'mex -setup' in Matlab, it goes well as followed
>> mex -setup
MEX configured to use 'Xcode with Clang' for C language compilation.
Warning: The MATLAB C and Fortran API has changed to support MATLAB
variables with more than 2^32-1 elements. In the near future
you will be required to update your code to utilize the
new API. You can find more information about this at:
http://www.mathworks.com/help/matlab/matlab_external/upgrading-mex-files-to-use-64-bit-api.html.
To choose a different language, select one from the following:
mex -setup C++
mex -setup FORTRAN
But when I mex a cpp on my matlab, I got an error message like this:
>> GCO_UnitTest
Building with 'Xcode Clang++'.
Error using mex
In file included from /Users/Downloads/decolor/gco-v3.0/matlab/gco_matlab.cpp:5:
In file included from
/Users/Downloads/decolor/gco-v3.0/matlab/../GCoptimization.h:109:
/Users/Downloads/decolor/gco-v3.0/matlab/../energy.h:262:2: warning: 'register'
storage class specifier is deprecated [-Wdeprecated-register]
register Value pi = (E000 + E011 + E101 + E110) - (E100 + E010 + E001 + E111);
^~~~~~~~~
/Users/Downloads/decolor/gco-v3.0/matlab/../energy.h:263:2: warning: 'register'
storage class specifier is deprecated [-Wdeprecated-register]
register Value delta;
^~~~~~~~~
/Users/Downloads/decolor/gco-v3.0/matlab/../energy.h:264:2: warning: 'register'
storage class specifier is deprecated [-Wdeprecated-register]
register Var u;
^~~~~~~~~
In file included from /Users/Downloads/decolor/gco-v3.0/matlab/gco_matlab.cpp:5:
In file included from
/Users/Downloads/decolor/gco-v3.0/matlab/../GCoptimization.h:111:
/Users/Downloads/decolor/gco-v3.0/matlab/../maxflow.cpp:366:16: warning: using the
result of an assignment as a condition without parentheses [-Wparentheses]
if (i->parent = a0_min)
~~~~~~~~~~^~~~~~~~
/Users/Downloads/decolor/gco-v3.0/matlab/../maxflow.cpp:366:16: note: place
parentheses around the assignment to silence this warning
if (i->parent = a0_min)
^
( )
/Users/Downloads/decolor/gco-v3.0/matlab/../maxflow.cpp:366:16: note: use '==' to
turn this assignment into an equality comparison
if (i->parent = a0_min)
^
==
/Users/Downloads/decolor/gco-v3.0/matlab/../maxflow.cpp:443:16: warning: using the
result of an assignment as a condition without parentheses [-Wparentheses]
if (i->parent = a0_min)
~~~~~~~~~~^~~~~~~~
/Users/Downloads/decolor/gco-v3.0/matlab/../maxflow.cpp:443:16: note: place
parentheses around the assignment to silence this warning
if (i->parent = a0_min)
^
( )
/Users/Downloads/decolor/gco-v3.0/matlab/../maxflow.cpp:443:16: note: use '==' to
turn this assignment into an equality comparison
if (i->parent = a0_min)
^
==
/Users/Downloads/decolor/gco-v3.0/matlab/gco_matlab.cpp:8:13: error: typedef
redefinition with different types ('int' vs 'size_t' (aka 'unsigned long'))
typedef int mwSize;
^
/Applications/MATLAB_R2016a.app/extern/include/tmwtypes.h:795:19: note: previous definition
is here
typedef size_t mwSize; /* unsigned pointer-width integer */
^
/Users/Downloads/decolor/gco-v3.0/matlab/gco_matlab.cpp:9:13: error: typedef
redefinition with different types ('int' vs 'size_t' (aka 'unsigned long'))
typedef int mwIndex;
^
/Applications/MATLAB_R2016a.app/extern/include/tmwtypes.h:796:19: note: previous definition
is here
typedef size_t mwIndex; /* unsigned pointer-width integer */
^
5 warnings and 2 errors generated.
Error in GCO_BuildLib (line 77)
eval(mexcmd); % compile and link in one step
Error in GCO_UnitTest (line 48)
GCO_BuildLib; disp('BuildLib PASSED');
I am confused on this problem for several days and have tried some solutions but no fixed. Could someone help me with this? Thanks!

Accepted Answer

Walter Roberson
Walter Roberson on 19 Oct 2016
The code you are compiling is outdated.
/Users/Downloads/decolor/gco-v3.0/matlab/gco_matlab.cpp lines 8 and 9 use
typedef int mwSize;
typedef int mwIndex;
There was a time when mwSize and mwIndex were "int", but that was a number of years ago, when only 32 bit systems were supported. They were changed to size_t . You should either remove those two lines or change the int to size_t
  2 Comments
Quihao Zeng
Quihao Zeng on 7 Mar 2018
Really thanks for you answer!

Sign in to comment.

More Answers (3)

Ken Atwell
Ken Atwell on 19 Oct 2016
MEX is working as expected.
How old is this code? Xcode's compiler is alerting you to several risky programming patterns being used. This code will need to be updated/corrected before Xcode will compile it.
  1 Comment
Adam Wei
Adam Wei on 19 Oct 2016
Thanks for your answer. It seems not the problem of the code, because I've tried many cpp files on my mac and showed the same error. But they could go well on other computers.

Sign in to comment.


Jan
Jan on 19 Oct 2016
The compiler shows useful and improtant error messages. They should be considered and fixed.
  1. Omit the "register" classifier, because it is deprected. If you really want to use it, disable the "-Wdeprecated-register" flag of the compiler.
  2. The assignment in "if (i->parent = a0_min)" is assumed to be a typo. If you do not mean "if (i->parent == a0_min)", insert the condition in additional round parentheses, as suggested in the message. By the way: It is worth to read the messages litterally.
  3. "typedef int mwSize" and "typedef size_t mwSize" is ugly, evil and confusing. Do not redefine fundamental types like "int". Do not do this. Don't.
Thank your XCode compiler for the detection of these inconsistent code and blame the other compilers, which accepted the code without warnings.
  1 Comment
Adam Wei
Adam Wei on 19 Oct 2016
Thank you for your detailed answer! I have fixed the problem!

Sign in to comment.


James Tursa
James Tursa on 19 Oct 2016
Edited: James Tursa on 19 Oct 2016
You should not redefine mwSize and mwIndex in your code ... you should only define them if they are not already defined. One way to tell if they are defined or not is to test for the existence of the MWSIZE_MAX macro. Earlier versions of MATLAB that don't have mwSize and mwIndex defined in the mex.h stuff will not have MWSIZE_MAX defined, but later versions of MATLAB will. So wrap your definition stuff in a conditional:
#ifndef MWSIZE_MAX
typedef int mwSize;
typedef int mwIndex;
typedef int mwSignedIndex;
#endif
I am using int in the above definitions instead of size_t because that is what matches the function signatures of the older versions of MATLAB for which the above is necessary.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!