Turn On Line Numbers in Eclipse [Luna]

Personally, I’m not sure what any code editor thinks it is doing by not having line numbers switched on by default.

But, each to their own, so if you want to see your line numbers in Eclipse and you just can’t find out where on earth to turn them on, aaargh! Don’t panic.

Just go to:

Window -> Preferences -> General -> Editors -> Text Editors

And check the handy “Show line numbers” box:


Then, take a deep breath, and feel much better than you can now see which line of code you are editing at any given time.

Simple ncurses Console Game

I wrote a simple ncurses game in C++ on Fedora.

I’ll actually be releasing the code and talking about how to build the game as part of a new Eclipse-C++ course I’m currently creating (sign up to my mailing list if you’d like updates on this).

It was pretty straightforward (once I finally got the ghost AI working properly – breadth first search anyone?). Ahem.

Anyway – here’s a little video I made of the game.

I cropped the screen area to get rid of distractions, which means the quality suffers a little bit, but you get the idea. And you’ve got to luuurve the cheesy music provided by iMovie…



Eclipse CDT + GDB: Setting Watchpoints (Juno)

I was trying to watch a variable in Eclipse today and just could not find how to set it up. No matter what I did the Toggle Watchpoint option remained greyed out.

A watchpoint is more fun than a regular breakpoint, because the debugger will stop whenever the variable is changed, even if your program is busy doing something else (like overrunning the end of an array).

So after a bit of research it turns out that you can set watchpoints, but the variable type dictates how.

Global variables

If your variable is global, you need to double-click the variable to highlight it (anywhere in your source) and then select Run > Toggle Watchpoint. Until your variable is selected, this option will always be greyed out.

You can also right click on it in the Outline View and then select Toggle Watchpoint from the context menu.

In both cases a watchpoint properties box pops up so you can edit the details. Just click OK and the debugger will stop every time the variable is changed.

If the variable is not listed in the Outline View then sorry – it’s not global and you can’t use the Eclipse GUI to set it – but you can do this:

Local variables

When you’re debugging with gdb via Eclipse, there is a sneaky gdb console view that you can use to talk directly to the gdb session.

Select the console tab at the bottom and on the right-hand side click the Display Selected Console button. This will reveal a drop down menu. Choose [C/C++ Application] gdb.

And now, in the console itself, just type your watchpoint as if you were on the gdb command line (in the screenshot below I want to be notified if a local variable called count goes above the value of 45):


gdb console watchpoint

Ta da! Eclipse will now stop whenever your local variable changes.

Creating Your First C++ Program in Eclipse

Have you seen my C++ Eclipse Masterclass
It’s a series of professional video and text tutorials on using C++ and Eclipse CDT.

This is a tutorial on creating and understanding a ‘Hello World’ program in C++ using Eclipse.

Why? Because in the majority of my other C++ tutorials I will be using Eclipse as the IDE (integrated development environment), and building on a basic project such as this.

I program on Fedora Linux, not Windows, so although your screenshots may differ slightly to mine, Eclipse runs quite happily on both.


There’s a wealth of information on the Eclipse website that tells you how to install Eclipse for your operating system, but the heads up if you’re a Fedora user is just switch to root (or use sudo) and run:

yum install eclipse-cdt

Create your project

Open eclipse (you’ll find it in the Applications > Programming menu), and if this is the first time you have used it, accept the default workspace and close the welcome window.

Then go to:

Window > Open Perspective > Other > C/C++

This opens the C++ ‘perspective’ in Eclipse, which is a fancy way of saying it makes available the windows and options you need to develop C++ programs.

Now create your first program with:

File > New > C++ Project

Name it Hello, and select the project type as Hello World C++ Project (Figure 1).

create project window
Figure 1: Create a Hello World C++ Project

Click next and accept all the defaults in the Basic Settings window. Click next and again accept all the defaults in the Select Configurations window. Click Finish.

