Debug a C + + mexfunction file in Visual Studio

Source: Internet
Author: User
Tags visual studio 2010

C + + Mex file: Also known as the Mex source file, is the normal fortran/c/c++ file, but it defines a mexfunction interface for communication with MATLAB, using the MEX () function can be compiled into a MATLAB Mex file. As for how to write the Mex source file, you can read the MATLAB help document!


The MATLAB Mex file: Also known as a Mex binary file, is a dynamic link library that can be called or run directly in MATLAB like a normal function, with the extension mexw32 or mexw64 under the Windows platform. The MEX source file can be compiled into MEXW32 using MEX () and the M file can be programmed mexw32 using the Coder Toolbox.


Mex () function: Matlab comes with a function to compile the Mex source file into a Mex binary file.
Mex function Library: In the Mex source code file called Matlab functions, mainly for some communication with MATLAB.
MX Matrix library: MATLAB provides some library functions for matrix operations such as C + +.


Many people know that using the MEX () function compiles a C/+ + source file into a binary Mex file that can be run directly in MATLAB. But without noticing, you can also debug a generated Mex file in Visual Studio!


Debugging on Windows platform
On the Visual Studio 2010 platform, you can debug C + + Source Mex file, the landlord is using Visual Studio 2010, illustrated Below
(1) Select Visual C + + 2010 as the default compiler and enter:>> on the MATLAB command line Mex-setup

Welcome to Mex-setup. This utility the set up a default compiler.

For a list of supported compilers, see

Http://www.mathworks.com/support/compilers/R2012a/win32.html

(2) direct return, MATLAB automatic search system compiler. Choose your compiler for building mex-files:
Would you like the MEX to locate installed compilers [y]/n?

Select a compiler:
[1] Intel visual Fortran 12.0 (with Microsoft Visual C + + linker) in C:\Program files\intel\composerxe-2011\

[2] Lcc-win32 C 2.4.1 in D:\PROGRAM\MATLAB\R2012A\SYS\LCC

[3] Microsoft Visual C + + in D:\Program\Visual Studio

[0] None

(3) The landlord system installs two compilers, of which Intel Visual Fortran is a FORTRAN compiler, Visual C + + 2010 is a C/s + + compiler. And Lcc-win32 is MATLAB comes with C compiler (can only compile C, not compile C + +), not the landlord installed.

Enter 3, and the carriage return indicates that Visual C + + 2010 is selected as the default compiler for C + +. Compiler:3

Please verify your choices:
Compiler:microsoft Visual C + + location:d:\program\visual Studio

(4) Enter directly to confirm and complete your configuration. is these correct [y]/n?
Warning:mex-files generated using Microsoft Visual C + + require that Microsoft Visual Studio Run-time libraries is available On the computer they is run on.

If you plan to redistribute your mex-files to other MATLAB users, being sure that they has the run-time Librarie S. ***************************************************************************

Trying to update options file:

C:\Users\Dynamic\AppData\Roaming\MathWorks\MATLAB\R2012a\mexopts.bat from
Template
D:\Program\MATLAB\R2012a\bin\win32\mexopts\msvc100opts.bat
Done ...
**************************************************************************
warning:the matlab C and Fortran API have changed to support MATLAB variables with more than 2^32-1 elements.            In the future??            You'll be required to update your code to utilize the new?? Api. You can find more information on:
Http://www.mathworks.com/help/techdoc/matlab_external/bsflnue-1.html?? Building with THE-LARGEARRAYDIMS option enables the new API.

**************************************************************************
(5) Use the-G switch to compile the yprime.c Mex source file for later debugging. Mex-g yprime.c at this time the current directory will generate a yprime.mexw32 file, the landlord's computer is 32-bit, if you are 64-bit operating system, that will generate a yprime.mexw64 file.
(6) Start visual Studio, and be careful not to exit MATLAB, or you will not find the relevant process.

(7) menu operation in Visual Studio, Tools→attach to Process ...

(8) in the Attach to Process dialog box, select the MATLAB process and click the "Attach (A)" button.

(9) At this time vs will automatically load all relevant data, menu operation, File→open→file ... Select the yprime.c file in the current working directory of MATLAB.

(10) You can set breakpoints in yprime.c, generally we like to give a breakpoint at the beginning of mexfunction.

(11) Running a Mex binary file in MATLAB, such as input: Yprime (1,1:4)

(12) The program will automatically run to the location where the breakpoint is set in VS, at which point we can do any debugging work in Visual Studio, such as checking the memory, looking at the address, reading the data, etc.

(13) Click menu operation, Debug→contiue, the program completed, MATLAB Command window will display the following:

>> Yprime (1,1:4)

Ans =

2.0000

8.9685

4.0000

-1.0947

Note : If you want to publish the generated Mexw32 file to a third party, use the MEX () function again to compile, and the Mexw32 file generated with the-G switch can only be used for debugging!

Debugging c/c++ mexfunction files in Visual studio

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.