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++
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 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.
So, it's time to compile!
- For the main() function, have some return type, void does not work very well.
- Just including <iostream.h> won't do your job, you have to use an additional line "using namespace std;"
- 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.
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
g++ firstprog.cpp -o pokemon *OR* g++ firstprog.cpp -o goku *OR* g++ firstprog.cpp -o a
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