Sunday 23 February 2014

Compiling c++ program using linux terminal

C++ is the most widely taught programming language to students .Getting your c++ code to work desirably requires it to be compiled without any errors. Windows users who have shifted to Linux are habitual to GUIs compiler like geany,code-blocks,visual c++ etc and have no idea where to compile their c++ code. Our beloved Terminal comes to the rescue of these linux newbies. Its as simple as writing two lines of commands. First you need to install the compiler called g++. Fire up the terminal and type in following commands.
To install g++,type:

[me@linuxbox]$ sudo apt-get install build-essential

This command installs other essential packages too. Alternatively, you may also try:

[me@linuxbox usr]$ sudo apt-get install g++
  
Thats it, you're good to go with your c++ code. Suppose you have a file called hello.cpp. To compile this program , type in:

[me@linuxbox usr]$ g++ hello.cpp  -o hello

-o option converts the compiled code into executable code and stores it in file "hello". To execute this file , type in:

[me@linuxbox usr]$ ./hello

and you see your output on the terminal itself.

No comments:

Post a Comment