Simple Hello World Program In C++

Linux tutorials and code.
1 post Page 1 of 1
Contributors
User avatar
mikethedj4
VIP - Site Partner
VIP - Site Partner
Posts: 2592
Joined: Thu Mar 25, 2010 4:36 am

helloworld.png
First lets begin by opening a text editor. I'm running Ubuntu 10.04 so I'll be opening gedit, If you're running Kubuntu you should have kedit, and some other distributions have emacs. Don't use a word processor, we must use a text editor!!! Now put down the following source code in the text editor.
Code: Select all
#include <iostream>

using namespace std;

int main()
{cout << "Hello World!" << endl;
return 0;}
Were gonna save it as hello.cpp (.cpp as the extension tells the compiler will be using that it's a C++ program) on our desktop.

Now open up your terminal and navigate to the desktop by putting down the following terminal command.
Code: Select all
cd Desktop
This will change the directory to our desktop.

We will now be using the GNU Compiler Collection (GCC, or G++ for C++ programs) as our compiler for our program. G++ should automatically be installed on your distro if not put in the following terminal command to install the program.
Code: Select all
sudo apt-get install g++
We will also need glibc as well, which should be already installed, and again if it's not installed put in the following terminal command to install it.
Code: Select all
sudo apt-get install glibc
Now that we have our compiler we need to compile our program that way it'll run. (Assuming you're still in the desktop directory on your terminal) Type in the following code in your terminal.
Code: Select all
g++ -o helloworld hello.cpp
Now if there's any errors in the code the compiler will automatically detect the error, but in our case we don't so in order to run our program lets put down the following terminal command, and we will now see our program.
Code: Select all
./helloworld
helloworld.png
You do not have the required permissions to view the files attached to this post.
1 post Page 1 of 1
Return to “Programming”