Eclipse will create your project and project file and return you to the main C++ view. You should now be able to see your ‘Hello’ project on the left hand side of the screen, and the contents of the Hello.cpp file in the main window. If you expand the project folder on the left, and then expand the ‘src’ folder underneath it, you will see the location of your program file, Hello.cpp (Figure 2).

hello world project
Figure 2: Your Hello World Project

Your project in action

Now all you have to do is hit ‘Run’ (it’s that little green button that looks like a play symbol).

Note: Eclipse may be set to NOT build automatically, in which case you will get an error about the binary not being found. If this happens, select Build (the hammer icon) before trying to Run.

You should see “!!!Hello World!!!” in the console area at the bottom of the screen (Figure 3). Note that if you prefer a pop-up console window, you can right click and select the ‘Detached’ option on the console tab. Even if you close the window, it will reappear next time you run the program.

console output
Figure 3: Console output

That’s great, but what does it all mean?

Let’s just run through the contents of the Hello.cpp file, so you have an idea of what you have created.

The first 7 lines are just comments. They were created for you by Eclipse and you can edit these to your heart’s content. What’s included at the top of a cpp file varies widely between companies and also depends on personal preference, but bear in mind that the point of the comments is to tell the next person that comes along a little bit about what this file contains.

The first two lines of actual code are:

#include <iostream>
using namespace std;

Any line that starts with a # character is called a ‘preprocessor directive’. The preprocessor makes any requested modifications to your source code before passing it to the compiler. In this example, line 9 is telling the preprocessor to include something called ‘iostream’, which refers to a header file that allows our program to utilize the input/output methods in the iostream library.

I will talk a little more about the preprocessor in a future tuturial, but for now you can look at this line as a succinct way to include pre-existing code.

Line 10 makes use of the using directive to tell the compiler that we are using functions from the standard namespace.


OK, a namespace is basically a collection of classes and functions grouped together under an umbrella and given a name. In this case the name is std, and it covers all the things you might want to use from the C++ standard library. Namespaces help resolve the potential issue of classes and functions having the same name and thus causing redefinition errors when you try to compile your program.

In our example, the functions we are using from the standard library are cout and endl. If you didn’t have line 10 in your source file, you wouldn’t be able to use these, because they would not be in scope, or in other words, the compiler would not know what on earth you were referring to*.

Next we have:

int main() {
    cout << "!!!Hello World!!!" << endl;
    return 0;

This is the most important part of the file – it’s referred to as the main function and is where our program starts and ends. main is defined only once for any C++ (or C) program, and the name is always all lower-case.

It contains two statements inside a set of curly brackets. The first uses the function cout to print the ‘Hello World’ text to the screen (or standard output), and finishes with endl, to declare an end of line character (the equivalent of pressing the return key after typing in a sentence).

The second statement returns a zero when the program exits. Traditionally a zero return means that the program has executed without errors. It is required here because the main function return type is declared as int (short for integer), meaning a whole number of some value must be returned when the program exits.

Tell me what I’ve learnt

Well, that just about covers it. Of course, you can dig into the details of just these few lines of code and read a huge amount on the hows and whys of it all, but to summarize what this basic tutorial covers, you should have done the following:

1) Created a Hello World C++ program in Eclipse.
2) Run that program and seen the output in the console window.
3) Identified the different parts of the program: comments, preprocessor directive and code.
4) Understood the importance of the main function being our program’s entry and exit point.
5) Noted the use of the std namespace to allow us access to the functions cout and endl.
6) Appreciated the use of these functions to output a text message to the screen.

Phew! That’s enough for today. Look out for the next C++ tutorial, coming soon.

*An alternative to the using directive at line 10 is to qualify each std method with its namespace when it appears in the code, i.e.:

std::cout << "!!!Hello World!!!" << std::endl;

This is considered a better way of doing things, but typing std:: over and over can be a little laborious, hence the widespread use of the using directive on line 10. If you are after more detail, there is a good explanation of this at the C++ FAQ here.