Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm a Visual Studio user and am used to breakpoints for debugging. I'm now working in a linux environment and am using Eclipse as an IDE. I'm a newbie in linux and eclipse. I don't have any idea how to use gdb in eclipse. I tried using gdb in command line, but is not as easy as having a UI.

How do I use gdb in eclipse?

The following instructions are for Eclipse 3.5 (Galileo). For 3.6 (Helios), they are similar except for the link in step 2.

  • Go to Help > Install New Software.
  • Add the CDT repository http://download.eclipse.org/tools/cdt/releases/galileo to the list of repositories.
  • Select the CDT Repository. Now you need to install the CDT plugin along with GDB support from the list of available plugins (Select the CDT Main Features as well as CDT GNU Toolchain Debug support).
  • You should now be able to set breakpoints and inspect values of variables in Eclipse.

    An alternative is to install DDD (an GUI frontend for GDB).

    First download Eclipse CDT ensure that you can import a project into Eclipse as shown at: How to create a project from existing source in Eclipse and then find it?

    You could try to test things out with this simple test directory: https://github.com/cirosantilli/ide-test-projects/tree/e93924d4e2ce8cd64b00a7ce67d10d62b497fda1/cpp

    git clone https://github.com/cirosantilli/ide-test-projects
    cd ide-test-projects/cpp
    ./main.out
    

    Now you will also want to tell Eclipse how to find standard library symbols as explained at: "Unresolved inclusion" error with Eclipse CDT for C standard library headers Their defaults are terrible and just don't work.

    Once Eclipse imported the project, and e.g. you seem to be able to jump to definitions, etc., let's setup a GDB step debug.

    First you have to go under:

  • Run Configurations
  • C/C++ Application
  • cpp Default ("cpp" is the project name)
  • C/C++ Application
  • and set it to:

    main.out
    

    Now eclipse knows how to run your program. We can confirm that by doing a test run:

  • Run (Ctrl + F11)
  • and the terminal on the bottom show the output of the program:

    Finally, we can put a breakpoint on any point, e.g. main by double clicking on the sidebar to the left of the code, which creates a blue circle (shown on image above).

    Now we can debug via:

  • Debug (F11)
  • and as expected we are left at main:

    The light blue line over (void)argv; indicates that this is the current line being executed under the debugger.

    From there on it is just a matter of learning the debugging interface, e.g.:

  • shortcuts such as F6 to step over, as now visible from under "Run" (only visible once you start debugging)
  • viewing variable values under "Variables" to the right
  • stopping runs with Run > Terminate (Ctrl + F2)
  • pass arguments to the program: Eclipse command line arguments
  • You can then switch back to the normal code view (non-debug) with Ctrl + F8 once you are done debugging: How to change back the perspective after terminating the debugged process in Eclipse?

    Tested on Eclipse 2020-03 (4.15.0).

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.