MATLAB Answers

How to debug generated mex codes

130 ビュー (過去 30 日間)
PT
PT 2018 年 10 月 19 日
編集済み: PT 2018 年 10 月 29 日
I am trying to build an application in C++ using Visual Studio 2017. I have added paths in the directory as recommended https://www.youtube.com/watch?reload=9&v=vQ5aIxCX3To. Somehow I get an error - libmx.dll not found. I tried following - I copied this dll from bin\win64 and pasted it in the solution directory. After this I got error saying a different dll is missing. Then I tried to copy entire bin\win64 in a different location and added that path. Still the same error. Please help me resolve the issue.
When I generate mex from my code it works just fine, but I can't run the code in Visual Studio-17. Thanks

  0 件のコメント

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

採用された回答

Ryan Livingston
Ryan Livingston 2018 年 10 月 26 日
Moved from comment To debug generated MEX code, just pass the -g option to codegen and that will compile a debug MEX file. You can then debug that MEX code by following the same process you'd use for debugging hand-written MEX files:
In short, you compile and run the MEX in MATLAB and attach Visual Studio to MATLAB, stopping when the MEX code is executed.
Note that the -g option is the same as the Coder config setting EnableDebugging:

  0 件のコメント

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

その他の回答 (1 件)

Ryan Livingston
Ryan Livingston 2018 年 10 月 23 日
It sounds like you are generating MEX code. MEX code assumes that MATLAB is present and as a result it may have a large number of dependencies. For this reason, MEX code should be compiled in MATLAB with MATLAB Coder or the MEX command. Compiling it with Visual Studio outside of MATLAB is not recommended.
Instead you can choose one of the standalone code generation options to use inside of Visual Studio. You can do that with:
cfg = coder.config('lib'); % or 'dll'
codegen -config cfg myFunction.m -args ...
and then use that code in Visual Studio. More info on generating standalone code:
Here's an example of using the generated code in Visual Studio:
Finally, the Deployment category in the documentation may be of interest:

  3 件のコメント

PT
PT 2018 年 10 月 24 日
Hi Ryan,
Please let me know if my understanding is correct - it does not make sense to debug a C++ project in VS with mexFunction and all related dependencies included. Instead, a more appropriate process of generating mex from a custom C++ program is to prepare the C++ code using whatever compiler, then put the mexFunction and all dependencies and then generate mex. The recommendation is to not debug C++ codes within Visual Studio after mexFunction is in place.
I can already generate mex code and use it in Matlab - that is not an issue. My problem is - I need to debug my custom C++ code using Visual Studio. When I try to run my code within VS I get an error - libmx.dll not found which I am unable to fix. I hope this explanation is better than my original question.
-PT
Ryan Livingston
Ryan Livingston 2018 年 10 月 24 日
Got it. That helps. Just pass the -g option to codegen and that will compile a debug MEX file. You can then debug that MEX code by following the same process you'd use for debugging hand-written MEX files:
In short, you compile and run the MEX in MATLAB and attach Visual Studio to MATLAB, stopping when the MEX code is executed.
Note that the -g option is the same as the Coder config setting EnableDebugging:
If that helps, would you consider changing the title of this question to something like "How do I debug MATLAB Coder MEX files?" so that others could find it?
PT
PT 2018 年 10 月 26 日
The second link is very helpful, and answers my high level question. If you think its appropriate you could move your comment into a new answer, so that it can be accepted.
Thanks
PT

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

カテゴリ

製品


リリース

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by