C++: Programming in Linux – Hello World

C++ is one of the most widely used programming languages today. I’m not going to talk about it in general but skip to the first example, which would be the Hello World program.
Before we start you should know that Ubuntu has an included C++ compiler called G++, so you won’t be needing a C++ IDE. I’ll be writing the programs here on GEdit (plain text editor) and compile it with G++. Also Ubuntu has an integrated debugger called GDB. Those two work on the command line, don’t be scared it isn’t complicated.

The first program, obviously, will be the notorious Hello World. So open GEdit and type in this:
Note: if you want you can use the Highlight Mode for C++ in GEdit. Go to View > Highlight Mode > Sources > C++

[CODE:]
—————————————————————————–
#include <iostream>

using namespace std;

int main () {

cout << "Hello World!\n";

return 0;
}
—————————————————————————–
[/CODE]

First I'll explain each line of the code and they jump to compiling it and running.
The code in the first line indicates that the iostream standard library. The standard C++ library is a collection of functions, constants, classes, objects and templates that extends the C++ language providing basic functionality to perform several tasks, like classes to interact with the operating system, data containers, manipulators to operate with them and algorithms commonly needed.
On to the third line (second was left blank). The keyword using is used to introduce a name from a namespace into the current declarative region, and namespaces allow to group entities like classes, objects and functions under a name, with std (standard) being the one used here. All the files in the C++ standard library declare all of its entities within the std namespace. With the semi-colon that particular line of code ends.
In the fifth line, the main is the main function in every C++ program. The execution of every program starts with the instructions which are in that function. The int indicates that an integer will return to main () (return 0; at the end).
In the seventh line there is the cout << command that’s the standard command from the iostream library for printing out the content in the quotes, in our case Hello World!. The \n indicates a new line, endl could have been used there too:
cout << “Hello World!”<< endl;
And the return 0; instruction returns a 0 to the main program, which is a message to the operating system that the program was successfully executed.
All that is left are the curved brackets {} in which every program’s instruction are written.

Compile and run your program

As said earlier, compiling is done with G++. After you save you program (I’ll use the name HelloWorld.cpp, it’s not important which name you use but use the .cpp extension) go to the terminal and type in this:

g++ -o Program HelloWorld.cpp

The -o option indicates the outfile, in this case Program.cpp, which is followed by the name of the file where the code is located.
After that run the program by typing: ./Program in the terminal.
You should have “Hello World!” printed out in the next line of the terminal. If not please comment here with your problem.
That’s it for now.

Advertisements

3 Responses to C++: Programming in Linux – Hello World

  1. Martin says:

    Thanks for the tutorial! It helped me a lot! Good luck with your blog!

  2. vedrank90 says:

    Hey, no problem! Stick around, you’ll find interesting stuff about c++ soon! Cheers!

  3. Thanks for this cool post. Anyway i found your blog on google and find it very useful. I’ll be sure to come back again for more!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: