This shows you the differences between two versions of the page.
mgl:workingwithc [2020/07/20 23:48] |
mgl:workingwithc [2022/08/30 13:40] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Very brief guide to working with C ====== | ||
- | |||
- | The following section has been tested on: | ||
- | - macOS High Sierra (10.13.6), Matlab 2014b, XCode 10.1 | ||
- | - 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: | ||
- | - source files (.c): The main code goes here. References some headers and object files. The source file is compiled to give us our program. | ||
- | - header files (.h): Function declarations, preprocessor statements. | ||
- | - 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. | ||
- | - 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): <code> expr -l objective-c -- @import CoreGraphics </code> | ||
- | * 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 [[https://www.mathworks.com/matlabcentral/answers/299329-how-to-debug-mex-file-on-mac-platforms|here]] but I outline them here again. The debugging process works through XCode debugger, which can be programmed to be triggered by Matlab Executable. | ||
- | |||
- | - We want to debug a function whose source code is, for instance, mglPrivateSetGammaTable.c | ||
- | - Recompile source mex file with debugging symbols (-g flag)<code> mex -g mglPrivateSetGammaTable.c </code> | ||
- | - Configure a new Xcode workspace for debugging | ||
- | - Create a new XCode workspace | ||
- | - 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 | ||
- | - Add the file(s) to be debugged | ||
- | - Drag and drop the file mglPrivateSetGammaTable.c to workspace | ||
- | - Uncheck "Destination: copy items into destination group's folder (if needed)" | ||
- | - Create a scheme | ||
- | - Product > Scheme > New Scheme | ||
- | - target: none > Name: debug> OK | ||
- | - Run > Info > Executable option "other" > Command + Shift + G > path to Matlab executable (e.g. /Applications/MATLAB_R2016b.app/Contents/MacOS/MATLAB_maci64) | ||
- | - Select "Wait for executable to be launched" | ||
- | - Close | ||
- | - Set a symbolic breakpoint (this allows the debugger not to stop on communication signals between the apps) | ||
- | - Debug > Breakpoints > Create Symbolic Breakpoint | ||
- | - Symbol: NSApplicationMain | ||
- | - Add action > type in: <code> process handle -p true -n false -s false SIGSEGV SIGBUS </code> | ||
- | - Check Automatically continue after evaluating | ||
- | - (If the editor disappears, right click on new breakpoint > edit breakpoint) | ||
- | - Set breakpoints in the mex file | ||
- | - View > Navigators > show project navigator > .c file to debug > click gutter to set breakpoints | ||
- | - Start the Xcode debugger | ||
- | - Product > Run | ||
- | - This should set the Xcode message on top of the Xcode window to "Waiting for MATLAB to launch" | ||
- | - Launch a new instance of matlab from terminal | ||
- | - Start Matlab executable from terminal (go to the path for the Matlab you indicated on the scheme) then type ./matlab | ||
- | - This should set the Xcode message on top of the Xcode window to "Running MATLAB: debug" | ||
- | - Run the mex file from Matlab in the terminal you just opened |