Running A C++ Program in Linux Terminal

Step 1. Open terminal.

Step 2. Type command to install gcc or g++ complier:

$ sudo apt-get install build-essential

This will install the necessary C/C++ development libraries for your Ubuntu to create C/C++ programs.

But most Linux distributions already come pre-installed with the gcc compiler

To check gcc version type this command:

$ gcc –version or gcc –v

Step 3. Now go to that folder where you will create C/C++ programs. I am creating my programs in Documents directory. Type these commands:

$ cd Documents/

$ sudo mkdir programs

$ cd programs/

Step 4. Open a file using any editor.

$ sudo gedit test.cpp

Step 5. Add this code in the file:

(i). C++ program code:

#include<iostream>

using namespace std;

int main()

{

            cout<<“\n\nHello World,\nWelcome to my blog, CodeWithBilly \n\n”<<endl;

            return 0;

}

Step 6. Save the file and exit.

Step 7. Compile the program using any of the following command:

(ii). Compiling C++ program.

$ sudo g++ test.cpp (or)

$ sudo g++ -o test test.cpp

[Note: Make sure you are in the same directory where you have created your program before compiling it.]

Step 8. To run this program type this command:

(ii). For running C++ program

$ ./a.out (If you compiled using first command)

Or

$ ./hello (If you compiled using second command)

It will show output on the terminal.

Try this. All the best!!!

Design a site like this with WordPress.com
Get started