Main Content

Debug C++ Library from MATLAB Interface

Suppose that you have a MATLAB® interface to a C++ library as described in Build MATLAB Interface to C++ Library. You can debug the code in the C++ library by building a debug version of the interface. Both your C++ compiled library and the MATLAB interface library must be built with debug symbols in order to debug the library in MATLAB.

Follow these steps, which include general guidance for debugging a program. This topic provides details for step 2 in the debugging process. Refer to your build environment documentation for additional details.

  1. Identify the types of files in your interface — supporting source files or compiled library files.

  2. Rebuild the interface using the information in either Rebuild Interface with Supporting Source Files or Rebuild Interface with Compiled Library Files. Modify the workflow script you created with the clibPublishInterfaceWorkflow function or call the clibgen.buildInterface function.

  3. Call addPath on your output folder.

  4. Configure your build environment. For more information, see Platform Debugging Tips.

  5. From your debugging program, set breakpoints in the C++ code. On Windows®, attach MATLAB to the debugger.

  6. In MATLAB, to call a function in interface libname, use the clib.libname namespace. The program stops at the breakpoints.

Rebuild Interface with Supporting Source Files

Debug Flags

If you built your interface with header and C++ source files, then rebuild the interface with these additional compiler and linker flags.

For MinGW®, Linux®, and macOS compilers:

  • Add compiler flag "-g".

  • Add linker flag "-g".

For Microsoft® Visual Studio® compilers:

  • Add compiler flag "/Z7".

  • Add linker flag "/debug".

Using Workflow Script

To rebuild with the debug flags using your workflow script, in the Step 1: GENERATE section, verify that Library type is set to Headers and source files, and then in the Specify optional C++ library settings section, set the Additional compiler flags and Additional linker flags parameters based on your compiler type.

In the Select configuration section, change the Name of interface library value to indicate a debug version of the interface, for example, to debug_version.

Then run the Step 3: BUILD section in your workflow script.

Using clibgen.buildInterface

To rebuild with the debug flags programmatically, use clibgen.buildInterface with the AdditionalCompilerFlags and AdditionalLinkerFlags name-value arguments. For example, programmatically build an interface named debug_version defined by header file h1.hpp and source file c1.cpp on Visual Studio.

clibgen.buildInterface("h1.hpp",SupportingSourceFiles="c1.cpp", ...
    AdditionalCompilerFlags="/Z7",AdditionalLinkerFlags="/debug", ...
    InterfaceName="debug_version")

Alternatively, build the same debug_version interface using any other compiler

clibgen.buildInterface("h1.hpp",SupportingSourceFiles="c1.cpp", ...
    AdditionalCompilerFlags="-g",AdditionalLinkerFlags="-g", ...
    InterfaceName="debug_version")

Rebuild Interface with Compiled Library Files

Debug Version of Compiled Library File

If you built your interface with compiled library files, then rebuild the interface with a debug version of the compiled library file. The library vendor might provide a debug version. If you have source files for a third-party library, then you can build the library from the source with debug flags. If you developed your own library, then build the library file outside MATLAB with debug flags.

Using Workflow Script

To rebuild with a debug version of the library file using your workflow script, in the Step 1: GENERATE section, verify that Library type is set to Headers and compiled library files, and then in the Select files section, change the Compiled library files value to the debug version of the library.

In the Select configuration section, change the Name of interface library value to indicate a debug version of the interface, for example, to debug_version.

Then run the Step 3: BUILD section in your workflow script.

Using clibgen.buildInterface

To rebuild with a debug version of the library file programmatically, in the clibgen.buildInterface call, change the Libraries name-value argument to the debug version of the file. For example, programmatically build an interface named debug_version defined by header file h1.hpp and the debug version of the library file lib1d.lib.

clibgen.buildInterface("h1.hpp",Libraries="lib1d.lib", ...
    InterfaceName="debug_version")

Platform Debugging Tips

After you build a debug version of the interface and determine where to set breakpoints in source files, use these articles for platform-specific debug information.

Related Topics