Web Design,Programming,Mobile,iOS/Android,GameDev,IT Businness,eCommerce,Social Media


Thursday, 17 January 2013

Compiling and running C++ source codes in Ubuntu


Compiling and running C++
With its clear fonts and pixel percet ui Ubuntu makes it so comfortable for me to code in many programming languages. With a lot of free tools and support Ubuntu is an amazing work desk for every developer. Coding in C++ under Ubuntu is all about using the terminal. Thats it.

You just install Geany text editor if you dont like the default one and with some packages you will be all set up. So lets get started. As we said the first thing you will do is open the Terminal. Firstly we must make sure that we have all the necessary packages to start writing codes. Type this command in terminal

sudo apt-get build-essential

This will install the necessary libraries for C++ for you start writing and compiling code in this language. If you love to organize your projects you may want to create a folder in the Home directory that will hold all your C++ projects. I created a folder called Cplusplus inside Home directory. And here i will create different folders for my projects. For example for this tutorial i created a folder inside Cplusplus named Hello World.

Geany is perfect for writing C++ code. You can install it through Ubuntu Software Center or through terminal by typing this command

sudo apt-get install geany

Now open geany and paste this C++ code to test if you can compile and run your C++ programs.

#include <iostream>
using namespace std;

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

Save this file inside folder in Home->Cplusplus->Hello World folders we created earlier and name it main.cpp or whatever you like to name your source files. Now in Terminal you must change your directory to where your source files are and then compile them.

cd Home/Cplusplus/Hello World

With this command we changed the current directory to the folder Hello World where our main.cpp file is. Now we are ready to compile and see if everything works.

g++ main.cpp -o hello

This command will compile the file main.cpp with g++ compiler and the executable file will be named hello. If everything is compiled we can run our program by calling the name of the executable as we named in the above command. Type this in terminal after compiling your source files to run them


Compiling and running C++

And as you can see your program is successfully compiled and running. Happy coding thanks for reading.

Written by

Thank you for taking your time to browse through our blog. We try to build as much rich content base as we can. We focus in modern technologies, computer science design and business.


Post a Comment


© 2013 smartnoob . All rights resevered. Designed by Templateism