c++: GDB & G++ compiler

G++

Best thing to do for g++ is consult the manual $man g++

Flags  
-Wall will print out all warnings
-g enables debugging
-ftime-report Nice little report at the end - very useful when testing the time and space results of the GCC passes and phases
-fmem-report Makes the compiler print some statistics about permanent memory allocation when it finishes.
   
-time Report the CPU time taken by each subprocess in the compilation sequence. For C source files, this is the compiler proper and assembler (plus the linker if linking is done).
-l lets the compiler know what other libraries to use
-I guides the compiler to the folder where it can find the include files
-o sets the output of the process
-O 0 Turns off the optimisation - useful when debugging
-E do the Pre-Processing operation - this will just output it to the terminal if you want to catch the output you either do a > in the terminal or set a -o flag.
-S takes the Pre-compiled file and transforms into Assembly - many times I find it quite interesting to read what the assembly code is doing in specific instances. It’s worth noting that this can also be read by using the gdb.
-S -masm=intel outputs the code in intel syntax
-c takes whatever you feed to it - pre-compiles and transforms it into assembly if needed. Then puts the Assembler to work into transforming the Assembly code into binary the default extension is .o and the files are called objects. This is what the machine actually reads. The
` $g++ | just worth mentinoning that when you call g++` the linker will link every file that is in its input.  
-v Verbose mode. Print out GNU CPP ‘s version number at the beginning of execution, and report the final form of the include path.
-H Print the name of each header file used, in addition to other normal activities. Each name is indented to show how deep in the #include stack it is. Precompiled header files are also printed, even if they are found to be invalid; an invalid precompiled header file is printed with …x and a valid one with …! .
Libraries these libraries can be found at the path: /usr/include/GL/ this is the default path that G++ will use in finding the libraries that are called with -l
-lGL OpenGL library
-lGLEW GLEW library
-lSDL2 LSDL 2 library

It is worth saying that the terminal reads g++ from /usr/bin and this can be tested with running which g++.

GDB

I have been coding quite a bit lately for #leetcode30daychallenge and I choose mostly to do it in C++.

I truly enjoy C++ and am enjoying both refreshing and learning new concepts about it. Let’s jump in

GDB oftenly used commands

Name Command What it does
TUI (Text User Interface) ctrl+x+a it opens the Text User Interface of the GDB. the same thing as layot next or layout src.
layout next layout next Iterates to the next view layout
List l or list - prints out the source code centred around the line we are currently at
list + number line l 13 will print out the code centred around line 13
Next n or next executes the next line of code
step s or step steps into the next line of code (for functions)
print + variable name p or print a prints the content of a variable
  p a = 2 by using p and specifying the value of a variable, you can set the variable to a specific amount from GDB
set var set var x = 2 Sets a specific variable to a specific value
info locals info locals Prints out all the local variables
breakpoint b or break it’s good practice to set the file when using this, just to avoid confusion with multi file projects. for example b test.cpp:13
disable break points disable disables all the breakpoints
continue c or continue Continues to the next breakpoint or to the end of the file
registers info reg Displays the registers

Breaks

You can add conditional breaks like b if (i==4) and the program will break when i==4.

-x flag

You can write a separate file containing all the commands to be executed within gdb. This can be loaded with the -x flag.

# a.db
b  3
b 24

define sumA
    print $arg1 + $arg2 + $arg3
end

This will put a break point at location 3 and 24 and define the function sumA that returns the sum of 3 arguments. In gdb the sum can be called

(gdb) sumA 1 2 3

Some useful resources

Everything you need to know about gcc

Question regarding -x flag

Nifty resource