Thursday, February 21, 2013

C++ Compiler (Code and Compile) for Linux mint/ Ubuntu Part 2

Unlike Windows the compiling in Linux is a bit tricky. Well in windows we just simply use some software like CodeBlock, Borland C etc. where we simply write the code on the panel and click on Compile and then Click on Run, "BAM" your code is done...

But my if you want to do the same thing in Linux, you are in problem my boy.
But don't worry, I'll tell you the easiest way to do it.


In my previous post HERE I described how to install C++ in Linux Mint and Ubuntu. Now I'm going to describe how to use it, compile and run a program.

Probably the best way to start learning a programming language is by writing a program. Therefore, here is our first program: 

1
2
3
4
5
6
7
8
9
10
// my first program in C++

#include <iostream>
using namespace std;

int main ()
{
  cout << "Hello World!";
  return 0;
}
Hello World!


Steps:

  1. Open a note pad and save it as "a.cpp" .
  2. Write the code as shown.
  3. Save the file.
  4. Go to Start Menu and find Terminal (Just tile Command Prompt in Windows)
  5. Go to the folder you saved your cpp file. In my case lets guess a folder called A in desktop.
  6. To go type "cd Desktop/A" with out quet. and enter.
  7. We can compile the file by typing gcc -o outputfilename filename.cpp . Example gcc -o a a.cpp Press enter.
  8. Now to run the code type, ./filename (Example: ./a)
Done...

You learned C++ in Linux Mint or Ubuntu.

To learn more and qury visit: http://www.cplusplus.com It is the best I've ever seen.


No comments:

Post a Comment