MEX File - Debugging LIBSVM in Visual Studio

I would like to debug a MEX file in Visual Studio (From MATLAB environment directly to C++ (Visual Studio 2012)). I've understood that this it is possible by adding the -g option to the make file. Attached you can find the makefile code I am using. What changes should be applied to make it work? Code:

# This Makefile is used under Linux

MATLABDIR ?= /usr/local/matlab
# for Mac
# MATLABDIR ?= /opt/local/matlab

CXX ?= g++
#CXX = g++-4.1
CFLAGS = -Wall -Wconversion -O3 -fPIC -I$(MATLABDIR)/extern/include -I..

MEX = $(MATLABDIR)/bin/mex
MEX_OPTION = CC\#$(CXX) CXX\#$(CXX) CFLAGS\#"$(CFLAGS)" CXXFLAGS\#"$(CFLAGS)"
# comment the following line if you use MATLAB on 32-bit computer
MEX_OPTION += -largeArrayDims
MEX_EXT = $(shell $(MATLABDIR)/bin/mexext)

OCTAVEDIR ?= /usr/include/octave
OCTAVE_MEX = env CC=$(CXX) mkoctfile
OCTAVE_MEX_OPTION = --mex
OCTAVE_MEX_EXT = mex
OCTAVE_CFLAGS = -Wall -O3 -fPIC -I$(OCTAVEDIR) -I..

all:    matlab

matlab: binary

octave:
    @make MEX="$(OCTAVE_MEX)" MEX_OPTION="$(OCTAVE_MEX_OPTION)" \
    MEX_EXT="$(OCTAVE_MEX_EXT)" CFLAGS="$(OCTAVE_CFLAGS)" \
    binary

binary: svmpredict.$(MEX_EXT) svmtrain.$(MEX_EXT) libsvmread.$(MEX_EXT) libsvmwrite.$(MEX_EXT)

svmpredict.$(MEX_EXT):     svmpredict.c ../svm.h ../svm.o svm_model_matlab.o
    $(MEX) $(MEX_OPTION) svmpredict.c ../svm.o svm_model_matlab.o

svmtrain.$(MEX_EXT):       svmtrain.c ../svm.h ../svm.o svm_model_matlab.o
    $(MEX) $(MEX_OPTION) svmtrain.c ../svm.o svm_model_matlab.o

libsvmread.$(MEX_EXT):  libsvmread.c
    $(MEX) $(MEX_OPTION) libsvmread.c

libsvmwrite.$(MEX_EXT): libsvmwrite.c
    $(MEX) $(MEX_OPTION) libsvmwrite.c

svm_model_matlab.o:     svm_model_matlab.c ../svm.h
    $(CXX) $(CFLAGS) -c svm_model_matlab.c

../svm.o: ../svm.cpp ../svm.h
    make -C .. svm.o

clean:
    rm -f *~ *.o *.mex* *.obj ../svm.o

Answers


In general, you run the mex command with the -g option, and depending on your OS, either attach to MATLAB (Visual Studio in Windows) or start the debugger with the matlab startup script (e.g. matlab -Dgdb in Linux). Use these detailed instructions for your own MEX files.

However, for LIBSVM, you are provided with make.m, which can be used to build in any supported OS, and Makefile for use in Linux only. The Makefile is used for building the MEX files outside of MATLAB (i.e. without using mex.m, but instead a shell script in $(MATLABDIR)/bin/mex). In Windows, you need to edit make.m and add the -g option to each mex call:

mex CFLAGS="\$CFLAGS -std=c99" -largeArrayDims -g libsvmread.c
mex CFLAGS="\$CFLAGS -std=c99" -largeArrayDims -g libsvmwrite.c
mex CFLAGS="\$CFLAGS -std=c99" -largeArrayDims -g svmtrain.c ../svm.cpp svm_model_matlab.c
mex CFLAGS="\$CFLAGS -std=c99" -largeArrayDims -g svmpredict.c ../svm.cpp svm_model_matlab.c

Then, in MATLAB, run make (the modified make.m). Start Visual Studio, set one or more break points, and from the Debug menu, "Attach to Process...". Now you can run the MEX functions (e.g. svmtrain) and it will stop in Visual Studio when it hits the break points.

Just for completeness, if you were using Linux and wanted to go the Makefile route, just change the following line to build debug versions of the code:

MEX_OPTION += -largeArrayDims -g

  • First you have to compile the MEX-file with debugging information enabled. For instance use mex -g file.cpp or add the equivalent to your build system.
  • Next attach Visual Studio the the MATLAB process, open the C/C++ source file and place a breakpoint
  • Finally run the MEX function from MATLAB. VS will stop at that point and enter debugging mode.

Here is a page explaining the procedure.


Need Your Help

How to use the form value in the action?

html forms grails

I am trying to make a form in html that uses the value you enter to form the destination URL.

XAML apps using C++/CX for Desktop Windows

c++ windows xaml windows-8 c++-cx

I have experimented with XAML UI programming using C++ for Windows 8 Store apps. I really like the developer experience, the XAML designer and the fact that I can get pure native apps with a moder...