There are plenty of ways with which you can do C++ programming under linux. You can use mdern IDE’s or use VI/Emacs to create C++ program files then can even use gcc on terminal to compile and execute C++ files.Let’s discuss the simple way to create programs under linux.
Before you start make sure you’ve:
Text editor: gedit,Kate,Kwrite any other that supports syntax highlighting will do the job.
GCC: You can download it through respositories or from gcc website.Use synaptic or adept to install GCC.You’re supposed to know how to use terminal as we’re using terminal to compile the source file.
Note: You can even use ajunta,kdevelop or eclipse C++ IDE.But it’s all about choice.If you’re newuser you can learn how to compile from terminal as bonus.This article will tell you how to do that.
Steps to create C++ source file:
1. Open any text editor or IDE.Create new source file.
2. Copy the source code below for hello world program.
#include <iostream>using namespace std;
cout << "Hello World \n";
3. Save the file with hw.cpp extention.
Now we’re done with the source file.Let’s see the code explaination before we compile it.
#include directive adds the contents of iostream header in the source file so that compiler allows us to use cout to print “hello world”.
using namespace std; means we’re using namespace std.
int main() is our main method,our programs looks first at this method,this method returns int.
cout is used for printing the text in between double quotes on the terminal.You can use any other text if you want in between the double quotes to print on screen.\n will take the cursor to the newline.
return 0 means it returns 0 to main method.At this point you don’t need to know about this.If you want you can place the system function to pause the screen.But again that is your choice.
Now that you’ve understood what is inside the program and what it is going to do,let’s compile this piece of code.Open the terminal.Go to the directory where you saved the .cpp file.The type following in terminal:
g++ hw.cpp -o hw
This command will tell the g++ to compile the hw.cpp and convert the file into executable with name “hw”.Now “Helloworld” will appear on your terminal.
There’s a lot more that can be done with the g++ and terminal.You can get user input,access files on disk etc.The above article was just short introduction of how to get into C++ programming.
I hope above information helps.If you’ve any correction or suggestion to improve this article please send feedback here.