You need to compile the code you want to debug with debug symbols. GCC offers two options for this: -g and -ggdb. The latter adds debug information that is specific to GDB, whereas the former generates information in an appropriate format for whichever target operating system you are using, making it the more portable option. In our particular case, the target operating system is always Linux, and it makes little difference whether you use -g or -ggdb. Of more interest is the fact that both options allow you to specify the level of debug information, from 0 to 3:
- 0: This produces no debug information at all and is equivalent to omitting the -g or -ggdb switch
- 1: This produces minimal information, but which includes function names and external variables, which is enough to generate a backtrace
- 2: This is the default and includes information about local variables...