I am no longer writing on this blog, go over to my new blog here. - Hrishikesh [1st Jan 2016]

A Personal Blog on Stuffs I Love.

Sunday, 29 March 2015

Getting Started With C++ in Linux Mint


Beginning to learn C++ in Linux environment is a great Idea. And if you are just starting with Linux you must have chosen Linux Mint as your distro to start with, Even if you haven't this tutorial will work in other distros too. But the truth is, C is much more popular than C++ in the open source world. But if you just want to "Learn C++"  You can do it in Windows OS too and most of the time it will do the same thing. The difference lies on the compilers as C++ is not platform specific.

What are some benefits of learning C++ on Linux over Windows? 

You will be familiar with the terminal, which is quite a powerful thing to have command over. Moreover, You have basically two options to do your code, the first is using some kind of IDE(Like geany) and the second option is using some kind of text editor and the terminal to compile and run your code. The second one is much fun because you directly access the compiler skipping the IDE thing.

How to get started with C++ on the Terminal?

Unfortunately mint by default does not contain the "g++" compiler which we'll be using to compile our C++ code. (It does include the gcc compiler to compile c programs) but commands for using them are quite identical. So, the first thing is to download and install the compiler. Pop up the terminal and put this command:
apt-get install g++  
After doing that, you'll be asked for a [Y/N], simply give in 'Y' and press Enter. After the installation process is completed you'll have g++ to compile your code.
Now it's your turn to hit the codes but there are some catches if you are coming from legacy compilers like the Turbo C++, about which we'll be talking in a while. So, just pop up any text editor of your choice, the following picture shows the two default text editors that come shipped with Linux mint. 

If you are a starter then I would recommend you to use the GUI one. Write your code on it and then save it where ever you want with a .cpp extension. 
For example, this is your code, and you saved it.

1
2
3
4
5
6
7
8
#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
 cout<<"Hello World!"
 return 0;
}
Now pop up the terminal and browse to the directory where you saved the cpp file using the cd command, alternatively you can use the file manager and pop up the terminal by right clicking on any free space in the folder, the terminal will automatically land you up in that location. 
Now all you have to do is compile and run your code,
Some of the things you have to keep in mind if you are moving from legacy compilers, you can do your own little research on why it is like that later.
  1. For the main() function, have some return type, void does not work very well.
  2. Just including <iostream.h> won't do your job, you have to use an additional line "using namespace std;"
  3. Infact if you include iostream.h you'll be showed an error of something like "doesn't exist in directory". For that, just use #include<iostream> i.e, drop the '.h' , but this is not the case for stdio.h including stdio.h requires the '.h', you can do a further research on it.  
 So, it's time to compile!

Compiling C++ code in Linux Terminal

Pop up term, as mentioned above in the particular directory, and put this command in the terminal: 
g++ filename.cpp -o executablefilename
If your file was named firstprog.cpp then it would look like,
g++ firstprog.cpp -o pokemon
*OR*
g++ firstprog.cpp -o goku
*OR*
g++ firstprog.cpp -o a
If your program is syntactically correct then there will be no errors and you'll get the terminal prompt back. It's almost done and you are left with running the program, just type ./executablefilename and you'll be seeing the output. That's all!

How to get started with C++ in an IDE?

An IDE is pretty straight forward and you should not have any problem with using an IDE if you are comfortable with the terminal. I would recommend the geany IDE, it's lightweight and fun.

Conclusion
Now that was all about Writing your first C++ program in Linux Mint, It's easy to learn things! Let me know if I can help you in any way.

0 comments:

Post a Comment