Very brief guide to working with C

The following section has been tested on:

  1. macOS High Sierra (10.13.6), Matlab 2014b, XCode 10.1
  2. macOS Sierra (10.12.6), Matlab 2016b, Xcode 9.2

One might have to work with C to meddle with low-level codes for softwares, or for developing new programs. There are different kinds of files for C:

  1. source files (.c): The main code goes here. References some headers and object files. The source file is compiled to give us our program.
  2. header files (.h): Function declarations, preprocessor statements.
  3. object files (.o): Output of the compiler, but not executable by themselves, but its functions can be called by other programs without having to recompile the functions.
  4. binary executable (.exe, .mex, etc..): executable output program. Mex (matlab executable) format is used by c source codes compiled for use by Matlab functions.

Refs: http://courses.cms.caltech.edu/cs11/material/c/mike/misc/compiling_c.html

Compiling in C

To compile a source code into object or executive files, one uses a compiler (for instance, gcc or clang). Flags:

  • [-c foo.c] : compiles the foo.c source file
  • [-o foo.o] : specifies the output of the compilation
  • [-g] : allows debuggers to be run on the output files
  • [-frameworks framework] : adds the framework libraries to be compiled with the source code.

Debugging in C (lldb or gdb)

In order to debug files in C on MacOS use lldb. On other systems, you can use gdb (gdb deosn't work as well on macOS).

Basic commands:

  • To import frameworks into the debugger (for instance, CoreGraphics for CG functions):
     expr -l objective-c -- @import CoreGraphics 
  • step
  • p : evaluates an expression (same as expr)
  • parray : prints c arrays in a readable format

Debugging in C (Matlab mex files)

You can debug mex files that are called by matlab functions. The steps are outlined here but I outline them here again. The debugging process works through XCode debugger, which can be programmed to be triggered by Matlab Executable.

  1. We want to debug a function whose source code is, for instance, mglPrivateSetGammaTable.c
  2. Recompile source mex file with debugging symbols (-g flag)
     mex -g mglPrivateSetGammaTable.c 
  3. Configure a new Xcode workspace for debugging
    1. Create a new XCode workspace
      1. Xcode > File > New > Workspace > Save with a name (e.g. mgl_debug) in an appropriate location (e.g. /User/josh/proj/mgl/mgllib). You can press Command+shift+G to find the folder
    2. Add the file(s) to be debugged
      1. Drag and drop the file mglPrivateSetGammaTable.c to workspace
      2. Uncheck “Destination: copy items into destination group's folder (if needed)”
    3. Create a scheme
      1. Product > Scheme > New Scheme
      2. target: none > Name: debug> OK
      3. Run > Info > Executable option “other” > Command + Shift + G > path to Matlab executable (e.g. /Applications/MATLAB_R2016b.app/Contents/MacOS/MATLAB_maci64)
      4. Select “Wait for executable to be launched”
      5. Close
    4. Set a symbolic breakpoint (this allows the debugger not to stop on communication signals between the apps)
      1. Debug > Breakpoints > Create Symbolic Breakpoint
      2. Symbol: NSApplicationMain
      3. Add action > type in:
         process handle -p true -n false -s false SIGSEGV SIGBUS 
      4. Check Automatically continue after evaluating
      5. (If the editor disappears, right click on new breakpoint > edit breakpoint)
    5. Set breakpoints in the mex file
      1. View > Navigators > show project navigator > .c file to debug > click gutter to set breakpoints
  4. Start the Xcode debugger
    1. Product > Run
    2. This should set the Xcode message on top of the Xcode window to “Waiting for MATLAB to launch”
  5. Launch a new instance of matlab from terminal
    1. Start Matlab executable from terminal (go to the path for the Matlab you indicated on the scheme) then type ./matlab
    2. This should set the Xcode message on top of the Xcode window to “Running MATLAB: debug”
  6. Run the mex file from Matlab in the terminal you just opened