How to Compile a C++ Code in Linux Using GNU compiler Collection

In order to execute/run an application, you have to compile your code in any programming language. The compiler reads all the instructions written in the code step by step and then creates the executable files of the program which are actually your code’s output. A number of paid compilers are available in the market but there is no need to pay for a compiler as this can be done free. GNU compiler collection is a free package that is available for Linux or any UNIX based operating system and it is very easy to use too. It contains a set of different compilers like G++ that is used to compile codes written in C++. Most of the UNIX based operating systems have these packages pre-installed but if they are not installed then you have to install this package from your distribution repository that is included in your OS.

Instructions

  • 1

    To use the GNU compiler package, make sure you have it installed on your OS. In order to check that, open terminal window on your UNIX machine.

  • 2

    To check if GCC is installed on your computer or not, write without quotes “which gcc”. If it returns you with the version of GCC installed then it is fine, if not then you have to install GNU compiler collection by yourself. To install compiler collection yourself, go to your machine’s distribution software repository and search for GCC and then install it.

  • 3

    Once you are done with checking and installation of the compiler, now is the phase to compile your own code. In your command prompt window or terminal, type “g++ -o exec program.cpp” without quotes. You have to replace program with the name of program you want to compile and replace exec with the name you want to assign to your executable new file which will show you the output of your code.

    Note: Make sure that the current directory shown on your terminal window contains the file you want to compile.

  • 4

    Now run the executable of your program. Type without quotes in your command prompt window “./exec”. It will execute the executable and will show you the output of your code.

Leave a Reply

Your email address will not be published. Required fields are marked *


9 × = fifty four