Eclipse - Debugger - An easy example

Using the debugger is just essantial in a huge complex project.

But even for a tiny program, it is also useful.

For this Eclipse debugguer tutorial we are going to test it with the C++ programming language.

Of course you know how to install Eclipse on Windows, if not, follow this Eclipse installation tutorial for Windows.
If you prefer GNU/Linux or MAC it is a bit much easy because all libraries are already installed on your OS.

So let's get started.

To debug a project we have to already have a project.

So let's create it.

Creating the project to debug

Create a new project test-1 with a simple main.cpp file.


#include <iostream>

int main()
    int myAge;

    std::cout << "Hello, my age is " << myAge << std::endl;
    return 0;

Let's build the project.
For that right click on the project name and select Build project.

A new folder appears named Debug and inside a binary test-1.exe.

As you notice, because you are a C++ expert, this code isn't correct.
Indeed, the myAge variable isn't initialized.

So the result is unknown and this is not what we want.
But let's assume that you don't see where is the error.

To know what's going on we are using the debugger.

Using the Eclipse debugger

Our program test-1.exe can now be debugged.

Right click on the project name and select: Debug As > Local C/C++ Application.
A new window appears asking if you want to switch to the debug perspective, click yes if you don't know what is this.

You are now in the debug perspective with new icons to press and different splitted tabs.

If you want to switch to the classic C/C++ perspective, at the top right of Eclipse, there is two buttons: C/C++ and Debug with an icon for each.
Just click it.

So let's continue!

On your main.cpp file, at line:

std::cout << "Hello, my age is " << a << std::endl;

double left click on the left of this line, just before the line number.

A new blue icon appears.
It's a breakpoint.

Relaunch your application with the debugger.
Click on the green triangle within the Debug tab.

On the right, the Variable tab is displaying the myAge variable with its type and its value!

In my case, I have for this value something like that: 14135619.
And this is a really weird age.

So we know now why the program was bugged!

A great help to understand what's going on with your program. laugh

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.