1. Not needed, although you may want to consider always building with -g (sometimes, you may even need to try and debug optimized (-O1, -O2, etc) code; why not leave -g on? For releases, you can always just run strip on the binary.

  2. Yes. Build just that file with -g .

Answer from Mikeage on Stack Overflow
๐ŸŒ
Intro COS Lab
introlab.cs.princeton.edu โ€บ guide-and-best-practices-for-gdb
Guide and Best Practices for GDB | Intro COS Lab
February 1, 2023 - Run gud-gdb (like this): gdb โ€”fullname followed by the name of an executable. If the executable is not your desired program (in this case testprgm), delete it and type the name of your program.
๐ŸŒ
Markaicode
markaicode.com โ€บ home โ€บ gdb debugging | complete step-by-step guide for beginners
GDB Debugging | Complete Step-by-Step Guide for Beginners | Markaicode
November 4, 2024 - Learn GDB debugging with practical examples, commands, and best practices. Perfect for beginners and intermediate developers seeking to master software debuggin
๐ŸŒ
Interrupt
interrupt.memfault.com โ€บ blog โ€บ advanced-gdb
Advanced GDB Usage | Interrupt
October 20, 2020 - In this reference-style post, we discuss some of the more advanced and powerful commands of the GNU debugger, GDB, as well as cover some of the best practices and hacks Iโ€™ve found over the years that help make GDB more pleasant to use.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ gnu_debugger โ€บ gdb_quick_guide.htm
GDB - Quick Guide
GDB allows you to run the program up to a certain point, then stop and print out the values of certain variables at that point, or step through the program one line at a time and print out the values of each variable after executing each line.
๐ŸŒ
TechBeamers
techbeamers.com โ€บ how-to-use-gdb-top-debugging-tips
GDB Tutorial: Essential GDB Tips to Learn Debugging
February 4, 2025 - In this tutorial, youโ€™ll learn how to use GDB to find and fix issues in your code, along with helpful tips and tricks.
๐ŸŒ
GNU Project
sourceware.org โ€บ gdb โ€บ current โ€บ onlinedocs โ€บ gdb.html
Top (Debugging with GDB)
This is the Tenth Edition, of Debugging with GDB: the GNU Source-Level Debugger for GDB (GDB) Version 18.0.50.20260401-git.
Find elsewhere
๐ŸŒ
LinkedIn
linkedin.com โ€บ all โ€บ engineering โ€บ operating systems
What are the best practices for using GDB in OS development?
April 12, 2024 - Employ step-by-step execution to trace code flow and breakpoints for targeted analysis. Utilize print and display commands for variable inspection, info registers for CPU register examination, and expressions with p for dynamic evaluation.
๐ŸŒ
GNU Project
sourceware.org โ€บ gdb โ€บ current โ€บ onlinedocs โ€บ gdb
Debugging with GDB
Debugging Pascal programs which use sets, subranges, file variables, or nested functions does not currently work. GDB does not support entering expressions, printing values, or similar features using Pascal syntax.
๐ŸŒ
Stanford University
web.stanford.edu โ€บ class โ€บ cs107 โ€บ resources โ€บ gdb
CS107 GDB and Debugging
If you wanted to step through the code inside the loop just the last time the loop executed, with a normal loop you may have to skip over many program breaks before you get to the part you want to examine. However, gdb lets you add an optional condition (in C code syntax) for when the breakpoint should be stopped at:
๐ŸŒ
Red Hat
developers.redhat.com โ€บ articles โ€บ the-gdb-developers-gnu-debugger-tutorial-part-1-getting-started-with-the-debugger
The GDB developer's GNU Debugger tutorial, Part 1: Getting started with the debugger | Red Hat Developer
February 27, 2024 - The one specific option worth adding ... other symbol in your program. In short, for the best debugging experience, use -g3 -O0 when compiling your code....
๐ŸŒ
Johnnysswlab
johnnysswlab.com โ€บ home โ€บ gdb: a quick guide to make your debugging easier
GDB: A quick guide to make your debugging easier - Johnny's Software Lab
March 19, 2022 - I am sure that by now you are familiar with the most important command that GDB offers: c (continue), n (next), b (break), s (step), bt (backtrace) and fr (frame). In this article we will talk about commands and features of GDB that are less ...
Top answer
1 of 3
16

Maybe not the sort of "tip" you were looking for, but I have to say that my experience after a few years of moving from C++ & STL to C++ & boost & STL is that I now spend a lot less time in GDB than I used to. I put this down to a number of things:

  • boost smart pointers (particularly "shared pointer", and the pointer containers when performance is needed). I can't remember the last time I had to write an explicit delete (delete is the "goto" of C++ IMHO). There goes a lot of GDB time tracking down invalid and leaking pointers.
  • boost is full of proven code for things you'd probably hack together an inferior version of otherwise. e.g boost::bimap is great for the common pattern of LRU caching logic. There goes another heap of GDB time.
  • Adopting unittesting. boost::test's AUTO macros mean it's an absolute doddle to set up test cases (easier than CppUnit). This catches lots of stuff long before it gets built into anything you'd have to attach a debugger to.
  • Related to that, tools like boost::bind make it easier to design-for-test. e.g algorithms can be more generic and less tied up with the types they operate on; this makes plugging them into test shims/proxies/mock objects etc easier (that and the fact that exposure to boost's template-tasticness will encourage you to "dare to template" things you'd never have considered before, yielding similar testing benefits).
  • boost::array. "C array" performance, with range checking in debug builds.
  • boost is full of great code you can't help but learn from
2 of 3
5

You might look at:

Inspecting standard container (std::map) contents with gdb

๐ŸŒ
Ucdavis
heather.cs.ucdavis.edu โ€บ ~matloff โ€บ UnixAndC โ€บ CLanguage โ€บ Debug.html
Guide to Faster, Less Frustrating Debugging
A typical usage of gdb runs as follows: After starting up gdb, we set breakpoints, which are places in the code where we wish execution to pause. Each time gdb encounters a breakpoint, it suspends execution of the program at that point, giving us a chance to check the values of various variables.
๐ŸŒ
Kauffman77
kauffman77.github.io โ€บ tutorials โ€บ gdb.html
Quick Guide to gdb: The GNU Debugger
April 4, 2025 - TUI Mode contrasts to the Normal Mode of gdb which does not show any source code unless the list command is issued.
๐ŸŒ
Markaicode
markaicode.com โ€บ home โ€บ security โ€บ gdb security best practices: blocking remote exploits in multi-target debugging
GDB Security Best Practices: Blocking Remote Exploits in Multi-Target Debugging | Markaicode
April 26, 2025 - Securing GDB for multi-target debugging requires a layered approach focused on authentication, encryption, and access controls. By implementing these security practices, you protect your development environment from remote exploits while maintaining debugging capabilities.
๐ŸŒ
Beej
beej.us โ€บ guide โ€บ bggdb
Beej's Quick Guide to GDB
June 14, 2009 - This is a very quick-and-dirty guide meant to get you started with the GNU Debugger, gdb, from the command line in a terminal.
๐ŸŒ
University of Michigan
web.eecs.umich.edu โ€บ ~sugih โ€บ pointers โ€บ summary.html
GDB Tutorial
To prepare your program for debugging with gdb, you must compile it with the -g flag.
๐ŸŒ
Xlog
zein-176.xlog.app โ€บ gdb-zui-jia-shi-jian
gdb Best Practices Manual - Zein
December 3, 2024 - gdb ./a.out # Debug an unrun program from the beginning gdb a <pid> # Attach to a running process gdb -p <pid> # Same as above gdb -q a (gdb) attach <pid> # You can also start first and then attach (gdb) detach # If debugging through gdb attach, after detach, the original process will continue to execute (gdb) q # Exit gdb ----------------Silent startup option: directly enter gdb without printing a bunch of version copyright information gdb -silent gdb -q # You can set an alias for gdb in ~/.bashrc: alias gdb="gdb -q" ----------------Silent exit setting: gdb will prompt when exiting: whether to exit the debugged program #A debugging session is active.