How to run C++ program on Ubuntu Linux?


C++ language was always one of my favorite programming language. Although not so easy to master but C++ is still industry standard in application as well as game development. But why do you wanna program in C++ on Linux? Good question! Here's why? C++ is object oriented programming language and on Linux Ubuntu you can produce high quality application that are easy to maintain and port to other platform such as Windows or Mac. If you ask me, why chose C++ on Ubuntu Linux, my answer is simple: No IDE required, only your favorite text editor (could be GNU/Emacs) and Terminal as compiler (GNU/G++).  So in this article you will find out how to write simple C++ program in Ubuntu Linux and compile it via Terminal.

Author: Amar Tufo
27. December, 2016

Ok, so you decided to program with C++ on Ubuntu Linux? Well, you make a good choice because this article is going to show you how to write very simple Hello World program in text editor such as Gedit and compile it via Terminal using GNU/G++. Before we start writing our program I suggest you to update your Ubuntu system to make sure that everything work fine. You can update your program using the following command in Terminal:

sudo apt-get update

Once you update your Ubuntu, we can start writing our program. Now, one of the big advantage of Ubuntu Linux as programming platform is a fact that you don't need an C++ IDE (Integrated Development Environment) to program with C++. All you need is simple text editor such as Gedit or even GNU/Emacs which is very good text editor full of features. Emacs is linked with Terminal and therefore it allows you to compile your source code directly from it's editor and to see the results.


Figure 1: GNU/Emacs text editor on Ubuntu Linux

If you prefer Emacs text editor, than you can install it from Ubuntu Software simply by typing Emacs and the installation will start immediately. Now let's take a look on our HelloWorld program source code.

//Hello World program
//Author: Amar Tufo

#include <iostream>

using namespace std;

int main()
{

 cout <<"Hello World from Ubuntu!!!"<<endl;
 cout <<"See more at: www.amartufointeractive.org"<<endl;


 return 0;
}

Figure 2: HelloWorld source code 

This is the source code of our HelloWorld program. You don't need to be a experienced C++ developer to know the output of this very simple program which I wrote for this article, the output is visible by it self. My HelloWorld program is stored in a folder called 'HelloWorld'. It'a a folder which contains my main.cpp file or the source code of my program.


Figure 3: Folder HelloWorld which contains the source file main.cpp

Now, let's compile our program to see the final results. To do that we need to navigate in to our folder HelloWorld via terminal in order to compile our main.cpp file. The following image explain the entire process.


Figure 4: Compiling HelloWorld program using g++ via Terminal

Now, you see here some commands that might be unknown to you. I have mentioned that we need to navigate to our folder via Terminal and that's exactly what I did in this image. Using the command cd Desktop I have navigate to Desktop and list the folders using ls command which than show's my HelloWorld folder. Than I use cd HelloWorld to enter the folder and list the files using ls command. And than I found the main.cpp file which was ready for compilation. This image already shows you how to compile our program. 

g++ -o test main.cpp
./test

Figure 5: G++ compiling command 

Once you enter the command above in terminal, our program will be compiled and the results are as follow:


Figure 6: HelloWorld program results

This is final results of our HelloWorld example program in Ubuntu Linux. As you can see, very simple program which output two messages on the screen : 'Hello World from Ubuntu!!!' and 'See more at: www.amartufointeractive.org'. That's the working part of our program.  


Conclusion:

Using the same method you can link numerous C++ files and compile them using only Terminal and GNU/G++ compiler. You don't need IDE on Linux. But just in case you prefer using C++ IDE, than I highly recommend you Code Blocks, an awesome and powerful native GTK IDE for Ubuntu Linux and various other distribution. Programming is good but with Ubuntu Linux it's more fun and awesome than you expected. Because it's Ubuntu - Linux for human beings.

______________

Last change in this article made on 28. December, 2016
23:48 PM
Share:

1 comment:


  1. The article was absolutely fantastic! Lot of great information which can be helpful in some or the other way. Keep updating,looking forward for more contents.

    by cognex AWS Training in chennai

    ReplyDelete

My Twitter news

Popular Posts

Recent Posts

Unordered List

  • Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
  • Aliquam tincidunt mauris eu risus.
  • Vestibulum auctor dapibus neque.

Pages

Theme Support

Need our help to upload or customize this blogger template? Contact me with details about the theme customization you need.