Why can't I mex cpp with matlab?

2 ビュー (過去 30 日間)
Adam Wei
Adam Wei 2016 年 10 月 19 日
コメント済み: Quihao Zeng 2018 年 3 月 7 日
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!

採用された回答

Walter Roberson
Walter Roberson 2016 年 10 月 19 日
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 件のコメント
Adam Wei
Adam Wei 2016 年 10 月 19 日
Thank you for your answer! I did as you told and it worked! Thanks so much!
Quihao Zeng
Quihao Zeng 2018 年 3 月 7 日
Really thanks for you answer!

サインインしてコメントする。

その他の回答 (3 件)

Ken Atwell
Ken Atwell 2016 年 10 月 19 日
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 件のコメント
Adam Wei
Adam Wei 2016 年 10 月 19 日
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.

サインインしてコメントする。


Jan
Jan 2016 年 10 月 19 日
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 件のコメント
Adam Wei
Adam Wei 2016 年 10 月 19 日
Thank you for your detailed answer! I have fixed the problem!

サインインしてコメントする。


James Tursa
James Tursa 2016 年 10 月 19 日
編集済み: James Tursa 2016 年 10 月 19 日
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.

カテゴリ

Help Center および File ExchangeWrite C Functions Callable from MATLAB (MEX Files) についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by