How go compile and run generated C++ code in Microsoft Visual Studio 2013

7 views (last 30 days)
What I want to do is simple.
1. Generated C++ code using Matlab Coder.
2. Create new project in Microsoft Visual Studio 2013.
3. Add all the generated files into new project
4. Add the Matlab setting as following:
  • Add Include path: C:\Program Files\MATLAB\R2017b\extern\include
  • Add library path:C:\Program Files\MATLAB\R2017b\extern\lib\win64\microsoft
  • Add library:libmx.lib libmat.lib libeng.lib (also tried mclmcrrt.lib)
When compiling, give me the following error:
Error 15 error LNK2019: unresolved external symbol _mexAtExit referenced in function _mexFunction C:\Work\VP_Touch\E2E\Matlab\matlab_214_LC_updated\itouch-e2e-master\codegen\lib\afe_entry\Matlab_Example\Matlab_Example\_coder_afe_entry_mex.obj Matlab_Example
Error 10 error LNK2019: unresolved external symbol _emlrtSetField referenced in function _emlrtMexFcnProperties C:\Work\VP_Touch\E2E\Matlab\matlab_214_LC_updated\itouch-e2e-master\codegen\lib\afe_entry\Matlab_Example\Matlab_Example\_coder_afe_entry_info.obj Matlab_Example
...
How to address it? I googled it and cannot find the answer. Really appreciate it!

Answers (1)

Denis Gurchenkov
Denis Gurchenkov on 21 Feb 2018
Hi Lukai, note that you have generated code for a MEX function (aka config:mex) as opposed to standalone (aka config:lib). I am guessing that the reason why you are compiling with Visual Studio is so that you can use Visual Studio debugger to step through your mex function in the debugger.
If that is the case, then you don't need to use Visual Studio. Instead, simply generate the mex function with debugging information by passing the "-g" flag to codegen command, and then attach Visual Studio debugger to MATLAB, open the source code of the generated MEX function and then place a breakpoint. Once you execute the mex function, your breakpoint should hit.
See this page:
https://www.mathworks.com/matlabcentral/answers/91908-how-do-i-debug-a-matlab-c-c-mex-file-in-microsoft-visual-studio-2005
but don't do the "mex -g" step, instead do "codegen ..... -g..." to produce a mex function.
All the above is related to getting mex function debugging to work.
If you want to build generated C code as a mex function for some other reason, the way I'd go about that is to use the "-v" option to mex (you'd need to add it to mex options in the autogenerated .mk file in codege/mex/yourpoject/) and then seeing what parameters does the mex command pass to the C compiler. Then you can replicate the same parameters (-I, -D etc) in your Visual Studio project.
All that being said, if your intent is different from just being able to debug your mex function, then please add a comment describing the use case, we'll go from there.
Thanks,
Denis
  2 Comments
Lukai Cai
Lukai Cai on 23 Feb 2018
Hi, Denis, Thanks! I finally figure out. I copy the setting in the generated makefile to MSVC. and it works.
The reason I need to do it is that I need to link it into another MSVC project, and can debug overthere.
Marlon Capuno
Marlon Capuno on 1 Aug 2018
Hi Lukai,
Would you please make a guide on this process? I'm also trying to do the same thing.
Thank you so much,
Marlon

Sign in to comment.

Categories

Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!